diff --git a/src/Util/Identifier.luau b/src/Util/Identifier.luau index abd4807..f9a1ade 100644 --- a/src/Util/Identifier.luau +++ b/src/Util/Identifier.luau @@ -1,30 +1,25 @@ --!optimize 2 ---!native --@EternityDev -local BITS: number = 16 +local BITS: number = 8 local MAX_VALUE: number = bit32.lshift(1, BITS) - 1 -local cache: { [string]: number } = {} -local hash: number = game.PlaceVersion + 5381 -local count: number = 0 - -return function(name: string): number - local cached = cache[name] - if cached then - return cached - end - if count >= 255 then -- 255 is safer from collision - warn("ran out of ids :(") - return 0 - end - - local b: number = hash - for i = 1, #name do - b = bit32.bxor(bit32.lshift(b, 5) + b, string.byte(name, i)) - end - - local reduced = bit32.band(b, MAX_VALUE) - cache[name] = reduced - count += 1 - return reduced +if not shared.__identifier_registry then + shared.__identifier_registry = { + cache = {} :: { [string]: number }, + counter = 0, + } +end + +local registry = shared.__identifier_registry +return function(name: string): number + local cached = registry.cache[name] + if cached then + return cached + end + + local id = registry.counter + 1 + assert(id <= MAX_VALUE, `Identifier overflow: exceeded {MAX_VALUE + 1} unique names.`) + registry.counter = id + 1 + registry.cache[name] = id + return id end