Skip to content

Commit 02f747d

Browse files
committed
perf(python): a module variable holding an instance is stored as the instance
The global slot holds the pointer, as a field does, instead of a box opened at every read.
1 parent f6257b2 commit 02f747d

2 files changed

Lines changed: 5 additions & 6 deletions

File tree

crates/zyntax_python/src/lib.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -582,10 +582,7 @@ pub fn parse_program_with(
582582
Span::new(0, 0),
583583
));
584584
for (name, ty) in &inferred.globals {
585-
let stored = match ty {
586-
types::Ty::Int | types::Ty::Float | types::Ty::Bool | types::Ty::Str => *ty,
587-
_ => types::Ty::Object,
588-
};
585+
let stored = lower::Lowerer::storage(*ty);
589586
declarations.push(TypedNode::new(
590587
TypedDeclaration::Variable(TypedVariable {
591588
name: intern(name),

crates/zyntax_python/src/lower.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,9 +1282,11 @@ impl<'m> Lowerer<'m> {
12821282

12831283
/// A global holds its own scalar type; anything else is stored boxed,
12841284
/// so a list global is one heap header shared by every reader.
1285-
fn storage(ty: Ty) -> Ty {
1285+
/// How a module variable of type `ty` is stored: primitives,
1286+
/// strings and instances as themselves, the rest boxed.
1287+
pub(crate) fn storage(ty: Ty) -> Ty {
12861288
match ty {
1287-
Ty::Int | Ty::Float | Ty::Bool | Ty::Str => ty,
1289+
Ty::Int | Ty::Float | Ty::Bool | Ty::Str | Ty::Class(_) => ty,
12881290
_ => Ty::Object,
12891291
}
12901292
}

0 commit comments

Comments
 (0)