diff options
| author | ruki <[email protected]> | 2025-10-01 00:36:38 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2025-10-01 00:36:38 +0800 |
| commit | e4a2d682015b651adfcf7d7494a295c3276d7f10 (patch) | |
| tree | 208f1fc98dec6ea1baeb3dbd294bea3697d30349 | |
| parent | b86ebe99285371dcdf08483208b7de70a7b8b050 (diff) | |
add hash.random32/64/128
| -rw-r--r-- | tests/benchmarks/hash.lua | 84 | ||||
| -rw-r--r-- | xmake/core/base/hash.lua | 58 | ||||
| -rw-r--r-- | xmake/core/base/os.lua | 3 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/hash.lua | 69 |
4 files changed, 193 insertions, 21 deletions
diff --git a/tests/benchmarks/hash.lua b/tests/benchmarks/hash.lua index 48c360d58..ee23921c6 100644 --- a/tests/benchmarks/hash.lua +++ b/tests/benchmarks/hash.lua @@ -49,6 +49,18 @@ function test_uuid(data) print("uuid(%d): %d ms, hash: %s", COUNT, t * 10000, h) end +function test_xxhash32(data) + data = bytes(data) + local h + local n = COUNT / 10 + local t = os.mclock() + for i = 1, n do + h = hash.xxhash32(data) + end + t = os.mclock() - t + print("xxhash32(%d): %d ms, hash: %s", COUNT, t * 10, h) +end + function test_xxhash64(data) data = bytes(data) local h @@ -84,6 +96,17 @@ function test_strhash32(data) print("strhash32(%d): %d ms, hash: %s", COUNT, t * 10, h) end +function test_strhash64(data) + local h + local n = COUNT / 10 + local t = os.mclock() + for i = 1, n do + h = hash.strhash64(data) + end + t = os.mclock() - t + print("strhash64(%d): %d ms, hash: %s", COUNT, t * 10, h) +end + function test_strhash128(data) local h local n = COUNT / 10 @@ -95,6 +118,50 @@ function test_strhash128(data) print("strhash128(%d): %d ms, hash: %s", COUNT, t * 10, h) end +function test_random_uuid() + local h + local n = COUNT / 10 + local t = os.mclock() + for i = 1, n do + h = hash.uuid() + end + t = os.mclock() - t + print("uuid(%d): %d ms, hash: %s", COUNT, t * 10, h) +end + +function test_random32() + local h + local n = COUNT / 10 + local t = os.mclock() + for i = 1, n do + h = hash.random32() + end + t = os.mclock() - t + print("random32(%d): %d ms, hash: %s", COUNT, t * 10, h) +end + +function test_random64() + local h + local n = COUNT / 10 + local t = os.mclock() + for i = 1, n do + h = hash.random64() + end + t = os.mclock() - t + print("random64(%d): %d ms, hash: %s", COUNT, t * 10, h) +end + +function test_random128() + local h + local n = COUNT / 10 + local t = os.mclock() + for i = 1, n do + h = hash.random128() + end + t = os.mclock() - t + print("random128(%d): %d ms, hash: %s", COUNT, t * 10, h) +end + function test_longstr() print("========================================== test long string ==========================================") local data = "" @@ -105,9 +172,11 @@ function test_longstr() test_sha1(data) test_sha256(data) test_uuid(data) + test_xxhash32(data) test_xxhash64(data) test_xxhash128(data) test_strhash32(data) + test_strhash64(data) test_strhash128(data) end @@ -122,13 +191,24 @@ function test_shortstr() test_sha1(data) test_sha256(data) test_uuid(data) + test_xxhash32(data) test_xxhash64(data) test_xxhash128(data) test_strhash32(data) + test_strhash64(data) test_strhash128(data) end +function test_random() + print("========================================== test random ==========================================") + test_random_uuid() + test_random32() + test_random64() + test_random128() +end + function main() - test_longstr() - test_shortstr() +-- test_longstr() +-- test_shortstr() + test_random() end diff --git a/xmake/core/base/hash.lua b/xmake/core/base/hash.lua index 53f535631..5f162af65 100644 --- a/xmake/core/base/hash.lua +++ b/xmake/core/base/hash.lua @@ -71,6 +71,20 @@ function hash.sha256(file_or_data) return hashstr, errors end +-- generate uuid, e.g "91E8ECF1-417F-4EDF-A574-E22D7D8D204A" +function hash.uuid(str) + return hash.uuid4(str) +end + +-- generate xxhash32 from the given file or data +function hash.xxhash32(file_or_data) + local result, errors = hash.xxhash64(file_or_data) + if result then + result = result:sub(1, 8) + end + return result, errors +end + -- generate xxhash64 from the given file or data function hash.xxhash64(file_or_data) local hashstr, errors @@ -97,16 +111,22 @@ function hash.xxhash128(file_or_data) return hashstr, errors end --- generate uuid, e.g "91E8ECF1-417F-4EDF-A574-E22D7D8D204A" -function hash.uuid(str) - return hash.uuid4(str) -end - -- generate hash32 from string, e.g. "91e8ecf1" function hash.strhash32(str) local data = libc.ptraddr(libc.dataptr(str)) local size = #str - return hash._xxhash(64, data, size):sub(1, 8) + local result, errors = hash._xxhash(64, data, size) + if result then + result = result:sub(1, 8) + end + return result, errors +end + +-- generate hash64 from string, e.g. "91e8ecf191e8ecf1" +function hash.strhash64(str) + local data = libc.ptraddr(libc.dataptr(str)) + local size = #str + return hash._xxhash(64, data, size) end -- generate hash128 from string, e.g. "91e8ecf1417f4edfa574e22d7d8d204a" @@ -116,5 +136,31 @@ function hash.strhash128(str) return hash._xxhash(128, data, size) end +-- init random seed +function hash._init_random_seed() + if hash._INIT_RANDOM_SEED == nil then + math.randomseed(os.time()) + hash._INIT_RANDOM_SEED = true + end +end + +-- generate random32 hash +function hash.random32() + return hash.strhash32(tostring(math.random())) +end + +-- generate random64 hash +function hash.random64() + return hash.strhash64(tostring(math.random())) +end + +-- generate random128 hash +function hash.random128() + return hash.strhash128(tostring(math.random())) +end + +-- init random seed first +hash._init_random_seed() + -- return module: hash return hash diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua index 41fe07d22..72d395d77 100644 --- a/xmake/core/base/os.lua +++ b/xmake/core/base/os.lua @@ -761,7 +761,8 @@ function os.tmpfile(opt_or_key) key = opt_or_key.key opt = opt_or_key end - return path.join(os.tmpdir(opt), "_" .. (hash.uuid4(key):gsub("-", ""))) + local filename = "_" .. (key and hash.strhash128(key) or (hash.random128())) + return path.join(os.tmpdir(opt), filename) end -- exit program diff --git a/xmake/core/sandbox/modules/hash.lua b/xmake/core/sandbox/modules/hash.lua index 11a85f3fb..178bb2a5f 100644 --- a/xmake/core/sandbox/modules/hash.lua +++ b/xmake/core/sandbox/modules/hash.lua @@ -70,40 +70,85 @@ function sandbox_hash.md5(file_or_data) return md5 end +-- generate xxhash32 from the given file or data +function sandbox_hash.xxhash32(file_or_data) + local result, errors = hash.xxhash32(file_or_data) + if not result then + raise("cannot generate xxhash32, %s", errors or "unknown errors") + end + return result +end + -- generate xxhash64 from the given file or data function sandbox_hash.xxhash64(file_or_data) - local xxhash64, errors = hash.xxhash64(file_or_data) - if not xxhash64 then + local result, errors = hash.xxhash64(file_or_data) + if not result then raise("cannot generate xxhash64, %s", errors or "unknown errors") end - return xxhash64 + return result end -- generate xxhash128 from the given file or data function sandbox_hash.xxhash128(file_or_data) - local xxhash128, errors = hash.xxhash128(file_or_data) - if not xxhash128 then + local result, errors = hash.xxhash128(file_or_data) + if not result then raise("cannot generate xxhash128, %s", errors or "unknown errors") end - return xxhash128 + return result end -- generate hash32 from string function sandbox_hash.strhash32(str) - local hash32, errors = hash.strhash32(str) - if not hash32 then + local result, errors = hash.strhash32(str) + if not result then + raise("cannot generate hash32 for %s, %s", str, errors or "unknown errors") + end + return result +end + +-- generate hash64 from string +function sandbox_hash.strhash64(str) + local result, errors = hash.strhash64(str) + if not result then raise("cannot generate hash32 for %s, %s", str, errors or "unknown errors") end - return hash32 + return result end -- generate hash128 from string function sandbox_hash.strhash128(str) - local hash128, errors = hash.strhash128(str) - if not hash128 then + local result, errors = hash.strhash128(str) + if not result then raise("cannot generate hash128 for %s, %s", str, errors or "unknown errors") end - return hash128 + return result +end + +-- generate random32 +function sandbox_hash.random32() + local result, errors = hash.random32() + if not result then + raise("cannot generate random32, %s", errors or "unknown errors") + end + return result +end + +-- generate random64 +function sandbox_hash.random64() + local result, errors = hash.random64() + if not result then + raise("cannot generate random64, %s", errors or "unknown errors") + end + return result +end + +-- generate random128 +function sandbox_hash.random128() + local result, errors = hash.random128() + if not result then + raise("cannot generate random128, %s", errors or "unknown errors") + end + return result end -- return module |
