Skip to content

Commit 1a38061

Browse files
Drop prev_commit from graph API responses
This is always -1 from the commit index, so there's not much point in sending it too. We also switch the commit index to u16 vs u32 because we're not likely to have ~65k commits (there's only 16k bors commits *total* in rust-lang/rust master!).
1 parent c458531 commit 1a38061

File tree

3 files changed

+45
-27
lines changed

3 files changed

+45
-27
lines changed

site/src/api.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,7 @@ pub mod graph {
235235

236236
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
237237
pub struct GraphData {
238-
pub commit: u32,
239-
pub prev_commit: Option<u32>,
238+
pub commit: u16,
240239
pub absolute: f32,
241240
pub percent: f32,
242241
pub y: f32,

site/src/server.rs

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use parking_lot::Mutex;
1111
use std::borrow::Cow;
1212
use std::cmp::Ordering;
1313
use std::collections::HashMap;
14-
use std::convert::TryFrom;
14+
use std::convert::TryInto;
1515
use std::fmt;
1616
use std::fs;
1717
use std::net::SocketAddr;
@@ -279,29 +279,36 @@ pub fn handle_next_commit(data: &InputData) -> Option<String> {
279279
.map(|c| c.0.sha.to_string())
280280
}
281281

282-
struct CommitCache {
283-
commits_cache: Vec<Sha>,
284-
commit_in_cache: HashMap<Sha, usize>,
282+
struct CommitIdxCache {
283+
commit_idx: HashMap<Sha, u16>,
284+
commits: Vec<Sha>,
285285
}
286286

287-
impl CommitCache {
288-
fn new() -> Self {
287+
impl CommitIdxCache {
288+
fn new(data: &[DateData]) -> Self {
289+
let mut commit_idx = HashMap::with_capacity(data.len());
290+
let mut commits = Vec::with_capacity(data.len());
291+
for (idx, cd) in data.iter().enumerate() {
292+
commit_idx.insert(
293+
cd.commit.clone(),
294+
idx.try_into().unwrap_or_else(|_| {
295+
panic!("{} too big", idx);
296+
}),
297+
);
298+
commits.push(cd.commit.clone());
299+
}
289300
Self {
290-
commits_cache: Vec::new(),
291-
commit_in_cache: HashMap::new(),
301+
commit_idx,
302+
commits,
292303
}
293304
}
294305

295-
fn insert(&mut self, commit: Sha) -> u32 {
296-
let idx = if let Some(idx) = self.commit_in_cache.get(&commit) {
306+
fn lookup(&self, commit: Sha) -> u16 {
307+
if let Some(idx) = self.commit_idx.get(&commit) {
297308
*idx
298309
} else {
299-
let idx = self.commits_cache.len();
300-
self.commits_cache.push(commit);
301-
self.commit_in_cache.insert(commit, idx);
302-
idx
303-
};
304-
u32::try_from(idx).unwrap_or_else(|_| panic!("{} did not fit into u32", idx))
310+
panic!("unknown commit {}", commit)
311+
}
305312
}
306313
}
307314

@@ -324,7 +331,7 @@ pub async fn handle_graph(body: graph::Request, data: &InputData) -> ServerResul
324331
let mut initial_check_base_compile = None;
325332
let mut initial_release_base_compile = None;
326333

327-
let mut cc = CommitCache::new();
334+
let cc = CommitIdxCache::new(&out);
328335

329336
for date_data in out {
330337
let commit = date_data.commit;
@@ -349,9 +356,14 @@ pub async fn handle_graph(body: graph::Request, data: &InputData) -> ServerResul
349356
.or_insert_with(|| Vec::<graph::GraphData>::with_capacity(elements));
350357
let first = entry.first().map(|d| d.absolute as f32);
351358
let percent = first.map_or(0.0, |f| (value - f) / f * 100.0);
359+
// Assert that the previous commit is always, well, the
360+
// previous one. This should always be true, in which
361+
// just a boolean exists.
362+
if let Some(prev_commit) = last_commit.map(|l| cc.lookup(l)) {
363+
assert_eq!(cc.lookup(commit).checked_sub(1), Some(prev_commit));
364+
}
352365
entry.push(graph::GraphData {
353-
commit: cc.insert(commit),
354-
prev_commit: last_commit.map(|l| cc.insert(l)),
366+
commit: cc.lookup(commit),
355367
absolute: value,
356368
percent: percent,
357369
y: if body.absolute { value } else { percent },
@@ -426,9 +438,15 @@ pub async fn handle_graph(body: graph::Request, data: &InputData) -> ServerResul
426438
};
427439
let first = entry.first().map(|d: &graph::GraphData| d.absolute as f32);
428440
let percent = first.map_or(0.0, |f| (value - f) / f * 100.0);
441+
442+
// Assert that the previous commit is always, well, the
443+
// previous one. This should always be true, in which
444+
// just a boolean exists.
445+
if let Some(prev_commit) = last_commit.map(|l| cc.lookup(l)) {
446+
assert_eq!(cc.lookup(commit).checked_sub(1), Some(prev_commit));
447+
}
429448
entry.push(graph::GraphData {
430-
commit: cc.insert(commit),
431-
prev_commit: last_commit.map(|l| cc.insert(l)),
449+
commit: cc.lookup(commit),
432450
absolute: value,
433451
percent: percent,
434452
y: if body.absolute { value } else { percent },
@@ -471,7 +489,7 @@ pub async fn handle_graph(body: graph::Request, data: &InputData) -> ServerResul
471489
.map(|(k, v)| (k, v.into_iter().map(|(k, v)| (k.into_owned(), v)).collect()))
472490
.collect(),
473491
colors: vec![String::new(), String::from(INTERPOLATED_COLOR)],
474-
commits: cc.commits_cache,
492+
commits: cc.commits,
475493
})
476494
}
477495

site/static/index.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@
5959
}
6060

6161
function clickHandler(event) {
62-
if (this.options.prev_commit !== null) {
62+
if (this.options.prev_commit !== false) {
63+
const prev_commit = this.options.commit
6364
window.open("/compare.html?start=" + this.options.prev_commit +
6465
"&end=" + this.options.commit +
6566
"&stat=" + stat, "_blank");
@@ -110,9 +111,9 @@
110111
name,
111112
animation: false,
112113
allowPointSelect: true,
113-
data: data.map(({commit, prev_commit, absolute, percent, y, x, is_interpolated}) => ({
114+
data: data.map(({commit, absolute, percent, y, x, is_interpolated}) => ({
114115
commit: response.commits[commit],
115-
prev_commit: commit == 0 ? null : response.commits[prev_commit],
116+
prev_commit: commit == 0 ? null : response.commits[commit - 1],
116117
absolute, percent, y, x, color: response.colors[is_interpolated ? 1 : 0],
117118
})),
118119
marker: {

0 commit comments

Comments
 (0)