Skip to content

Commit 5e4d70e

Browse files
committed
Simplify API a bit, and another unit test.
1 parent ddf5189 commit 5e4d70e

File tree

2 files changed

+44
-27
lines changed

2 files changed

+44
-27
lines changed

memapi/src/lib.rs

+3-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use smallstr::SmallString;
21
use std::ffi::CStr;
32
use std::os::raw::c_char;
43
use std::str;
@@ -25,16 +24,9 @@ pub unsafe extern "C" fn pymemprofile_start_call(
2524
file_name: *const c_char,
2625
func_name: *const c_char,
2726
) {
28-
let function_name = SmallString::from_str(str::from_utf8_unchecked(
29-
CStr::from_ptr(func_name).to_bytes(),
30-
));
31-
let module_name = SmallString::from_str(str::from_utf8_unchecked(
32-
CStr::from_ptr(file_name).to_bytes(),
33-
));
34-
let call_site = memorytracking::CallSite {
35-
function_name,
36-
module_name,
37-
};
27+
let function_name = str::from_utf8_unchecked(CStr::from_ptr(func_name).to_bytes());
28+
let module_name = str::from_utf8_unchecked(CStr::from_ptr(file_name).to_bytes());
29+
let call_site = memorytracking::CallSite::new(module_name, function_name);
3830
memorytracking::start_call(call_site);
3931
}
4032

memapi/src/memorytracking.rs

+41-16
Original file line numberDiff line numberDiff line change
@@ -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)]
5151
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+
}
5463
}
5564

5665
impl fmt::Display for CallSite {
@@ -295,10 +304,9 @@ fn write_flamegraph<'a, I: IntoIterator<Item = &'a str>>(
295304

296305
#[cfg(test)]
297306
mod 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

Comments
 (0)