@@ -11,7 +11,7 @@ use parking_lot::Mutex;
11
11
use std:: borrow:: Cow ;
12
12
use std:: cmp:: Ordering ;
13
13
use std:: collections:: HashMap ;
14
- use std:: convert:: TryFrom ;
14
+ use std:: convert:: TryInto ;
15
15
use std:: fmt;
16
16
use std:: fs;
17
17
use std:: net:: SocketAddr ;
@@ -279,29 +279,36 @@ pub fn handle_next_commit(data: &InputData) -> Option<String> {
279
279
. map ( |c| c. 0 . sha . to_string ( ) )
280
280
}
281
281
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 > ,
285
285
}
286
286
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
+ }
289
300
Self {
290
- commits_cache : Vec :: new ( ) ,
291
- commit_in_cache : HashMap :: new ( ) ,
301
+ commit_idx ,
302
+ commits ,
292
303
}
293
304
}
294
305
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) {
297
308
* idx
298
309
} 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
+ }
305
312
}
306
313
}
307
314
@@ -324,7 +331,7 @@ pub async fn handle_graph(body: graph::Request, data: &InputData) -> ServerResul
324
331
let mut initial_check_base_compile = None ;
325
332
let mut initial_release_base_compile = None ;
326
333
327
- let mut cc = CommitCache :: new ( ) ;
334
+ let cc = CommitIdxCache :: new ( & out ) ;
328
335
329
336
for date_data in out {
330
337
let commit = date_data. commit ;
@@ -349,9 +356,14 @@ pub async fn handle_graph(body: graph::Request, data: &InputData) -> ServerResul
349
356
. or_insert_with ( || Vec :: < graph:: GraphData > :: with_capacity ( elements) ) ;
350
357
let first = entry. first ( ) . map ( |d| d. absolute as f32 ) ;
351
358
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
+ }
352
365
entry. push ( graph:: GraphData {
353
- commit : cc. insert ( commit) ,
354
- prev_commit : last_commit. map ( |l| cc. insert ( l) ) ,
366
+ commit : cc. lookup ( commit) ,
355
367
absolute : value,
356
368
percent : percent,
357
369
y : if body. absolute { value } else { percent } ,
@@ -426,9 +438,15 @@ pub async fn handle_graph(body: graph::Request, data: &InputData) -> ServerResul
426
438
} ;
427
439
let first = entry. first ( ) . map ( |d : & graph:: GraphData | d. absolute as f32 ) ;
428
440
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
+ }
429
448
entry. push ( graph:: GraphData {
430
- commit : cc. insert ( commit) ,
431
- prev_commit : last_commit. map ( |l| cc. insert ( l) ) ,
449
+ commit : cc. lookup ( commit) ,
432
450
absolute : value,
433
451
percent : percent,
434
452
y : if body. absolute { value } else { percent } ,
@@ -471,7 +489,7 @@ pub async fn handle_graph(body: graph::Request, data: &InputData) -> ServerResul
471
489
. map ( |( k, v) | ( k, v. into_iter ( ) . map ( |( k, v) | ( k. into_owned ( ) , v) ) . collect ( ) ) )
472
490
. collect ( ) ,
473
491
colors : vec ! [ String :: new( ) , String :: from( INTERPOLATED_COLOR ) ] ,
474
- commits : cc. commits_cache ,
492
+ commits : cc. commits ,
475
493
} )
476
494
}
477
495
0 commit comments