From 22e76f1a679eca1f5e586cd132ea3800aa54b69b Mon Sep 17 00:00:00 2001 From: Noah <33094578+coolreader18@users.noreply.github.com> Date: Mon, 30 Nov 2020 22:35:02 -0600 Subject: [PATCH] Use a boxed slice for codeobject fields --- bytecode/src/bytecode.rs | 45 +++++++++++++------------- compiler/src/compile.rs | 63 +++++++++++++++++++++++-------------- vm/src/builtins/function.rs | 2 +- 3 files changed, 62 insertions(+), 48 deletions(-) diff --git a/bytecode/src/bytecode.rs b/bytecode/src/bytecode.rs index 85ec43a99..778455581 100644 --- a/bytecode/src/bytecode.rs +++ b/bytecode/src/bytecode.rs @@ -93,8 +93,8 @@ impl ConstantBag for BasicBag { /// a codeobject. Also a module has a codeobject. #[derive(Clone, PartialEq, Serialize, Deserialize)] pub struct CodeObject { - pub instructions: Vec, - pub locations: Vec, + pub instructions: Box<[Instruction]>, + pub locations: Box<[Location]>, pub flags: CodeFlags, pub posonlyarg_count: usize, // Number of positional-only arguments pub arg_count: usize, @@ -103,15 +103,15 @@ pub struct CodeObject { pub first_line_number: usize, pub obj_name: String, // Name of the object that created this code object pub cell2arg: Option>, - pub constants: Vec, + pub constants: Box<[C]>, #[serde(bound( deserialize = "C::Name: serde::Deserialize<'de>", serialize = "C::Name: serde::Serialize" ))] - pub names: Vec, - pub varnames: Vec, - pub cellvars: Vec, - pub freevars: Vec, + pub names: Box<[C::Name]>, + pub varnames: Box<[C::Name]>, + pub cellvars: Box<[C::Name]>, + pub freevars: Box<[C::Name]>, } bitflags! { @@ -129,12 +129,6 @@ bitflags! { } } -impl Default for CodeFlags { - fn default() -> Self { - Self::NEW_LOCALS - } -} - impl CodeFlags { pub const NAME_MAPPING: &'static [(&'static str, CodeFlags)] = &[ ("GENERATOR", CodeFlags::IS_GENERATOR), @@ -542,8 +536,8 @@ impl CodeObject { obj_name: String, ) -> Self { CodeObject { - instructions: Vec::new(), - locations: Vec::new(), + instructions: Box::new([]), + locations: Box::new([]), flags, posonlyarg_count, arg_count, @@ -552,11 +546,11 @@ impl CodeObject { first_line_number, obj_name, cell2arg: None, - constants: Vec::new(), - names: Vec::new(), - varnames: Vec::new(), - cellvars: Vec::new(), - freevars: Vec::new(), + constants: Box::new([]), + names: Box::new([]), + varnames: Box::new([]), + cellvars: Box::new([]), + freevars: Box::new([]), } } @@ -593,7 +587,7 @@ impl CodeObject { pub fn label_targets(&self) -> BTreeSet