rewrite(phase4): revert changes due to collision and updated tests unit for identifier

This commit is contained in:
khtsly 2026-02-14 00:27:25 +07:00
parent a82b8fd532
commit e4184709bd
2 changed files with 42 additions and 35 deletions

View file

@ -5,33 +5,38 @@ if not shared.__identifier_cache then
shared.__identifier_cache = { shared.__identifier_cache = {
cache = {} :: { [string]: number }, cache = {} :: { [string]: number },
used = {} :: { [number]: string }, used = {} :: { [number]: string },
count = 0 :: number,
} }
end end
local registry = shared.__identifier_cache local registry = shared.__identifier_cache
local cache = registry.cache local cache = registry.cache
local used = registry.used local used = registry.used
local count = registry.count
local BITS: number = 16
local MAX_VALUE: number = bit32.lshift(1, BITS) - 1
local hash: number = game.PlaceVersion + 5381
return function(name: string): number return function(name: string): number
local cached = cache[name] local cached = cache[name]
if cached then if cached then
return cached return cached
end end
local b: number = hash
local h = 2166136261 + game.PlaceVersion
for i = 1, #name do for i = 1, #name do
h = bit32.bxor(h, string.byte(name, i)) b = bit32.bxor(bit32.lshift(b, 5) + b, string.byte(name, i))
h = bit32.band(h * 16777619, 0xFFFFFFFF)
end end
local reduced = bit32.band(b, MAX_VALUE)
local id = h % 65534 + 2 if reduced < 2 then
reduced += 2
local existing = used[id] end
local existing = used[reduced]
if existing and existing ~= name then if existing and existing ~= name then
error(`[Warp] Hash collision: "{name}" & "{existing}" both map to ID {id}. Rename one.`) error(`[Warp] Hash collision: "{name}" & "{existing}" both map to ID {reduced}. Rename one.`)
end end
used[name] = reduced
cache[name] = id cache[name] = reduced
used[id] = name count += 1
return id return reduced
end end

View file

@ -4,6 +4,8 @@ return function()
local ReplicatedStorage = game:GetService("ReplicatedStorage") local ReplicatedStorage = game:GetService("ReplicatedStorage")
local WarpModule = ReplicatedStorage:WaitForChild("Warp") local WarpModule = ReplicatedStorage:WaitForChild("Warp")
local Identifier = require(WarpModule:WaitForChild("Util"):WaitForChild("Identifier")) local Identifier = require(WarpModule:WaitForChild("Util"):WaitForChild("Identifier"))
local U16_MAX, U16_MIN = 65535, 2
describe("Warp.Identifier", function() describe("Warp.Identifier", function()
it("returns a number", function() it("returns a number", function()
@ -16,10 +18,10 @@ return function()
expect(result == math.floor(result)).to.equal(true) expect(result == math.floor(result)).to.equal(true)
end) end)
it("returns a value within 0-255 range (8-bit)", function() it(`returns a value within {U16_MIN}-{U16_MAX} range (8-bit)`, function()
local result = Identifier("TestEvent") local result = Identifier("TestEvent")
expect(result >= 0).to.equal(true) expect(result >= U16_MIN).to.equal(true)
expect(result <= 255).to.equal(true) expect(result <= U16_MAX).to.equal(true)
end) end)
it("is deterministic (same name returns same value)", function() it("is deterministic (same name returns same value)", function()
@ -39,23 +41,23 @@ return function()
it("handles empty string", function() it("handles empty string", function()
local result = Identifier("") local result = Identifier("")
expect(type(result)).to.equal("number") expect(type(result)).to.equal("number")
expect(result >= 0).to.equal(true) expect(result >= U16_MIN).to.equal(true)
expect(result <= 255).to.equal(true) expect(result <= U16_MAX).to.equal(true)
end) end)
it("handles single character strings", function() it("handles single character strings", function()
local result = Identifier("A") local result = Identifier("A")
expect(type(result)).to.equal("number") expect(type(result)).to.equal("number")
expect(result >= 0).to.equal(true) expect(result >= U16_MIN).to.equal(true)
expect(result <= 255).to.equal(true) expect(result <= U16_MAX).to.equal(true)
end) end)
it("handles long strings", function() it("handles long strings", function()
local longName = string.rep("abcdefghij", 100) local longName = string.rep("abcdefghij", 100)
local result = Identifier(longName) local result = Identifier(longName)
expect(type(result)).to.equal("number") expect(type(result)).to.equal("number")
expect(result >= 0).to.equal(true) expect(result >= U16_MIN).to.equal(true)
expect(result <= 255).to.equal(true) expect(result <= U16_MAX).to.equal(true)
end) end)
it("produces at least some distinct values for different names", function() it("produces at least some distinct values for different names", function()
@ -64,7 +66,7 @@ return function()
for _, name in names do for _, name in names do
unique[Identifier(name)] = true unique[Identifier(name)] = true
end end
local count = 0 local count = U16_MIN
for _ in unique do for _ in unique do
count += 1 count += 1
end end
@ -75,8 +77,8 @@ return function()
it("stays within 8-bit range across many inputs", function() it("stays within 8-bit range across many inputs", function()
for i = 1, 500 do for i = 1, 500 do
local result = Identifier(`Event{i}`) local result = Identifier(`Event{i}`)
expect(result >= 0).to.equal(true) expect(result >= U16_MIN).to.equal(true)
expect(result <= 255).to.equal(true) expect(result <= U16_MAX).to.equal(true)
expect(result == math.floor(result)).to.equal(true) expect(result == math.floor(result)).to.equal(true)
end end
end) end)
@ -104,8 +106,8 @@ return function()
for _, name in specialNames do for _, name in specialNames do
local result = Identifier(name) local result = Identifier(name)
expect(type(result)).to.equal("number") expect(type(result)).to.equal("number")
expect(result >= 0).to.equal(true) expect(result >= U16_MIN).to.equal(true)
expect(result <= 255).to.equal(true) expect(result <= U16_MAX).to.equal(true)
end end
end) end)
@ -114,10 +116,10 @@ return function()
local b = Identifier("456") local b = Identifier("456")
expect(type(a)).to.equal("number") expect(type(a)).to.equal("number")
expect(type(b)).to.equal("number") expect(type(b)).to.equal("number")
expect(a >= 0).to.equal(true) expect(a >= U16_MIN).to.equal(true)
expect(a <= 255).to.equal(true) expect(a <= U16_MAX).to.equal(true)
expect(b >= 0).to.equal(true) expect(b >= U16_MIN).to.equal(true)
expect(b <= 255).to.equal(true) expect(b <= U16_MAX).to.equal(true)
end) end)
it("different names that are similar still hash correctly", function() it("different names that are similar still hash correctly", function()
@ -128,9 +130,9 @@ return function()
expect(type(b)).to.equal("number") expect(type(b)).to.equal("number")
expect(type(c)).to.equal("number") expect(type(c)).to.equal("number")
-- all in range -- all in range
expect(a >= 0 and a <= 255).to.equal(true) expect(a >= U16_MIN and a <= U16_MAX).to.equal(true)
expect(b >= 0 and b <= 255).to.equal(true) expect(b >= U16_MIN and b <= U16_MAX).to.equal(true)
expect(c >= 0 and c <= 255).to.equal(true) expect(c >= U16_MIN and c <= U16_MAX).to.equal(true)
end) end)
end) end)
end end