Skip to content

Commit 765f96e

Browse files
authored
Merge pull request #9737 from eth3lbert/slice-refs
Avoid unnecessary allocations
2 parents 9519ec2 + d28d84d commit 765f96e

File tree

7 files changed

+24
-34
lines changed

7 files changed

+24
-34
lines changed

src/controllers/krate/metadata.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ pub async fn show(app: AppState, Path(name): Path<String>, req: Parts) -> AppRes
6565
let versions = versions_and_publishers
6666
.iter()
6767
.map(|(v, _)| v)
68-
.cloned()
6968
.collect::<Vec<_>>();
69+
let actions = VersionOwnerAction::for_versions(conn, &versions)?;
7070
Some(
7171
versions_and_publishers
7272
.into_iter()
73-
.zip(VersionOwnerAction::for_versions(conn, &versions)?)
73+
.zip(actions)
7474
.map(|((v, pb), aas)| (v, pb, aas))
7575
.collect::<Vec<_>>(),
7676
)
@@ -267,12 +267,12 @@ pub async fn reverse_dependencies(
267267
.load(conn)?;
268268
let versions = versions_and_publishers
269269
.iter()
270-
.map(|(v, _, _)| v)
271-
.cloned()
270+
.map(|(v, ..)| v)
272271
.collect::<Vec<_>>();
272+
let actions = VersionOwnerAction::for_versions(conn, &versions)?;
273273
let versions = versions_and_publishers
274274
.into_iter()
275-
.zip(VersionOwnerAction::for_versions(conn, &versions)?)
275+
.zip(actions)
276276
.map(|((version, krate_name, published_by), actions)| {
277277
EncodableVersion::from(version, &krate_name, published_by, actions)
278278
})

src/controllers/krate/search.rs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -214,15 +214,7 @@ pub async fn search(app: AppState, req: Parts) -> AppResult<Json<Value>> {
214214
)
215215
};
216216

217-
let perfect_matches = data.iter().map(|&(_, b, _, _, _)| b).collect::<Vec<_>>();
218-
let downloads = data
219-
.iter()
220-
.map(|&(_, _, total, recent, _)| (total, recent.unwrap_or(0)))
221-
.collect::<Vec<_>>();
222-
let crates = data
223-
.into_iter()
224-
.map(|(c, _, _, _, _)| c)
225-
.collect::<Vec<_>>();
217+
let crates = data.iter().map(|(c, ..)| c).collect::<Vec<_>>();
226218

227219
let versions: Vec<Version> = info_span!("db.query", message = "SELECT ... FROM versions")
228220
.in_scope(|| crates.versions().load(conn))?;
@@ -232,17 +224,15 @@ pub async fn search(app: AppState, req: Parts) -> AppResult<Json<Value>> {
232224
.map(TopVersions::from_versions);
233225

234226
let crates = versions
235-
.zip(crates)
236-
.zip(perfect_matches)
237-
.zip(downloads)
238-
.map(|(((max_version, krate), perfect_match), (total, recent))| {
227+
.zip(data)
228+
.map(|(max_version, (krate, perfect_match, total, recent, _))| {
239229
EncodableCrate::from_minimal(
240230
krate,
241231
Some(&max_version),
242232
Some(vec![]),
243233
perfect_match,
244234
total,
245-
Some(recent),
235+
Some(recent.unwrap_or(0)),
246236
)
247237
})
248238
.collect::<Vec<_>>();

src/controllers/krate/versions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ pub async fn versions(
6868
.data
6969
.iter()
7070
.map(|(v, _)| v)
71-
.cloned()
7271
.collect::<Vec<_>>();
72+
let actions = VersionOwnerAction::for_versions(conn, &versions)?;
7373
let versions = versions_and_publishers
7474
.data
7575
.into_iter()
76-
.zip(VersionOwnerAction::for_versions(conn, &versions)?)
76+
.zip(actions)
7777
.map(|((v, pb), aas)| EncodableVersion::from(v, &crate_name, pb, aas))
7878
.collect::<Vec<_>>();
7979

src/controllers/summary.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,14 @@ pub async fn summary(state: AppState) -> AppResult<Json<Value>> {
2727
conn: &mut impl Conn,
2828
data: Vec<(Crate, i64, Option<i64>)>,
2929
) -> AppResult<Vec<EncodableCrate>> {
30-
let downloads = data
31-
.iter()
32-
.map(|&(_, total, recent)| (total, recent))
33-
.collect::<Vec<_>>();
34-
35-
let krates = data.into_iter().map(|(c, _, _)| c).collect::<Vec<_>>();
36-
30+
let krates = data.iter().map(|(c, ..)| c).collect::<Vec<_>>();
3731
let versions: Vec<Version> = krates.versions().load(conn)?;
3832
versions
3933
.grouped_by(&krates)
4034
.into_iter()
4135
.map(TopVersions::from_versions)
42-
.zip(krates)
43-
.zip(downloads)
44-
.map(|((top_versions, krate), (total, recent))| {
36+
.zip(data)
37+
.map(|(top_versions, (krate, total, recent))| {
4538
Ok(EncodableCrate::from_minimal(
4639
krate,
4740
Some(&top_versions),

src/controllers/user/me.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,11 @@ pub async fn updates(app: AppState, req: Parts) -> AppResult<Json<Value>> {
8585
.pages_pagination(PaginationOptions::builder().gather(&req)?);
8686
let data: Paginated<(Version, String, Option<User>)> = query.load(conn)?;
8787
let more = data.next_page_params().is_some();
88-
let versions = data.iter().map(|(v, _, _)| v).cloned().collect::<Vec<_>>();
88+
let versions = data.iter().map(|(v, ..)| v).collect::<Vec<_>>();
89+
let actions = VersionOwnerAction::for_versions(conn, &versions)?;
8990
let data = data
9091
.into_iter()
91-
.zip(VersionOwnerAction::for_versions(conn, &versions)?)
92+
.zip(actions)
9293
.map(|((v, cn, pb), voas)| (v, cn, pb, voas));
9394

9495
let versions = data

src/models/action.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ impl VersionOwnerAction {
6565

6666
pub fn for_versions(
6767
conn: &mut impl Conn,
68-
versions: &[Version],
68+
versions: &[&Version],
6969
) -> QueryResult<Vec<Vec<(Self, User)>>> {
7070
Ok(Self::belonging_to(versions)
7171
.inner_join(users::table)

src/models/krate.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,12 @@ impl CrateVersions for [Crate] {
494494
}
495495
}
496496

497+
impl CrateVersions for [&Crate] {
498+
fn all_versions(&self) -> versions::BoxedQuery<'_, Pg> {
499+
Version::belonging_to(self).into_boxed()
500+
}
501+
}
502+
497503
#[derive(Debug, Eq, PartialEq, thiserror::Error)]
498504
pub enum InvalidFeature {
499505
#[error("feature cannot be empty")]

0 commit comments

Comments
 (0)