Reduce Vec allocations

pull/3009/head
dullbananas 2023-06-10 05:33:52 +00:00
parent ecc9469a02
commit 29e46abf44
13 changed files with 32 additions and 46 deletions

View File

@ -98,7 +98,6 @@ pub async fn send_local_notifs(
for mention in mentions for mention in mentions
.iter() .iter()
.filter(|m| m.is_local(&context.settings().hostname) && m.name.ne(&person.name)) .filter(|m| m.is_local(&context.settings().hostname) && m.name.ne(&person.name))
.collect::<Vec<&MentionData>>()
{ {
let mention_name = mention.name.clone(); let mention_name = mention.name.clone();
let user_view = LocalUserView::read_from_name(context.pool(), &mention_name).await; let user_view = LocalUserView::read_from_name(context.pool(), &mention_name).await;

View File

@ -640,10 +640,9 @@ pub async fn remove_user_data(
let first_mod_communities = CommunityModeratorView::get_community_first_mods(pool).await?; let first_mod_communities = CommunityModeratorView::get_community_first_mods(pool).await?;
// Filter to only this banned users top communities // Filter to only this banned users top communities
let banned_user_first_communities: Vec<CommunityModeratorView> = first_mod_communities let banned_user_first_communities = first_mod_communities
.into_iter() .into_iter()
.filter(|fmc| fmc.moderator.id == banned_person_id) .filter(|fmc| fmc.moderator.id == banned_person_id);
.collect();
for first_mod_community in banned_user_first_communities { for first_mod_community in banned_user_first_communities {
let community_id = first_mod_community.community.id; let community_id = first_mod_community.community.id;

View File

@ -191,7 +191,7 @@ impl PerformCrud for CreateComment {
pub fn check_comment_depth(comment: &Comment) -> Result<(), LemmyError> { pub fn check_comment_depth(comment: &Comment) -> Result<(), LemmyError> {
let path = &comment.path.0; let path = &comment.path.0;
let length = path.split('.').collect::<Vec<&str>>().len(); let length = path.split('.').count();
if length > MAX_COMMENT_DEPTH_LIMIT { if length > MAX_COMMENT_DEPTH_LIMIT {
Err(LemmyError::from_message("max_comment_depth_reached")) Err(LemmyError::from_message("max_comment_depth_reached"))
} else { } else {

View File

@ -7,7 +7,6 @@ use lemmy_api_common::{
utils::{local_site_to_slur_regex, local_user_view_from_jwt}, utils::{local_site_to_slur_regex, local_user_view_from_jwt},
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
newtypes::PersonId,
source::{ source::{
actor_language::{CommunityLanguage, SiteLanguage}, actor_language::{CommunityLanguage, SiteLanguage},
community::{Community, CommunityUpdateForm}, community::{Community, CommunityUpdateForm},
@ -43,10 +42,11 @@ impl PerformCrud for EditCommunity {
// Verify its a mod (only mods can edit it) // Verify its a mod (only mods can edit it)
let community_id = data.community_id; let community_id = data.community_id;
let mods: Vec<PersonId> = CommunityModeratorView::for_community(context.pool(), community_id) let user_is_mod: bool = CommunityModeratorView::for_community(context.pool(), community_id)
.await .await?
.map(|v| v.into_iter().map(|m| m.moderator.id).collect())?; .into_iter()
if !mods.contains(&local_user_view.person.id) { .any(|m| m.moderator.id == local_user_view.person.id);
if !user_is_mod {
return Err(LemmyError::from_message("not_a_moderator")); return Err(LemmyError::from_message("not_a_moderator"));
} }

View File

@ -43,12 +43,11 @@ pub(crate) async fn send_activity_in_community(
// send to user followers // send to user followers
if !is_mod_action { if !is_mod_action {
inboxes.append( inboxes.extend(
&mut PersonFollower::list_followers(context.pool(), actor.id) PersonFollower::list_followers(context.pool(), actor.id)
.await? .await?
.into_iter() .into_iter()
.map(|p| ApubPerson(p).shared_inbox_or_inbox()) .map(|p| ApubPerson(p).shared_inbox_or_inbox()),
.collect(),
); );
} }

View File

@ -112,7 +112,7 @@ impl CreateOrUpdateNote {
audience: Some(community.id().into()), audience: Some(community.id().into()),
}; };
let tagged_users: Vec<ObjectId<ApubPerson>> = create_or_update let tagged_users = create_or_update
.tag .tag
.iter() .iter()
.filter_map(|t| { .filter_map(|t| {
@ -123,8 +123,7 @@ impl CreateOrUpdateNote {
} }
}) })
.map(|t| t.href.clone()) .map(|t| t.href.clone())
.map(ObjectId::from) .map(ObjectId::<ApubPerson>::from);
.collect();
let mut inboxes = vec![]; let mut inboxes = vec![];
for t in tagged_users { for t in tagged_users {
let person = t.dereference(context).await?; let person = t.dereference(context).await?;

View File

@ -41,13 +41,10 @@ impl Collection for ApubCommunityOutbox {
owner: &Self::Owner, owner: &Self::Owner,
data: &Data<Self::DataType>, data: &Data<Self::DataType>,
) -> Result<Self::Kind, LemmyError> { ) -> Result<Self::Kind, LemmyError> {
let post_list: Vec<ApubPost> = Post::list_for_community(data.pool(), owner.id) let post_list: Vec<Post> = Post::list_for_community(data.pool(), owner.id).await?;
.await? let mut ordered_items = Vec::with_capacity(post_list.len());
.into_iter()
.map(Into::into)
.collect();
let mut ordered_items = vec![];
for post in post_list { for post in post_list {
let post = ApubPost::from(post);
let person = Person::read(data.pool(), post.creator_id).await?.into(); let person = Person::read(data.pool(), post.creator_id).await?.into();
let create = let create =
CreateOrUpdatePage::new(post, &person, owner, CreateOrUpdateType::Create, data).await?; CreateOrUpdatePage::new(post, &person, owner, CreateOrUpdateType::Create, data).await?;

View File

@ -132,18 +132,17 @@ pub(crate) fn check_apub_id_valid_with_strictness(
if is_strict && !local_site_data.allowed_instances.is_empty() { if is_strict && !local_site_data.allowed_instances.is_empty() {
// need to allow this explicitly because apub receive might contain objects from our local // need to allow this explicitly because apub receive might contain objects from our local
// instance. // instance.
let mut allowed_and_local = local_site_data
.allowed_instances
.iter()
.map(|i| i.domain.clone())
.collect::<Vec<String>>();
let local_instance = settings let local_instance = settings
.get_hostname_without_port() .get_hostname_without_port()
.expect("local hostname is valid"); .expect("local hostname is valid");
allowed_and_local.push(local_instance); let mut allowed_and_local = local_site_data
.allowed_instances
.iter()
.map(|i| &i.domain)
.chain([&local_instance]);
let domain = apub_id.domain().expect("apud id has domain").to_string(); let domain = apub_id.domain().expect("apud id has domain").to_string();
if !allowed_and_local.contains(&domain) { if !allowed_and_local.any(|i| i == &domain) {
return Err(LemmyError::from_message( return Err(LemmyError::from_message(
"Federation forbidden by strict allowlist", "Federation forbidden by strict allowlist",
)); ));

View File

@ -11,10 +11,7 @@ use lemmy_db_schema::{
traits::Crud, traits::Crud,
utils::DbPool, utils::DbPool,
}; };
use lemmy_utils::{ use lemmy_utils::{error::LemmyError, utils::mention::scrape_text_for_mentions};
error::LemmyError,
utils::mention::{scrape_text_for_mentions, MentionData},
};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::Value; use serde_json::Value;
use url::Url; use url::Url;
@ -67,10 +64,9 @@ pub async fn collect_non_local_mentions(
let mentions = scrape_text_for_mentions(&comment.content) let mentions = scrape_text_for_mentions(&comment.content)
.into_iter() .into_iter()
// Filter only the non-local ones // Filter only the non-local ones
.filter(|m| !m.is_local(&context.settings().hostname)) .filter(|m| !m.is_local(&context.settings().hostname));
.collect::<Vec<MentionData>>();
for mention in &mentions { for mention in mentions {
let identifier = format!("{}@{}", mention.name, mention.domain); let identifier = format!("{}@{}", mention.name, mention.domain);
let person = webfinger_resolve_actor::<LemmyContext, ApubPerson>(&identifier, context).await; let person = webfinger_resolve_actor::<LemmyContext, ApubPerson>(&identifier, context).await;
if let Ok(person) = person { if let Ok(person) = person {

View File

@ -82,7 +82,7 @@ impl LanguageTag {
langs: Vec<Self>, langs: Vec<Self>,
pool: &DbPool, pool: &DbPool,
) -> Result<Vec<LanguageId>, LemmyError> { ) -> Result<Vec<LanguageId>, LemmyError> {
let mut language_ids = Vec::new(); let mut language_ids = Vec::with_capacity(langs.len());
for l in langs { for l in langs {
let id = l.identifier; let id = l.identifier;

View File

@ -98,8 +98,8 @@ impl Comment {
// left join comment c2 on c2.path <@ c.path and c2.path != c.path // left join comment c2 on c2.path <@ c.path and c2.path != c.path
// group by c.id // group by c.id
let path_split = parent_path.0.split('.').collect::<Vec<&str>>(); let mut path_split = parent_path.0.split('.');
let parent_id = path_split.get(1); let parent_id = path_split.nth(1);
if let Some(parent_id) = parent_id { if let Some(parent_id) = parent_id {
let top_parent = format!("0.{}", parent_id); let top_parent = format!("0.{}", parent_id);

View File

@ -92,13 +92,11 @@ impl CommunityView {
) -> Result<bool, Error> { ) -> Result<bool, Error> {
let is_mod = CommunityModeratorView::for_community(pool, community_id) let is_mod = CommunityModeratorView::for_community(pool, community_id)
.await .await
.map(|v| { .is_ok_and(|v| {
v.into_iter() v.into_iter()
.map(|m| m.moderator.id) .map(|m| m.moderator.id)
.collect::<Vec<PersonId>>() .any(|i| i == person_id)
}) });
.unwrap_or_default()
.contains(&person_id);
if is_mod { if is_mod {
return Ok(true); return Ok(true);
} }

View File

@ -414,7 +414,7 @@ fn create_post_items(
posts: Vec<PostView>, posts: Vec<PostView>,
protocol_and_hostname: &str, protocol_and_hostname: &str,
) -> Result<Vec<Item>, LemmyError> { ) -> Result<Vec<Item>, LemmyError> {
let mut items: Vec<Item> = Vec::new(); let mut items: Vec<Item> = Vec::with_capacity(posts.len());
for p in posts { for p in posts {
let mut i = ItemBuilder::default(); let mut i = ItemBuilder::default();