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
.iter()
.filter(|m| m.is_local(&context.settings().hostname) && m.name.ne(&person.name))
.collect::<Vec<&MentionData>>()
{
let mention_name = mention.name.clone();
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?;
// 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()
.filter(|fmc| fmc.moderator.id == banned_person_id)
.collect();
.filter(|fmc| fmc.moderator.id == banned_person_id);
for first_mod_community in banned_user_first_communities {
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> {
let path = &comment.path.0;
let length = path.split('.').collect::<Vec<&str>>().len();
let length = path.split('.').count();
if length > MAX_COMMENT_DEPTH_LIMIT {
Err(LemmyError::from_message("max_comment_depth_reached"))
} else {

View File

@ -7,7 +7,6 @@ use lemmy_api_common::{
utils::{local_site_to_slur_regex, local_user_view_from_jwt},
};
use lemmy_db_schema::{
newtypes::PersonId,
source::{
actor_language::{CommunityLanguage, SiteLanguage},
community::{Community, CommunityUpdateForm},
@ -43,10 +42,11 @@ impl PerformCrud for EditCommunity {
// Verify its a mod (only mods can edit it)
let community_id = data.community_id;
let mods: Vec<PersonId> = CommunityModeratorView::for_community(context.pool(), community_id)
.await
.map(|v| v.into_iter().map(|m| m.moderator.id).collect())?;
if !mods.contains(&local_user_view.person.id) {
let user_is_mod: bool = CommunityModeratorView::for_community(context.pool(), community_id)
.await?
.into_iter()
.any(|m| m.moderator.id == local_user_view.person.id);
if !user_is_mod {
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
if !is_mod_action {
inboxes.append(
&mut PersonFollower::list_followers(context.pool(), actor.id)
inboxes.extend(
PersonFollower::list_followers(context.pool(), actor.id)
.await?
.into_iter()
.map(|p| ApubPerson(p).shared_inbox_or_inbox())
.collect(),
.map(|p| ApubPerson(p).shared_inbox_or_inbox()),
);
}

View File

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

View File

@ -41,13 +41,10 @@ impl Collection for ApubCommunityOutbox {
owner: &Self::Owner,
data: &Data<Self::DataType>,
) -> Result<Self::Kind, LemmyError> {
let post_list: Vec<ApubPost> = Post::list_for_community(data.pool(), owner.id)
.await?
.into_iter()
.map(Into::into)
.collect();
let mut ordered_items = vec![];
let post_list: Vec<Post> = Post::list_for_community(data.pool(), owner.id).await?;
let mut ordered_items = Vec::with_capacity(post_list.len());
for post in post_list {
let post = ApubPost::from(post);
let person = Person::read(data.pool(), post.creator_id).await?.into();
let create =
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() {
// need to allow this explicitly because apub receive might contain objects from our local
// instance.
let mut allowed_and_local = local_site_data
.allowed_instances
.iter()
.map(|i| i.domain.clone())
.collect::<Vec<String>>();
let local_instance = settings
.get_hostname_without_port()
.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();
if !allowed_and_local.contains(&domain) {
if !allowed_and_local.any(|i| i == &domain) {
return Err(LemmyError::from_message(
"Federation forbidden by strict allowlist",
));

View File

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

View File

@ -82,7 +82,7 @@ impl LanguageTag {
langs: Vec<Self>,
pool: &DbPool,
) -> Result<Vec<LanguageId>, LemmyError> {
let mut language_ids = Vec::new();
let mut language_ids = Vec::with_capacity(langs.len());
for l in langs {
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
// group by c.id
let path_split = parent_path.0.split('.').collect::<Vec<&str>>();
let parent_id = path_split.get(1);
let mut path_split = parent_path.0.split('.');
let parent_id = path_split.nth(1);
if let Some(parent_id) = parent_id {
let top_parent = format!("0.{}", parent_id);

View File

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

View File

@ -414,7 +414,7 @@ fn create_post_items(
posts: Vec<PostView>,
protocol_and_hostname: &str,
) -> 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 {
let mut i = ItemBuilder::default();