Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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.
  • Loading branch information
youknowone committed Mar 25, 2026
commit c7f505d77877131577ab40a6f9f56254f321374d
9 changes: 2 additions & 7 deletions crates/codegen/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -723,9 +723,7 @@ impl Compiler {
// Function-local imports use method mode (scope is Local)
return !matches!(
current.typ,
CompilerScope::Function
| CompilerScope::AsyncFunction
| CompilerScope::Lambda
CompilerScope::Function | CompilerScope::AsyncFunction | CompilerScope::Lambda
);
}
if sym.scope == SymbolScope::Local {
Expand Down Expand Up @@ -7046,10 +7044,7 @@ impl Compiler {
let has_unpacking = items.iter().any(|item| item.key.is_none());

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