From e34cfc7add41e4ae5908210abeeaac6fcacf833e Mon Sep 17 00:00:00 2001 From: imxyy_soope_ Date: Sat, 11 Apr 2026 19:06:26 +0800 Subject: [PATCH] minor changes --- Cargo.lock | 2 -- Cargo.toml | 4 +++ fix-codegen/Cargo.toml | 1 - fix-vm/src/boxing.rs | 2 +- fix-vm/src/lib.rs | 59 +++++++++++++++++++++--------------------- fix-vm/src/value.rs | 7 ++--- fix/Cargo.toml | 1 - fix/src/lib.rs | 3 +++ flake.nix | 8 +----- 9 files changed, 42 insertions(+), 45 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6736184..f7f82b0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -460,7 +460,6 @@ dependencies = [ "mimalloc", "rnix", "rustyline", - "smallvec", "string-interner", "tempfile", "test-log", @@ -485,7 +484,6 @@ dependencies = [ "fix-builtins", "fix-common", "fix-ir", - "gc-arena", "hashbrown 0.16.1", "num_enum", "rnix", diff --git a/Cargo.toml b/Cargo.toml index 8237a58..d4ac7cd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,10 @@ members = [ inherits = "release" debug = true +[profile.lto] +inherits = "release" +lto = true + [workspace.dependencies] bumpalo = { version = "3.20", features = [ "allocator-api2", diff --git a/fix-codegen/Cargo.toml b/fix-codegen/Cargo.toml index 926ba45..24981d0 100644 --- a/fix-codegen/Cargo.toml +++ b/fix-codegen/Cargo.toml @@ -4,7 +4,6 @@ version = "0.1.0" edition = "2024" [dependencies] -gc-arena = { workspace = true } hashbrown = { workspace = true } num_enum = { workspace = true } rnix = { workspace = true } diff --git a/fix-vm/src/boxing.rs b/fix-vm/src/boxing.rs index 1695269..4b8f4ce 100644 --- a/fix-vm/src/boxing.rs +++ b/fix-vm/src/boxing.rs @@ -24,7 +24,7 @@ impl ArrayExt for [T; N] { } } -pub trait RawStore: Sized { +pub(crate) trait RawStore: Sized { fn to_val(self, value: &mut Value); fn from_val(value: &Value) -> Self; } diff --git a/fix-vm/src/lib.rs b/fix-vm/src/lib.rs index 899975c..f8582e0 100644 --- a/fix-vm/src/lib.rs +++ b/fix-vm/src/lib.rs @@ -122,6 +122,7 @@ pub struct Vm { struct GcRoot<'gc> { stack: Vec>, call_stack: Vec>, + call_depth: usize, with_env: Option>>, builtins: Value<'gc>, empty_list: Value<'gc>, @@ -143,16 +144,16 @@ fn init_builtins<'gc>(mc: &Mutation<'gc>, ctx: &mut impl VmContext) -> Value<'gc let consts = [ ( "__currentSystem", - Value::new_gc(Gc::new(mc, NixString::new("x86_64-linux"))), + Value::new_inline(ctx.intern_string("x86_64-linux")), ), ("__langVersion", Value::new_inline(6i32)), ( "__nixVersion", - Value::new_gc(Gc::new(mc, NixString::new("2.24.0"))), + Value::new_inline(ctx.intern_string("2.24.0")), ), ( "__storeDir", - Value::new_gc(Gc::new(mc, NixString::new("/nix/store"))), + Value::new_inline(ctx.intern_string("/nix/store")), ), ( "__nixPath", @@ -190,6 +191,7 @@ impl<'gc> GcRoot<'gc> { GcRoot { stack: Vec::with_capacity(8192), call_stack: Vec::with_capacity(1024), + call_depth: 0, with_env: None, builtins, empty_list: Value::new_gc(Gc::new(mc, List::default())), @@ -404,6 +406,21 @@ impl<'gc> GcRoot<'gc> { } } }}; + (SelectKeyData, $n:expr) => {{ + let mut keys = SmallVec::<[SelectKeyData; 4]>::with_capacity($n as usize); + for _ in 0..$n { + let tag = read!(u8); + let Ok(ty) = AttrKeyType::try_from_primitive(tag) + .map_err(|err| panic!("unknown key tag: {:#04x}", err.number)); + match ty { + AttrKeyType::Static => { + keys.push(SelectKeyData::Static(read!(StringId))) + } + AttrKeyType::Dynamic => keys.push(SelectKeyData::Dynamic), + }; + } + keys + }}; ($type:ty) => { <$type>::from_le_bytes(read_array(ctx.bytecode(), pc)) }; @@ -436,12 +453,12 @@ impl<'gc> GcRoot<'gc> { ThunkState::Evaluated(v) => { self.replace_stack($depth, v.relax()); } + ThunkState::Apply { .. } => todo!("force apply"), ThunkState::Blackhole => { return Action::Done(Err(Error::eval_error( "infinite recursion encountered", ))); } - ThunkState::Apply { .. } => todo!("force apply"), } } }}; @@ -583,6 +600,10 @@ impl<'gc> GcRoot<'gc> { Call => { // force func try_force!(0, inst_start_pc); + if self.call_depth > 10000 { + return Action::Done(Err(Error::eval_error("stack overflow; max-call-depth exceeded"))) + } + self.call_depth += 1; let func = self.pop_stack(); let arg = self.pop_stack(); if let Some(closure) = func.as_gc::() { @@ -649,19 +670,7 @@ impl<'gc> GcRoot<'gc> { let n = read!(u16) as usize; let _span_id = read!(u32); - let mut keys = SmallVec::<[SelectKeyData; 4]>::with_capacity(n); - for _ in 0..n { - let tag = read!(u8); - let Ok(ty) = AttrKeyType::try_from_primitive(tag) - .map_err(|err| panic!("unknown key tag: {:#04x}", err.number)); - match ty { - AttrKeyType::Static => { - keys.push(SelectKeyData::Static(read!(StringId))) - } - AttrKeyType::Dynamic => keys.push(SelectKeyData::Dynamic), - }; - } - + let keys = read!(SelectKeyData, n); let dyn_count = keys .iter() .filter(|k| matches!(k, SelectKeyData::Dynamic)) @@ -736,19 +745,7 @@ impl<'gc> GcRoot<'gc> { let n = read!(u16) as usize; let _span_id = read!(u32); - let mut keys = SmallVec::<[SelectKeyData; 4]>::with_capacity(n); - for _ in 0..n { - let tag = read!(u8); - let Ok(ty) = AttrKeyType::try_from_primitive(tag) - .map_err(|err| panic!("unknown key tag: {:#04x}", err.number)); - match ty { - AttrKeyType::Static => { - keys.push(SelectKeyData::Static(read!(StringId))) - } - AttrKeyType::Dynamic => keys.push(SelectKeyData::Dynamic), - }; - } - + let keys = read!(SelectKeyData, n); let dyn_count = keys .iter() .filter(|k| matches!(k, SelectKeyData::Dynamic)) @@ -1253,6 +1250,8 @@ impl<'gc> GcRoot<'gc> { } } } + } else { + self.call_depth -= 1; } self.current_env = Some(env); self.with_env = with_env; diff --git a/fix-vm/src/value.rs b/fix-vm/src/value.rs index 106f276..9c42197 100644 --- a/fix-vm/src/value.rs +++ b/fix-vm/src/value.rs @@ -22,13 +22,14 @@ mod private { } /// # Safety +/// /// 1. TAG must be unique among all implementors. /// 2. TAG must be within 1..=7 -pub(crate) unsafe trait Storable: private::Cealed { +pub unsafe trait Storable: private::Cealed { const TAG: (bool, u8); } -pub(crate) trait InlineStorable: Storable + RawStore {} -pub(crate) trait GcStorable: Storable {} +pub trait InlineStorable: Storable + RawStore {} +pub trait GcStorable: Storable {} macro_rules! define_value_types { ( diff --git a/fix/Cargo.toml b/fix/Cargo.toml index fff4342..8d2cfea 100644 --- a/fix/Cargo.toml +++ b/fix/Cargo.toml @@ -23,7 +23,6 @@ miette = { version = "7.4", features = ["fancy"] } # Data Structure hashbrown = { workspace = true } -smallvec = { workspace = true } string-interner = { workspace = true } # Memory Management diff --git a/fix/src/lib.rs b/fix/src/lib.rs index cabd6b6..fafcc52 100644 --- a/fix/src/lib.rs +++ b/fix/src/lib.rs @@ -509,6 +509,9 @@ struct OwnedIr { } impl OwnedIr { + /// # Safety + /// + /// `ir` must be allocated from `bump`. unsafe fn new(ir: RawIrRef<'_>, bump: Bump) -> Self { Self { _bump: bump, diff --git a/flake.nix b/flake.nix index 122ef72..c2a0c58 100644 --- a/flake.nix +++ b/flake.nix @@ -26,22 +26,16 @@ "rustfmt" "rust-analyzer" ]) - cargo-outdated cargo-machete + cargo-bloat lldb valgrind hyperfine just samply - jq tokei - nodejs - biome - claude-code - codex - opencode ]; }; }