Skip to content

Commit c499be3

Browse files
stepanchegfacebook-github-bot
authored andcommitted
StackFrameWithContext
Summary: `StackFrameWithContext = StackFrame + StringIndex`. Refactoring for the following diff. Reviewed By: bobyangyf Differential Revision: D38518065 fbshipit-source-id: eb8c67002a1de97ba68f1885665670bddbdfe0a2
1 parent 6509740 commit c499be3

File tree

1 file changed

+31
-13
lines changed

1 file changed

+31
-13
lines changed

starlark/src/values/layout/heap/profile/aggregated.rs

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -209,24 +209,36 @@ pub(crate) struct StackFrame {
209209
pub(crate) calls_x2: u32,
210210
}
211211

212-
impl StackFrame {
212+
struct StackFrameWithContext<'c> {
213+
frame: &'c StackFrame,
214+
strings: &'c StringIndex,
215+
}
216+
217+
impl<'c> StackFrameWithContext<'c> {
218+
fn callees(&self) -> impl Iterator<Item = (&'c str, StackFrameWithContext<'c>)> + '_ {
219+
self.frame.callees.iter().map(move |(id, callee)| {
220+
(
221+
self.strings.get(*id),
222+
StackFrameWithContext {
223+
frame: callee,
224+
strings: self.strings,
225+
},
226+
)
227+
})
228+
}
229+
213230
/// Write this stack frame's data to a file in flamegraph.pl format.
214-
fn write_flame_graph<'a>(
215-
&self,
216-
file: &mut FlameGraphWriter,
217-
stack: &'_ mut Vec<&'a str>,
218-
ids: &'a StringIndex,
219-
) {
220-
for (k, v) in &self.allocs.summary {
231+
fn write_flame_graph(&self, file: &mut FlameGraphWriter, stack: &'_ mut Vec<&'c str>) {
232+
for (k, v) in &self.frame.allocs.summary {
221233
file.write(
222234
stack.iter().copied().chain(std::iter::once(*k)),
223235
v.bytes as u64,
224236
);
225237
}
226238

227-
for (id, frame) in &self.callees {
228-
stack.push(ids.get(*id));
229-
frame.write_flame_graph(file, stack, ids);
239+
for (id, frame) in self.callees() {
240+
stack.push(id);
241+
frame.write_flame_graph(file, stack);
230242
stack.pop();
231243
}
232244
}
@@ -273,11 +285,17 @@ impl AggregateHeapProfileInfo {
273285
}
274286
}
275287

288+
fn root(&self) -> StackFrameWithContext {
289+
StackFrameWithContext {
290+
frame: &self.root,
291+
strings: &self.strings,
292+
}
293+
}
294+
276295
/// Write this out recursively to a file.
277296
pub(crate) fn gen_flame_graph(&self) -> String {
278297
let mut writer = FlameGraphWriter::new();
279-
self.root
280-
.write_flame_graph(&mut writer, &mut vec![], &self.strings);
298+
self.root().write_flame_graph(&mut writer, &mut vec![]);
281299
writer.finish()
282300
}
283301

0 commit comments

Comments
 (0)