Skip to content

Commit

Permalink
Merge pull request #4667 from laytan/use-map-entry-in-strings-intern
Browse files Browse the repository at this point in the history
strings: use map_entry in Intern datastructure
  • Loading branch information
gingerBill authored Jan 9, 2025
2 parents 2620721 + 16e3abf commit cabc76d
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions core/strings/intern.odin
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ intern_get_cstring :: proc(m: ^Intern, text: string) -> (str: cstring, err: runt
entry := _intern_get_entry(m, text) or_return
return cstring(&entry.str[0]), nil
}

/*
Internal function to lookup whether the text string exists in the map, returns the entry
Sets and allocates the entry if it wasn't set yet
Expand All @@ -104,13 +105,15 @@ Returns:
- err: An allocator error if one occured, `nil` otherwise
*/
_intern_get_entry :: proc(m: ^Intern, text: string) -> (new_entry: ^Intern_Entry, err: runtime.Allocator_Error) #no_bounds_check {
if prev, ok := m.entries[text]; ok {
return prev, nil
}
if m.allocator.procedure == nil {
m.allocator = context.allocator
}

key_ptr, val_ptr, inserted := map_entry(&m.entries, text) or_return
if !inserted {
return val_ptr^, nil
}

entry_size := int(offset_of(Intern_Entry, str)) + len(text) + 1
bytes := runtime.mem_alloc(entry_size, align_of(Intern_Entry), m.allocator) or_return
new_entry = (^Intern_Entry)(raw_data(bytes))
Expand All @@ -120,6 +123,9 @@ _intern_get_entry :: proc(m: ^Intern, text: string) -> (new_entry: ^Intern_Entry
new_entry.str[new_entry.len] = 0

key := string(new_entry.str[:new_entry.len])
m.entries[key] = new_entry
return new_entry, nil

key_ptr^ = key
val_ptr^ = new_entry

return
}

0 comments on commit cabc76d

Please sign in to comment.