Skip to content

Commit 801edab

Browse files
Use a single query to fetch self profile results
This avoids numerous roundtrips to the database, saving a lot of time (especially with the postgres backend).
1 parent dfba134 commit 801edab

File tree

4 files changed

+109
-26
lines changed

4 files changed

+109
-26
lines changed

database/src/pool.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ pub trait Connection: Send + Sync {
4949
series: &[u32],
5050
cid: &[Option<ArtifactIdNumber>],
5151
) -> Vec<Vec<Option<f64>>>;
52+
async fn get_self_profile(
53+
&self,
54+
cid: ArtifactIdNumber,
55+
crate_: &str,
56+
profile: &str,
57+
cache: &str,
58+
) -> HashMap<crate::QueryLabel, QueryDatum>;
5259
async fn get_self_profile_query(
5360
&self,
5461
series: u32,

database/src/pool/postgres.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ pub struct CachedStatements {
232232
get_pstat: Statement,
233233
insert_pstat: Statement,
234234
get_self_profile_query: Statement,
235+
get_self_profile: Statement,
235236
insert_self_profile_query: Statement,
236237
select_self_query_series: Statement,
237238
insert_self_query_series: Statement,
@@ -333,6 +334,17 @@ impl PostgresConnection {
333334
)
334335
.await
335336
.unwrap(),
337+
get_self_profile: conn.prepare("
338+
select
339+
query, self_time, blocked_time, incremental_load_time, number_of_cache_hits, invocation_count
340+
from self_profile_query_series
341+
join self_profile_query on self_profile_query_series.id = self_profile_query.series
342+
where
343+
crate = $1
344+
and profile = $2
345+
and cache = $3
346+
and aid = $4
347+
").await.unwrap(),
336348
insert_self_profile_query: conn
337349
.prepare(
338350
"insert into self_profile_query(
@@ -563,6 +575,40 @@ where
563575
invocation_count: row.get::<_, i32>(4) as u32,
564576
})
565577
}
578+
async fn get_self_profile(
579+
&self,
580+
cid: ArtifactIdNumber,
581+
crate_: &str,
582+
profile: &str,
583+
cache: &str,
584+
) -> HashMap<crate::QueryLabel, crate::QueryDatum> {
585+
let rows = self
586+
.conn()
587+
.query(
588+
&self.statements().get_self_profile,
589+
&[&crate_, &profile, &cache, &(cid.0 as i16)],
590+
)
591+
.await
592+
.unwrap();
593+
594+
rows.into_iter()
595+
.map(|r| {
596+
let self_time: i64 = r.get(1);
597+
let blocked_time: i64 = r.get(2);
598+
let incremental_load_time: i64 = r.get(3);
599+
(
600+
r.get::<_, &str>(0).into(),
601+
crate::QueryDatum {
602+
self_time: Duration::from_nanos(self_time as u64),
603+
blocked_time: Duration::from_nanos(blocked_time as u64),
604+
incremental_load_time: Duration::from_nanos(incremental_load_time as u64),
605+
number_of_cache_hits: r.get::<_, i32>(4) as u32,
606+
invocation_count: r.get::<_, i32>(5) as u32,
607+
},
608+
)
609+
})
610+
.collect()
611+
}
566612
async fn get_error(&self, cid: crate::ArtifactIdNumber) -> HashMap<String, Option<String>> {
567613
let rows = self
568614
.conn()

database/src/pool/sqlite.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,45 @@ impl Connection for SqliteConnection {
397397
.optional()
398398
.unwrap()
399399
}
400+
async fn get_self_profile(
401+
&self,
402+
cid: ArtifactIdNumber,
403+
crate_: &str,
404+
profile: &str,
405+
cache: &str,
406+
) -> HashMap<crate::QueryLabel, crate::QueryDatum> {
407+
self.raw_ref()
408+
.prepare_cached("
409+
select
410+
query, self_time, blocked_time, incremental_load_time, number_of_cache_hits, invocation_count
411+
from self_profile_query_series
412+
join self_profile_query on self_profile_query_series.id = self_profile_query.series
413+
where
414+
crate = ?
415+
and profile = ?
416+
and cache = ?
417+
and aid = ?
418+
")
419+
.unwrap()
420+
.query_map(params![&crate_, &profile, &cache, &cid.0], |r| {
421+
let self_time: i64 = r.get(1)?;
422+
let blocked_time: i64 = r.get(2)?;
423+
let incremental_load_time: i64 = r.get(3)?;
424+
Ok((
425+
r.get::<_, String>(0)?.as_str().into(),
426+
crate::QueryDatum {
427+
self_time: Duration::from_nanos(self_time as u64),
428+
blocked_time: Duration::from_nanos(blocked_time as u64),
429+
incremental_load_time: Duration::from_nanos(incremental_load_time as u64),
430+
number_of_cache_hits: r.get::<_, i32>(4)? as u32,
431+
invocation_count: r.get::<_, i32>(5)? as u32,
432+
},
433+
))
434+
})
435+
.unwrap()
436+
.collect::<Result<_, _>>()
437+
.unwrap()
438+
}
400439
async fn get_error(&self, cid: crate::ArtifactIdNumber) -> HashMap<String, Option<String>> {
401440
self.raw_ref()
402441
.prepare_cached(

site/src/selector.rs

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -673,32 +673,23 @@ impl SelfProfile {
673673
res.push(None);
674674
continue;
675675
};
676-
for label in labels.iter() {
677-
let query = crate::db::DbLabel::SelfProfileQuery {
678-
krate,
679-
profile,
680-
cache,
681-
query: *label,
682-
};
683-
let qid = if let Some(qid) = query.lookup(&idx) {
684-
qid
685-
} else {
686-
continue;
687-
};
688-
if let Some(qd) = conn.get_self_profile_query(qid, cid_id).await {
689-
queries.push(QueryData {
690-
label: *label,
691-
self_time: qd.self_time.as_nanos().try_into().unwrap(),
692-
number_of_cache_hits: qd.number_of_cache_hits,
693-
invocation_count: qd.invocation_count,
694-
blocked_time: qd.blocked_time.as_nanos().try_into().unwrap(),
695-
incremental_load_time: qd
696-
.incremental_load_time
697-
.as_nanos()
698-
.try_into()
699-
.unwrap(),
700-
});
701-
}
676+
let cid_data = conn
677+
.get_self_profile(
678+
cid_id,
679+
krate.as_str(),
680+
&profile.to_string(),
681+
&cache.to_string(),
682+
)
683+
.await;
684+
for (label, qd) in cid_data {
685+
queries.push(QueryData {
686+
label,
687+
self_time: qd.self_time.as_nanos().try_into().unwrap(),
688+
number_of_cache_hits: qd.number_of_cache_hits,
689+
invocation_count: qd.invocation_count,
690+
blocked_time: qd.blocked_time.as_nanos().try_into().unwrap(),
691+
incremental_load_time: qd.incremental_load_time.as_nanos().try_into().unwrap(),
692+
});
702693
}
703694
if queries.is_empty() {
704695
res.push(None);

0 commit comments

Comments
 (0)