diff --git a/README.md b/README.md index 6d676b5..f9c62da 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ While reading [Crafting Interpreters](https://craftinginterpreters.com/), after - [x] 27 - Classes and Instances - [x] 28 - Method and Initializers - [x] 29 - Superclasses -- [ ] 30 - Optimization +- [x] 30 - Optimization (without NaN boxing/NaN tagging) ## Build & run diff --git a/src/constant.zig b/src/constant.zig index 83d1904..0f390ff 100644 --- a/src/constant.zig +++ b/src/constant.zig @@ -10,14 +10,14 @@ pub const UINT8_COUNT = UINT8_MAX + 1; pub const FRAMES_MAX = 64; pub const STACK_MAX = (FRAMES_MAX * UINT8_MAX); -pub const DEBUG_PRINT_CODE = true; -pub const DEBUG_TRACE_EXECUTION = true; +pub const DEBUG_PRINT_CODE = false; +pub const DEBUG_TRACE_EXECUTION = false; pub const DEBUG_PRINT_INTERNAL_STRINGS = false; pub const DEBUG_PRINT_GLOBALS = false; pub const DEBUG_STRESS_GC = true; pub const DEBUG_LOG_GC = false; -pub const USE_CUSTON_ALLOCATOR = false; +pub const USE_CUSTON_ALLOCATOR = true; pub const GC_HEAP_GROW_FACTOR = 2; diff --git a/src/table.zig b/src/table.zig index 6ad4847..0af9747 100644 --- a/src/table.zig +++ b/src/table.zig @@ -61,7 +61,8 @@ pub const Table = struct { pub fn find_entry(entries: []Entry, key: *Obj.String) ?*Entry { var tombstone: ?*Entry = null; - var index = key.hash % entries.len; + // var index = key.hash % entries.len; + var index = key.hash & (entries.len - 1); while (true) { const entry = &entries[index]; @@ -84,7 +85,8 @@ pub const Table = struct { return entry; } - index = (index + 1) % entries.len; + // index = (index + 1) % entries.len; + index = (index + 1) & (entries.len - 1); } } @@ -179,7 +181,8 @@ pub const Table = struct { return null; } - var index = hash % self.capacity; + // var index = hash % self.capacity; + var index = hash & (self.capacity - 1); while (true) { const entry = &self.entries[index]; if (entry.key == null) { @@ -191,7 +194,8 @@ pub const Table = struct { return entry.key; } - index = (index + 1) % self.capacity; + // index = (index + 1) % self.capacity; + index = (index + 1) & (self.capacity - 1); } } };