Skip to content

Commit 28a78b5

Browse files
samwgoldmanmeta-codesync[bot]
authored andcommitted
Check Calculation cell in lookup_answer before Arc dupes
Summary: When lookup_answer finds that a module has Answers but not Solutions, check the Calculation cell directly while still holding the read lock, before duping the Answers/Load/Bindings Arcs and constructing a TransactionHandle for solve_exported_key. The vast majority of solve_exported_key calls hit the Calculation cell fast path, yet each call still paid for 3+ Arc refcount increments, a SmallMap lookup for get_stdlib, and TransactionHandle construction. By checking the Calculation cell first under the existing read lock, we skip all of that overhead for cached answers. Reviewed By: yangdanny97 Differential Revision: D93209983 fbshipit-source-id: 841216c657ba8bb30305e7b3f065ee857e77c714
1 parent b0b0632 commit 28a78b5

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

pyrefly/lib/state/state.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1478,12 +1478,21 @@ impl<'a> Transaction<'a> {
14781478
{
14791479
return solutions.get_hashed_opt(key).duped();
14801480
} else if let Some(answers) = &lock.steps.answers {
1481+
// Fast path: check if the answer is already computed in the
1482+
// Calculation cell. This avoids duping Arcs and constructing
1483+
// a TransactionHandle when the value is cached.
1484+
if let Some(idx) = answers.0.key_to_idx_hashed_opt(key)
1485+
&& let Some(v) = answers.1.get_idx(idx)
1486+
{
1487+
return Some(v);
1488+
}
1489+
// Slow path: need full solve_exported_key for computation.
14811490
let load = lock.steps.load.dupe().unwrap();
14821491
let answers = answers.dupe();
14831492
drop(lock);
14841493
let stdlib = self.get_stdlib(&module_data.handle);
14851494
let lookup = self.lookup(module_data);
1486-
return answers.1.solve_exported_key(
1495+
let result = answers.1.solve_exported_key(
14871496
&lookup,
14881497
&lookup,
14891498
&answers.0,
@@ -1493,6 +1502,7 @@ impl<'a> Transaction<'a> {
14931502
key,
14941503
thread_state,
14951504
);
1505+
return result;
14961506
}
14971507
}
14981508
drop(lock);

0 commit comments

Comments
 (0)