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
Fold constant list/set/tuple literals in compiler
When all elements of a list/set/tuple literal are constants and
there are 3+ elements, fold them into a single constant:
- list: BUILD_LIST 0 + LOAD_CONST (tuple) + LIST_EXTEND 1
- set:  BUILD_SET 0  + LOAD_CONST (tuple) + SET_UPDATE 1
- tuple: LOAD_CONST (tuple)

This matches CPython's compiler optimization and fixes the most
common bytecode difference (92/200 sampled files).

Also add bytecode comparison scripts (dis_dump.py, compare_bytecode.py)
for systematic parity tracking.
  • Loading branch information
youknowone committed Mar 25, 2026
commit b2eaa7126d0106457eebc82ed7395615f448575c
72 changes: 72 additions & 0 deletions crates/codegen/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,28 @@ impl Compiler {
_ => n > 4,
};

// Fold all-constant collections (>= 3 elements) regardless of size
if !seen_star && pushed == 0 && n >= 3 && elts.iter().all(|e| e.is_constant()) {
if let Some(folded) = self.try_fold_constant_collection(elts)? {
match collection_type {
CollectionType::Tuple => {
self.emit_load_const(folded);
}
CollectionType::List => {
emit!(self, Instruction::BuildList { count: 0 });
self.emit_load_const(folded);
emit!(self, Instruction::ListExtend { i: 1 });
}
CollectionType::Set => {
emit!(self, Instruction::BuildSet { count: 0 });
self.emit_load_const(folded);
emit!(self, Instruction::SetUpdate { i: 1 });
}
}
return Ok(());
}
}

// If no stars and not too big, compile all elements and build once
if !seen_star && !big {
for elt in elts {
Expand Down Expand Up @@ -8554,6 +8576,56 @@ impl Compiler {
info.metadata.consts.insert_full(constant).0.to_u32().into()
}

/// Try to fold a collection of constant expressions into a single ConstantData::Tuple.
/// Returns None if any element cannot be folded.
fn try_fold_constant_collection(
&mut self,
elts: &[ast::Expr],
) -> CompileResult<Option<ConstantData>> {
let mut constants = Vec::with_capacity(elts.len());
for elt in elts {
match elt {
ast::Expr::NumberLiteral(num) => match &num.value {
ast::Number::Int(int) => {
let value = ruff_int_to_bigint(int).map_err(|e| self.error(e))?;
constants.push(ConstantData::Integer { value });
}
ast::Number::Float(f) => {
constants.push(ConstantData::Float { value: *f });
}
ast::Number::Complex { real, imag } => {
constants.push(ConstantData::Complex {
value: Complex::new(*real, *imag),
});
}
},
ast::Expr::StringLiteral(s) => {
constants.push(ConstantData::Str {
value: s.value.to_string().into(),
});
}
ast::Expr::BytesLiteral(b) => {
constants.push(ConstantData::Bytes {
value: b.value.bytes().collect(),
});
}
ast::Expr::BooleanLiteral(b) => {
constants.push(ConstantData::Boolean { value: b.value });
}
ast::Expr::NoneLiteral(_) => {
constants.push(ConstantData::None);
}
ast::Expr::EllipsisLiteral(_) => {
constants.push(ConstantData::Ellipsis);
}
_ => return Ok(None),
}
}
Ok(Some(ConstantData::Tuple {
elements: constants,
}))
}

fn emit_load_const(&mut self, constant: ConstantData) {
let idx = self.arg_constant(constant);
self.emit_arg(idx, |consti| Instruction::LoadConst { consti })
Expand Down