diff options
| author | Daniel Gavin <danielgavin5@hotmail.com> | 2022-03-10 15:16:58 +0100 |
|---|---|---|
| committer | Daniel Gavin <danielgavin5@hotmail.com> | 2022-03-10 15:16:58 +0100 |
| commit | 968e428c217e963b82e3c8fd39b9a9721e00f7d6 (patch) | |
| tree | 83c80c6d669b448c482570479d2fe721ddc34f1a /src/index | |
| parent | 9f1e92e8316ff0db92a3e72d0a21d1e7311171f3 (diff) | |
Use murmur hash and bucket a map in each package.
Diffstat (limited to 'src/index')
| -rw-r--r-- | src/index/collector.odin | 33 | ||||
| -rw-r--r-- | src/index/memory_index.odin | 49 | ||||
| -rw-r--r-- | src/index/symbol.odin | 6 |
3 files changed, 50 insertions, 38 deletions
diff --git a/src/index/collector.odin b/src/index/collector.odin index c527873..fc8d857 100644 --- a/src/index/collector.odin +++ b/src/index/collector.odin @@ -15,7 +15,7 @@ import "shared:common" SymbolCollection :: struct { allocator: mem.Allocator, config: ^common.Config, - symbols: map[uint]Symbol, + packages: map[uint]map[uint]Symbol, unique_strings: map[string]string, //store all our strings as unique strings and reference them to save memory. } @@ -41,21 +41,27 @@ make_symbol_collection :: proc(allocator := context.allocator, config: ^common.C return SymbolCollection { allocator = allocator, config = config, - symbols = make(map[uint]Symbol, 16, allocator), + packages = make(map[uint]map[uint]Symbol, 16, allocator), unique_strings = make(map[string]string, 16, allocator), } } delete_symbol_collection :: proc(collection: SymbolCollection) { - for k, v in collection.symbols { - free_symbol(v, collection.allocator) + for k, v in collection.packages { + for k2, v2 in v { + free_symbol(v2, collection.allocator) + } } for k, v in collection.unique_strings { delete(v, collection.allocator) } - delete(collection.symbols) + for k, v in collection.packages { + delete(v) + } + + delete(collection.packages) delete(collection.unique_strings) } @@ -383,15 +389,22 @@ collect_symbols :: proc(collection: ^SymbolCollection, file: ast.File, uri: stri symbol.uri = get_index_unique_string(collection, uri) } - cat := strings.concatenate({symbol.pkg, name}, context.temp_allocator) + package_id := get_id(symbol.pkg) + name_id := get_id(name) - id := get_symbol_id(cat) + pkg: ^map[uint]Symbol + ok: bool - if v, ok := collection.symbols[id]; !ok || v.name == "" { - collection.symbols[id] = symbol + if pkg, ok = &collection.packages[package_id]; !ok { + collection.packages[package_id] = make(map[uint]Symbol, 100, collection.allocator) + pkg = &collection.packages[package_id] + } + + if v, ok := pkg[name_id]; !ok || v.name == "" { + pkg[name_id] = symbol } else { free_symbol(symbol, collection.allocator) - } + } } return .None diff --git a/src/index/memory_index.odin b/src/index/memory_index.odin index 5146bf8..4afa334 100644 --- a/src/index/memory_index.odin +++ b/src/index/memory_index.odin @@ -25,8 +25,17 @@ make_memory_index :: proc(collection: SymbolCollection) -> MemoryIndex { } memory_index_lookup :: proc(index: ^MemoryIndex, name: string, pkg: string) -> (Symbol, bool) { - id := get_symbol_id(strings.concatenate({pkg, name}, context.temp_allocator)) - return index.collection.symbols[id] + package_id := get_id(pkg) + name_id := get_id(name) + + pkg: ^map[uint]Symbol + ok: bool + + if pkg, ok = &index.collection.packages[package_id]; ok { + return pkg[name_id] + } + + return {}, false } memory_index_fuzzy_search :: proc(index: ^MemoryIndex, name: string, pkgs: []string) -> ([]FuzzyResult, bool) { @@ -36,20 +45,21 @@ memory_index_fuzzy_search :: proc(index: ^MemoryIndex, name: string, pkgs: []str top := 20 - for _, symbol in index.collection.symbols { - - if !exists_in_scope(symbol.pkg, pkgs) { - continue - } - - if score, ok := common.fuzzy_match(fuzzy_matcher, symbol.name); ok == 1 { - result := FuzzyResult { - symbol = symbol, - score = score, + for pkg in pkgs { + package_id := get_id(pkg) + + if pkg, ok := index.collection.packages[package_id]; ok { + for _, symbol in pkg { + if score, ok := common.fuzzy_match(fuzzy_matcher, symbol.name); ok == 1 { + result := FuzzyResult { + symbol = symbol, + score = score, + } + + append(&symbols, result) + } } - - append(&symbols, result) - } + } } sort.sort(fuzzy_sort_interface(&symbols)) @@ -61,12 +71,3 @@ memory_index_fuzzy_search :: proc(index: ^MemoryIndex, name: string, pkgs: []str } } -exists_in_scope :: proc(symbol_scope: string, scope: []string) -> bool { - for s in scope { - if strings.compare(symbol_scope, s) == 0 { - return true - } - } - - return false -} diff --git a/src/index/symbol.odin b/src/index/symbol.odin index c00c08e..4f80032 100644 --- a/src/index/symbol.odin +++ b/src/index/symbol.odin @@ -184,8 +184,6 @@ free_symbol :: proc(symbol: Symbol, allocator: mem.Allocator) { } } -get_symbol_id :: proc(str: string) -> uint { - ret := common.sha1_hash(transmute([]byte)str) - r := cast(^uint)slice.first_ptr(ret[:]) - return r^ +get_id :: proc(str: string) -> uint { + return cast(uint)hash.murmur64(transmute([]byte)str) } |