Skip to content

Commit 6729aad

Browse files
stepanchegfacebook-github-bot
authored andcommitted
Do not call StmtProfile::set_codemap twice per function call
Summary: Do not call `self.stmt_profile.set_codemap(codemap)` in `Evaluator::set_codemap`. Although `StmtProfile::set_codemap` is cheap, `Evaluator::set_codemap` is called twice per function call. Reviewed By: ndmitchell Differential Revision: D30872419 fbshipit-source-id: ee63849c92c44c30b626ea556e37ba2d7b5c155a
1 parent 97e9ab6 commit 6729aad

File tree

2 files changed

+8
-12
lines changed

2 files changed

+8
-12
lines changed

starlark/src/eval/runtime/evaluator.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ impl<'v, 'a> Evaluator<'v, 'a> {
189189
/// See [`Evaluator::enable_heap_profile`] for details about the types of Starlark profiles.
190190
pub fn enable_stmt_profile(&mut self) {
191191
self.stmt_profile.enable();
192-
self.before_stmt(&|span, eval| eval.stmt_profile.before_stmt(span));
192+
self.before_stmt(&|span, eval| eval.stmt_profile.before_stmt(span, eval.codemap));
193193
}
194194

195195
/// Enable statement profiling, allowing [`Evaluator::write_flame_profile`] to be used.
@@ -304,7 +304,6 @@ impl<'v, 'a> Evaluator<'v, 'a> {
304304
}
305305

306306
pub(crate) fn set_codemap(&mut self, codemap: &'v CodeMap) -> &'v CodeMap {
307-
self.stmt_profile.set_codemap(codemap);
308307
mem::replace(&mut self.codemap, codemap)
309308
}
310309

starlark/src/eval/runtime/stmt_profile.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,17 @@ impl StmtProfileData {
8282
}
8383
}
8484

85-
fn before_stmt(&mut self, span: Span) {
85+
fn before_stmt(&mut self, span: Span, codemap: &CodeMap) {
8686
let now = Instant::now();
8787
self.add_last(now);
88+
if self.last_span.0 != FileId::new(codemap) {
89+
self.add_codemap(codemap);
90+
}
8891
self.last_span = (self.next_file, span);
8992
self.last_start = now;
9093
}
9194

92-
fn set_codemap(&mut self, codemap: &CodeMap) {
95+
fn add_codemap(&mut self, codemap: &CodeMap) {
9396
let id = FileId::new(codemap);
9497
self.next_file = id;
9598
match self.files.entry(id) {
@@ -179,15 +182,9 @@ impl StmtProfile {
179182
self.0 = Some(box StmtProfileData::new())
180183
}
181184

182-
pub fn before_stmt(&mut self, span: Span) {
183-
if let Some(box data) = &mut self.0 {
184-
data.before_stmt(span)
185-
}
186-
}
187-
188-
pub fn set_codemap(&mut self, codemap: &CodeMap) {
185+
pub fn before_stmt(&mut self, span: Span, codemap: &CodeMap) {
189186
if let Some(box data) = &mut self.0 {
190-
data.set_codemap(codemap)
187+
data.before_stmt(span, codemap)
191188
}
192189
}
193190

0 commit comments

Comments
 (0)