Skip to content

Commit 97029af

Browse files
committed
Fast locals part 1
1 parent 479610f commit 97029af

File tree

14 files changed

+715
-535
lines changed

14 files changed

+715
-535
lines changed

bytecode/src/bytecode.rs

Lines changed: 79 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ pub struct CodeObject<C: Constant = ConstantData> {
108108
serialize = "C::Name: serde::Serialize"
109109
))]
110110
pub names: Vec<C::Name>,
111+
pub varnames: Vec<C::Name>,
112+
pub cellvars: Vec<C::Name>,
113+
pub freevars: Vec<C::Name>,
111114
}
112115

113116
bitflags! {
@@ -121,6 +124,7 @@ bitflags! {
121124
const IS_COROUTINE = 0x20;
122125
const HAS_VARARGS = 0x40;
123126
const HAS_VARKEYWORDS = 0x80;
127+
const IS_OPTIMIZED = 0x0100;
124128
}
125129
}
126130

@@ -154,22 +158,6 @@ impl fmt::Display for Label {
154158
}
155159
}
156160

157-
#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
158-
/// An indication where the name must be accessed.
159-
pub enum NameScope {
160-
/// The name will be in the local scope.
161-
Local,
162-
163-
/// The name will be located in scope surrounding the current scope.
164-
NonLocal,
165-
166-
/// The name will be in global scope.
167-
Global,
168-
169-
/// The name will be located in any scope between the current scope and the top scope.
170-
Free,
171-
}
172-
173161
/// Transforms a value prior to formatting it.
174162
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
175163
pub enum ConversionFlag {
@@ -195,17 +183,20 @@ pub enum Instruction {
195183
ImportFrom {
196184
idx: NameIdx,
197185
},
198-
LoadName {
199-
idx: NameIdx,
200-
scope: NameScope,
201-
},
202-
StoreName {
203-
idx: NameIdx,
204-
scope: NameScope,
205-
},
206-
DeleteName {
207-
idx: NameIdx,
208-
},
186+
LoadFast(NameIdx),
187+
LoadLocal(NameIdx),
188+
LoadGlobal(NameIdx),
189+
LoadDeref(NameIdx),
190+
LoadClassDeref(NameIdx),
191+
StoreFast(NameIdx),
192+
StoreLocal(NameIdx),
193+
StoreGlobal(NameIdx),
194+
StoreDeref(NameIdx),
195+
DeleteFast(NameIdx),
196+
DeleteLocal(NameIdx),
197+
DeleteGlobal(NameIdx),
198+
DeleteDeref(NameIdx),
199+
LoadClosure(NameIdx),
209200
Subscript,
210201
StoreSubscript,
211202
DeleteSubscript,
@@ -561,6 +552,9 @@ impl<C: Constant> CodeObject<C> {
561552
obj_name,
562553
constants: Vec::new(),
563554
names: Vec::new(),
555+
varnames: Vec::new(),
556+
cellvars: Vec::new(),
557+
freevars: Vec::new(),
564558
}
565559
}
566560

@@ -569,19 +563,19 @@ impl<C: Constant> CodeObject<C> {
569563
let nargs = self.arg_count;
570564
let nkwargs = self.kwonlyarg_count;
571565
let mut varargspos = nargs + nkwargs;
572-
let posonlyargs = &self.names[..self.posonlyarg_count];
573-
let args = &self.names[..nargs];
574-
let kwonlyargs = &self.names[nargs..varargspos];
566+
let posonlyargs = &self.varnames[..self.posonlyarg_count];
567+
let args = &self.varnames[..nargs];
568+
let kwonlyargs = &self.varnames[nargs..varargspos];
575569

576570
let vararg = if self.flags.contains(CodeFlags::HAS_VARARGS) {
577-
let vararg = &self.names[varargspos];
571+
let vararg = &self.varnames[varargspos];
578572
varargspos += 1;
579573
Some(vararg)
580574
} else {
581575
None
582576
};
583577
let varkwarg = if self.flags.contains(CodeFlags::HAS_VARKEYWORDS) {
584-
Some(&self.names[varargspos])
578+
Some(&self.varnames[varargspos])
585579
} else {
586580
None
587581
};
@@ -617,8 +611,10 @@ impl<C: Constant> CodeObject<C> {
617611
}
618612

619613
#[rustfmt::skip]
620-
Import { .. } | ImportStar | ImportFrom { .. } | LoadName { .. } | StoreName { .. }
621-
| DeleteName { .. } | Subscript | StoreSubscript | DeleteSubscript
614+
Import { .. } | ImportStar | ImportFrom { .. } | LoadFast(_) | LoadLocal(_)
615+
| LoadGlobal(_) | LoadDeref(_) | LoadClassDeref(_) | StoreFast(_) | StoreLocal(_)
616+
| StoreGlobal(_) | StoreDeref(_) | DeleteFast(_) | DeleteLocal(_) | DeleteGlobal(_)
617+
| DeleteDeref(_) | LoadClosure(_) | Subscript | StoreSubscript | DeleteSubscript
622618
| StoreAttr { .. } | DeleteAttr { .. } | LoadConst { .. } | UnaryOperation { .. }
623619
| BinaryOperation { .. } | LoadAttr { .. } | CompareOperation { .. } | Pop
624620
| Rotate { .. } | Duplicate | GetIter | Continue | Break | MakeFunction
@@ -652,7 +648,16 @@ impl<C: Constant> CodeObject<C> {
652648
write!(f, " ")?;
653649
}
654650
write!(f, "{} {:5} ", arrow, offset)?;
655-
instruction.fmt_dis(f, &self.constants, &self.names, expand_codeobjects, level)?;
651+
instruction.fmt_dis(
652+
f,
653+
&self.constants,
654+
&self.names,
655+
&self.varnames,
656+
&self.cellvars,
657+
&self.freevars,
658+
expand_codeobjects,
659+
level,
660+
)?;
656661
}
657662
Ok(())
658663
}
@@ -668,17 +673,22 @@ impl<C: Constant> CodeObject<C> {
668673
}
669674

