Skip to content

Commit f817ab8

Browse files
authored
Bytecode oparg optimization (#7032)
* Generate optimized oparg enums * No need to match on 255 * Remove `num_enum` crate from `compiler-core` * Update `Cargo.lock` * macro fmt * Rename macro vars * Match without `,` * Support alternative values * Fix alternatives * Improve docs * Add const assert * Don't use `as u32` * Make only ComparisonOperator unoptimized * Fix test * cleanup * All opargs are optimized * Remove comment
1 parent 3d283bb commit f817ab8

File tree

6 files changed

+362
-354
lines changed

6 files changed

+362
-354
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/codegen/src/compile.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,7 +1165,7 @@ impl Compiler {
11651165
arg: OpArgMarker::marker(),
11661166
}
11671167
.into(),
1168-
arg: OpArg(bytecode::ResumeType::AtFuncStart as u32),
1168+
arg: OpArg(u32::from(bytecode::ResumeType::AtFuncStart)),
11691169
target: BlockIdx::NULL,
11701170
location,
11711171
end_location,
@@ -1267,16 +1267,19 @@ impl Compiler {
12671267
/// Emit format parameter validation for annotation scope
12681268
/// if format > VALUE_WITH_FAKE_GLOBALS (2): raise NotImplementedError
12691269
fn emit_format_validation(&mut self) -> CompileResult<()> {
1270-
use bytecode::ComparisonOperator::Greater;
1271-
12721270
// Load format parameter (first local variable, index 0)
12731271
emit!(self, Instruction::LoadFast(0));
12741272

12751273
// Load VALUE_WITH_FAKE_GLOBALS constant (2)
12761274
self.emit_load_const(ConstantData::Integer { value: 2.into() });
12771275

12781276
// Compare: format > 2
1279-
emit!(self, Instruction::CompareOp { op: Greater });
1277+
emit!(
1278+
self,
1279+
Instruction::CompareOp {
1280+
op: ComparisonOperator::Greater
1281+
}
1282+
);
12801283

12811284
// Jump to body if format <= 2 (comparison is false)
12821285
let body_block = self.new_block();
@@ -6545,9 +6548,9 @@ impl Compiler {
65456548
self,
65466549
Instruction::Resume {
65476550
arg: if is_await {
6548-
bytecode::ResumeType::AfterAwait as u32
6551+
u32::from(bytecode::ResumeType::AfterAwait)
65496552
} else {
6550-
bytecode::ResumeType::AfterYieldFrom as u32
6553+
u32::from(bytecode::ResumeType::AfterYieldFrom)
65516554
}
65526555
}
65536556
);
@@ -6702,7 +6705,7 @@ impl Compiler {
67026705
emit!(
67036706
self,
67046707
Instruction::Resume {
6705-
arg: bytecode::ResumeType::AfterYield as u32
6708+
arg: u32::from(bytecode::ResumeType::AfterYield)
67066709
}
67076710
);
67086711
}
@@ -6924,7 +6927,7 @@ impl Compiler {
69246927
emit!(
69256928
compiler,
69266929
Instruction::Resume {
6927-
arg: bytecode::ResumeType::AfterYield as u32
6930+
arg: u32::from(bytecode::ResumeType::AfterYield)
69286931
}
69296932
);
69306933
emit!(compiler, Instruction::PopTop);
@@ -8412,7 +8415,7 @@ impl Compiler {
84128415

84138416
// Emit BUILD_INTERPOLATION
84148417
// oparg encoding: (conversion << 2) | has_format_spec
8415-
let oparg = (conversion << 2) | (has_format_spec as u32);
8418+
let oparg = (conversion << 2) | u32::from(has_format_spec);
84168419
emit!(self, Instruction::BuildInterpolation { oparg });
84178420

84188421
*interp_count += 1;

crates/compiler-core/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ bitflags = { workspace = true }
1717
itertools = { workspace = true }
1818
malachite-bigint = { workspace = true }
1919
num-complex = { workspace = true }
20-
num_enum = { workspace = true }
2120

2221
lz4_flex = "0.12"
2322

crates/compiler-core/src/bytecode.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,8 @@ bitflags! {
304304
}
305305
}
306306

307-
#[derive(Copy, Clone)]
308307
#[repr(C)]
308+
#[derive(Copy, Clone, Debug)]
309309
pub struct CodeUnit {
310310
pub op: Instruction,
311311
pub arg: OpArgByte,
@@ -330,7 +330,7 @@ impl TryFrom<&[u8]> for CodeUnit {
330330
}
331331
}
332332

333-
#[derive(Clone)]
333+
#[derive(Clone, Debug)]
334334
pub struct CodeUnits(Box<[CodeUnit]>);
335335

336336
impl TryFrom<&[u8]> for CodeUnits {

0 commit comments

Comments
 (0)