@@ -49,8 +49,17 @@ thread_local!(static THREAD_CALLSTACK: RefCell<Callstack> = RefCell::new(Callsta
4949/// A particular place where a call happened.
5050#[ derive( Clone , Debug , PartialEq , Eq , Hash ) ]
5151pub 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+ }
5463}
5564
5665impl fmt:: Display for CallSite {
@@ -295,10 +304,9 @@ fn write_flamegraph<'a, I: IntoIterator<Item = &'a str>>(
295304
296305#[ cfg( test) ]
297306mod tests {
298- use super :: { AllocationTracker , CallSite , Callstack } ;
307+ use super :: { AllocationTracker , CallSite , CallSites , Callstack } ;
299308 use itertools:: Itertools ;
300309 use proptest:: prelude:: * ;
301- use smallstr:: SmallString ;
302310 use std:: collections;
303311
304312 proptest ! {
@@ -354,21 +362,38 @@ mod tests {
354362 assert_eq ! ( tracker. peak_allocated_bytes, 2123 ) ;
355363 }
356364
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+
357383 #[ test]
358384 fn combine_callstacks_and_sum_allocations ( ) {
359385 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" ) ) ;
372397 let mut cs1 = Callstack :: new ( ) ;
373398 cs1. start_call ( id1) ;
374399 cs1. start_call ( id2) ;
0 commit comments