@@ -49,8 +49,17 @@ thread_local!(static THREAD_CALLSTACK: RefCell<Callstack> = RefCell::new(Callsta
49
49
/// A particular place where a call happened.
50
50
#[ derive( Clone , Debug , PartialEq , Eq , Hash ) ]
51
51
pub struct CallSite {
52
- pub module_name : SmallString < [ u8 ; 24 ] > ,
53
- pub function_name : SmallString < [ u8 ; 24 ] > ,
52
+ module_name : SmallString < [ u8 ; 24 ] > ,
53
+ function_name : SmallString < [ u8 ; 24 ] > ,
54
+ }
55
+
56
+ impl CallSite {
57
+ pub fn new ( module_name : & str , function_name : & str ) -> CallSite {
58
+ CallSite {
59
+ module_name : SmallString :: from_str ( module_name) ,
60
+ function_name : SmallString :: from_str ( function_name) ,
61
+ }
62
+ }
54
63
}
55
64
56
65
impl fmt:: Display for CallSite {
@@ -295,10 +304,9 @@ fn write_flamegraph<'a, I: IntoIterator<Item = &'a str>>(
295
304
296
305
#[ cfg( test) ]
297
306
mod tests {
298
- use super :: { AllocationTracker , CallSite , Callstack } ;
307
+ use super :: { AllocationTracker , CallSite , CallSites , Callstack } ;
299
308
use itertools:: Itertools ;
300
309
use proptest:: prelude:: * ;
301
- use smallstr:: SmallString ;
302
310
use std:: collections;
303
311
304
312
proptest ! {
@@ -354,21 +362,38 @@ mod tests {
354
362
assert_eq ! ( tracker. peak_allocated_bytes, 2123 ) ;
355
363
}
356
364
365
+ #[ test]
366
+ fn callsites_notices_duplicate_callsites ( ) {
367
+ let callsite1 = CallSite :: new ( "a" , "af" ) ;
368
+ let callsite2 = CallSite :: new ( "b" , "af" ) ;
369
+ let callsite3 = CallSite :: new ( "a" , "bf" ) ;
370
+ let mut callsites = CallSites :: new ( ) ;
371
+ let id1 = callsites. get_or_insert_id ( callsite1. clone ( ) ) ;
372
+ let id1b = callsites. get_or_insert_id ( callsite1) ;
373
+ let id2 = callsites. get_or_insert_id ( callsite2) ;
374
+ let id3 = callsites. get_or_insert_id ( callsite3. clone ( ) ) ;
375
+ let id3b = callsites. get_or_insert_id ( callsite3. clone ( ) ) ;
376
+ assert_eq ! ( id1, id1b) ;
377
+ assert_ne ! ( id1, id2) ;
378
+ assert_ne ! ( id1, id3) ;
379
+ assert_ne ! ( id2, id3) ;
380
+ assert_eq ! ( id3, id3b) ;
381
+ }
382
+
357
383
#[ test]
358
384
fn combine_callstacks_and_sum_allocations ( ) {
359
385
let mut tracker = AllocationTracker :: new ( ) ;
360
- let id1 = tracker. call_sites . get_or_insert_id ( CallSite {
361
- module_name : SmallString :: from_str ( "a" ) ,
362
- function_name : SmallString :: from_str ( "af" ) ,
363
- } ) ;
364
- let id2 = tracker. call_sites . get_or_insert_id ( CallSite {
365
- module_name : SmallString :: from_str ( "b" ) ,
366
- function_name : SmallString :: from_str ( "bf" ) ,
367
- } ) ;
368
- let id3 = tracker. call_sites . get_or_insert_id ( CallSite {
369
- module_name : SmallString :: from_str ( "c" ) ,
370
- function_name : SmallString :: from_str ( "cf" ) ,
371
- } ) ;
386
+ let id1 = tracker
387
+ . call_sites
388
+ . get_or_insert_id ( CallSite :: new ( "a" , "af" ) ) ;
389
+
390
+ let id2 = tracker
391
+ . call_sites
392
+ . get_or_insert_id ( CallSite :: new ( "b" , "bf" ) ) ;
393
+
394
+ let id3 = tracker
395
+ . call_sites
396
+ . get_or_insert_id ( CallSite :: new ( "c" , "cf" ) ) ;
372
397
let mut cs1 = Callstack :: new ( ) ;
373
398
cs1. start_call ( id1) ;
374
399
cs1. start_call ( id2) ;
0 commit comments