670675
pub fn map_bag<Bag: ConstantBag>(self, bag: &Bag) -> CodeObject<Bag::Constant> {
676+
let map_names = |names: Vec<C::Name>| {
677+
names
678+
.into_iter()
679+
.map(|x| bag.make_name_ref(x.as_ref()))
680+
.collect::<Vec<_>>()
681+
};
671682
CodeObject {
672683
constants: self
673684
.constants
674685
.into_iter()
675686
.map(|x| x.map_constant(bag))
676687
.collect(),
677-
names: self
678-
.names
679-
.into_iter()
680-
.map(|x| bag.make_name_ref(x.as_ref()))
681-
.collect(),
688+
names: map_names(self.names),
689+
varnames: map_names(self.varnames),
690+
cellvars: map_names(self.cellvars),
691+
freevars: map_names(self.freevars),
682692

683693
instructions: self.instructions,
684694
locations: self.locations,
@@ -693,17 +703,22 @@ impl<C: Constant> CodeObject<C> {
693703
}
694704

695705
pub fn map_clone_bag<Bag: ConstantBag>(&self, bag: &Bag) -> CodeObject<Bag::Constant> {
706+
let map_names = |names: &[C::Name]| {
707+
names
708+
.iter()
709+
.map(|x| bag.make_name_ref(x.as_ref()))
710+
.collect()
711+
};
696712
CodeObject {
697713
constants: self
698714
.constants
699715
.iter()
700716
.map(|x| bag.make_constant_borrowed(x.borrow_constant()))
701717
.collect(),
702-
names: self
703-
.names
704-
.iter()
705-
.map(|x| bag.make_name_ref(x.as_ref()))
706-
.collect(),
718+
names: map_names(&self.names),
719+
varnames: map_names(&self.varnames),
720+
cellvars: map_names(&self.cellvars),
721+
freevars: map_names(&self.freevars),
707722

708723
instructions: self.instructions.clone(),
709724
locations: self.locations.clone(),
@@ -755,6 +770,9 @@ impl Instruction {
755770
f: &mut fmt::Formatter,
756771
constants: &[C],
757772
names: &[C::Name],
773+
varnames: &[C::Name],
774+
cellvars: &[C::Name],
775+
freevars: &[C::Name],
758776
expand_codeobjects: bool,
759777
level: usize,
760778
) -> fmt::Result {
@@ -780,6 +798,8 @@ impl Instruction {
780798
};
781799
}
782800

801+
let cellname = |i: usize| cellvars.get(i).unwrap_or_else(|| &freevars[i]).as_ref();
802+
783803
match self {
784804
Import {
785805
name_idx,
@@ -799,9 +819,20 @@ impl Instruction {
799819
),
800820
ImportStar => w!(ImportStar),
801821
ImportFrom { idx } => w!(ImportFrom, names[*idx].as_ref()),
802-
LoadName { idx, scope } => w!(LoadName, names[*idx].as_ref(), format!("{:?}", scope)),
803-
StoreName { idx, scope } => w!(StoreName, names[*idx].as_ref(), format!("{:?}", scope)),
804-
DeleteName { idx } => w!(DeleteName, names[*idx].as_ref()),
822+
LoadFast(idx) => w!(LoadFast, varnames[*idx].as_ref()),
823+
LoadLocal(idx) => w!(LoadLocal, names[*idx].as_ref()),
824+
LoadGlobal(idx) => w!(LoadGlobal, names[*idx].as_ref()),
825+
LoadDeref(idx) => w!(LoadDeref, cellname(*idx)),
826+
LoadClassDeref(idx) => w!(LoadClassDeref, cellname(*idx)),
827+
StoreFast(idx) => w!(StoreFast, varnames[*idx].as_ref()),
828+
StoreLocal(idx) => w!(StoreLocal, names[*idx].as_ref()),
829+
StoreGlobal(idx) => w!(StoreGlobal, names[*idx].as_ref()),
830+
StoreDeref(idx) => w!(StoreDeref, cellname(*idx)),
831+
DeleteFast(idx) => w!(DeleteFast, varnames[*idx].as_ref()),
832+
DeleteLocal(idx) => w!(DeleteLocal, names[*idx].as_ref()),
833+
DeleteGlobal(idx) => w!(DeleteGlobal, names[*idx].as_ref()),
834+
DeleteDeref(idx) => w!(DeleteDeref, cellname(*idx)),
835+
LoadClosure(i) => w!(LoadClosure, cellname(*i)),
805836
Subscript => w!(Subscript),
806837
StoreSubscript => w!(StoreSubscript),
807838
DeleteSubscript => w!(DeleteSubscript),

0 commit comments

Comments
 (0)