diff options
| author | ruki <[email protected]> | 2025-09-30 22:43:05 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2025-09-30 22:43:05 +0800 |
| commit | 703dec087d98946bc2ba5384d2274e588f8ee96d (patch) | |
| tree | 27cc9c304c05c2916135b3afc2d8e4e51f828834 | |
| parent | 9dd7fe46d8678216b62016b721fe084d5bad9246 (diff) | |
improve strhash
| -rw-r--r-- | tests/benchmarks/hash.lua | 9 | ||||
| -rw-r--r-- | xmake/core/base/hash.lua | 4 | ||||
| -rw-r--r-- | xmake/core/package/package.lua | 8 |
3 files changed, 14 insertions, 7 deletions
diff --git a/tests/benchmarks/hash.lua b/tests/benchmarks/hash.lua index 8c70c342a..48c360d58 100644 --- a/tests/benchmarks/hash.lua +++ b/tests/benchmarks/hash.lua @@ -75,24 +75,24 @@ end function test_strhash32(data) local h - local n = COUNT / 10000 + local n = COUNT / 10 local t = os.mclock() for i = 1, n do h = hash.strhash32(data) end t = os.mclock() - t - print("strhash32(%d): %d ms, hash: %s", COUNT, t * 10000, h) + print("strhash32(%d): %d ms, hash: %s", COUNT, t * 10, h) end function test_strhash128(data) local h - local n = COUNT / 10000 + local n = COUNT / 10 local t = os.mclock() for i = 1, n do h = hash.strhash128(data) end t = os.mclock() - t - print("strhash128(%d): %d ms, hash: %s", COUNT, t * 10000, h) + print("strhash128(%d): %d ms, hash: %s", COUNT, t * 10, h) end function test_longstr() @@ -113,6 +113,7 @@ end function test_shortstr() print("========================================== test short string ==========================================") + COUNT = COUNT * 100 local data = "" for i = 1, 10 do data = data .. "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" diff --git a/xmake/core/base/hash.lua b/xmake/core/base/hash.lua index 362eb67e5..b40f7b885 100644 --- a/xmake/core/base/hash.lua +++ b/xmake/core/base/hash.lua @@ -104,12 +104,12 @@ end -- TODO, we should optimize it -- generate hash32 from string, e.g. "91e8ecf1" function hash.strhash32(str) - return hash.uuid4(str):split("-", {plain = true})[1]:lower() + return hash.xxhash64(bytes(str)):sub(1, 8) end -- generate hash128 from string, e.g. "91e8ecf1417f4edfa574e22d7d8d204a" function hash.strhash128(str) - return hash.uuid4(str):replace("-", "", {plain = true}):lower() + return hash.xxhash128(bytes(str)) end -- return module: hash diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua index 4b0ddd036..4c68f277c 100644 --- a/xmake/core/package/package.lua +++ b/xmake/core/package/package.lua @@ -1687,6 +1687,12 @@ function _instance:_compute_buildhash() self:buildhash() end +-- hash.strhash128 has been switched to xxhash. +-- For compatibility, the old hash algorithm is still used here. +function _instance:_strhash128(str) + return hash.uuid4(str):replace("-", "", {plain = true}):lower() +end + -- get the build hash function _instance:buildhash() local buildhash = self._BUILDHASH @@ -1748,7 +1754,7 @@ function _instance:buildhash() table.sort(toolchains) str = str .. "_" .. table.concat(toolchains, "_") end - return hash.strhash128(str) + return self:_strhash128(str) end local function _get_installdir(...) local name = self:name():lower():gsub("::", "_") |
