Skip to content

Commit 1cb8325

Browse files
committed
Use BUILD_MAP 0 + MAP_ADD for large dicts (>= 16 pairs)
Match CPython's compiler behavior: dicts with 16+ key-value pairs use BUILD_MAP 0 followed by MAP_ADD for each pair, instead of pushing all keys/values on the stack and calling BUILD_MAP N.
1 parent 4f8450d commit 1cb8325

File tree

1 file changed

+2
-7
lines changed

1 file changed

+2
-7
lines changed

crates/codegen/src/compile.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -723,9 +723,7 @@ impl Compiler {
723723
// Function-local imports use method mode (scope is Local)
724724
return !matches!(
725725
current.typ,
726-
CompilerScope::Function
727-
| CompilerScope::AsyncFunction
728-
| CompilerScope::Lambda
726+
CompilerScope::Function | CompilerScope::AsyncFunction | CompilerScope::Lambda
729727
);
730728
}
731729
if sym.scope == SymbolScope::Local {
@@ -7046,10 +7044,7 @@ impl Compiler {
70467044
let has_unpacking = items.iter().any(|item| item.key.is_none());
70477045

70487046
if !has_unpacking {
7049-
// STACK_USE_GUIDELINE: for large dicts (16+ pairs), use
7050-
// BUILD_MAP 0 + MAP_ADD to avoid excessive stack usage
7051-
let big = items.len() * 2 > 30; // ~15 pairs threshold
7052-
if big {
7047+
if items.len() >= 16 {
70537048
emit!(self, Instruction::BuildMap { count: 0 });
70547049
for item in items {
70557050
self.compile_expression(item.key.as_ref().unwrap())?;

0 commit comments

Comments
 (0)