implementing table optimization (ch30)

This commit is contained in:
Patrick MARIE 2024-09-01 13:51:34 +02:00
parent 031ca59a07
commit 7e216dd382
3 changed files with 12 additions and 8 deletions

View File

@ -20,7 +20,7 @@ While reading [Crafting Interpreters](https://craftinginterpreters.com/), after
- [x] 27 - Classes and Instances - [x] 27 - Classes and Instances
- [x] 28 - Method and Initializers - [x] 28 - Method and Initializers
- [x] 29 - Superclasses - [x] 29 - Superclasses
- [ ] 30 - Optimization - [x] 30 - Optimization (without NaN boxing/NaN tagging)
## Build & run ## Build & run

View File

@ -10,14 +10,14 @@ pub const UINT8_COUNT = UINT8_MAX + 1;
pub const FRAMES_MAX = 64; pub const FRAMES_MAX = 64;
pub const STACK_MAX = (FRAMES_MAX * UINT8_MAX); pub const STACK_MAX = (FRAMES_MAX * UINT8_MAX);
pub const DEBUG_PRINT_CODE = true; pub const DEBUG_PRINT_CODE = false;
pub const DEBUG_TRACE_EXECUTION = true; pub const DEBUG_TRACE_EXECUTION = false;
pub const DEBUG_PRINT_INTERNAL_STRINGS = false; pub const DEBUG_PRINT_INTERNAL_STRINGS = false;
pub const DEBUG_PRINT_GLOBALS = false; pub const DEBUG_PRINT_GLOBALS = false;
pub const DEBUG_STRESS_GC = true; pub const DEBUG_STRESS_GC = true;
pub const DEBUG_LOG_GC = false; 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; pub const GC_HEAP_GROW_FACTOR = 2;

View File

@ -61,7 +61,8 @@ pub const Table = struct {
pub fn find_entry(entries: []Entry, key: *Obj.String) ?*Entry { pub fn find_entry(entries: []Entry, key: *Obj.String) ?*Entry {
var tombstone: ?*Entry = null; 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) { while (true) {
const entry = &entries[index]; const entry = &entries[index];
@ -84,7 +85,8 @@ pub const Table = struct {
return entry; 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; return null;
} }
var index = hash % self.capacity; // var index = hash % self.capacity;
var index = hash & (self.capacity - 1);
while (true) { while (true) {
const entry = &self.entries[index]; const entry = &self.entries[index];
if (entry.key == null) { if (entry.key == null) {
@ -191,7 +194,8 @@ pub const Table = struct {
return entry.key; return entry.key;
} }
index = (index + 1) % self.capacity; // index = (index + 1) % self.capacity;
index = (index + 1) & (self.capacity - 1);
} }
} }
}; };