Skip to content

Commit

Permalink
Split up a few functions to minimize monomorphization-bloat (#1894)
Browse files Browse the repository at this point in the history
Splitting up the large and commonly used functions `ChannelId::send_message`,
`Message::edit`, `ApplicationCommandInteraction::create_interaction_response`
and `create_followup_message` to minimize the length of the function
that is generic over the closure argument.

In most cases, closures count as unique types, and thus every use of
these functions can cause a new copy of that function to be generated
during monomorphization.

By moving most of the function bodies into separate, non-generic functions,
we can minimize the impact of this monomorphization-bloat.

Co-authored-by: nickelc <constantin.nickel@gmail.com>
Co-authored-by: Alex M. M. <acdenissk69@gmail.com>
  • Loading branch information
3 people authored May 8, 2022
1 parent dc22cf2 commit 39fb310
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
11 changes: 7 additions & 4 deletions src/model/channel/channel_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -764,19 +764,22 @@ impl ChannelId {
for<'b> F: FnOnce(&'b mut CreateMessage<'a>) -> &'b mut CreateMessage<'a>,
{
let mut create_message = CreateMessage::default();
let msg = f(&mut create_message);
f(&mut create_message);
self._send_message(http.as_ref(), create_message).await
}

let map = json::hashmap_to_json_map(msg.0.clone());
async fn _send_message<'a>(self, http: &Http, msg: CreateMessage<'a>) -> Result<Message> {
let map = json::hashmap_to_json_map(msg.0);

Message::check_lengths(&map)?;

let message = if msg.2.is_empty() {
http.as_ref().send_message(self.0, &Value::from(map)).await?
} else {
http.as_ref().send_files(self.0, msg.2.clone(), &map).await?
http.as_ref().send_files(self.0, msg.2, &map).await?
};

if let Some(reactions) = msg.1.clone() {
if let Some(reactions) = msg.1 {
for reaction in reactions {
self.create_reaction(&http, message.id, reaction).await?;
}
Expand Down
14 changes: 9 additions & 5 deletions src/model/channel/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,12 @@ impl Message {
}
}
}
let mut builder = self._prepare_edit_builder();
f(&mut builder);
self._send_edit(cache_http.http(), builder).await
}

fn _prepare_edit_builder<'a>(&self) -> EditMessage<'a> {
let mut builder = EditMessage::default();

if !self.content.is_empty() {
Expand All @@ -345,21 +350,20 @@ impl Message {
for attachment in &self.attachments {
builder.add_existing_attachment(attachment.id);
}
builder
}

f(&mut builder);

async fn _send_edit<'a>(&mut self, http: &Http, builder: EditMessage<'a>) -> Result<()> {
let map = json::hashmap_to_json_map(builder.0);

*self = cache_http
.http()
*self = http
.edit_message_and_attachments(
self.channel_id.0,
self.id.0,
&Value::from(map),
builder.1,
)
.await?;

Ok(())
}

Expand Down
14 changes: 14 additions & 0 deletions src/model/interactions/application_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,14 @@ impl ApplicationCommandInteraction {
{
let mut interaction_response = CreateInteractionResponse::default();
f(&mut interaction_response);
self._create_interaction_response(http.as_ref(), interaction_response).await
}

async fn _create_interaction_response<'a>(
&self,
http: &Http,
interaction_response: CreateInteractionResponse<'a>,
) -> Result<()> {
let map = json::hashmap_to_json_map(interaction_response.0);

Message::check_lengths(&map)?;
Expand Down Expand Up @@ -198,7 +205,14 @@ impl ApplicationCommandInteraction {
{
let mut interaction_response = CreateInteractionResponseFollowup::default();
f(&mut interaction_response);
self._create_followup_message(http.as_ref(), interaction_response).await
}

async fn _create_followup_message<'a>(
&self,
http: &Http,
interaction_response: CreateInteractionResponseFollowup<'a>,
) -> Result<Message> {
let map = json::hashmap_to_json_map(interaction_response.0);

Message::check_lengths(&map)?;
Expand Down

0 comments on commit 39fb310

Please sign in to comment.