Compare commits

...

2 Commits

Author SHA1 Message Date
Jarred Sumner
a83a6702c0 Update zlib.zig 2024-06-25 23:13:41 -07:00
Jarred Sumner
768a36a706 Faster Bun.hash.crc32 2024-06-25 20:26:18 -07:00
5 changed files with 73 additions and 6 deletions

View File

@@ -0,0 +1,26 @@
import { bench, group, run } from "./runner.mjs";
const hashes = ["wyhash", "adler32", "crc32", "cityHash32", "cityHash64", "murmur32v3", "murmur32v2", "murmur64v2"];
group("hello world", () => {
for (const name of hashes) {
const fn = Bun.hash[name];
bench(`${name}`, () => {
return fn("hello world");
});
}
});
group("hello world (x 1024)", () => {
for (const name of hashes) {
const fn = Bun.hash[name];
const repeated = Buffer.alloc("hello world".length * 1024, "hello world").toString();
bench(`${name}`, () => {
return fn(repeated);
});
}
});
await run();

View File

@@ -3613,11 +3613,10 @@ pub fn getHashObject(
) callconv(.C) JSC.JSValue {
return HashObject.create(globalThis);
}
const HashObject = struct {
pub const wyhash = hashWrap(std.hash.Wyhash).hash;
pub const adler32 = hashWrap(std.hash.Adler32).hash;
pub const crc32 = hashWrap(std.hash.Crc32).hash;
pub const adler32 = hashWrap(bun.zlib.Adler32).hash;
pub const crc32 = hashWrap(bun.zlib.Crc32).hash;
pub const cityHash32 = hashWrap(std.hash.CityHash32).hash;
pub const cityHash64 = hashWrap(std.hash.CityHash64).hash;
pub const murmur32v2 = hashWrap(std.hash.murmur.Murmur2_32).hash;

View File

@@ -87,3 +87,6 @@ pub inline fn inflateInit2(strm: anytype, windowBits: anytype) ReturnCode {
pub inline fn inflateBackInit(strm: anytype, windowBits: anytype, window: anytype) ReturnCode {
return inflateBackInit_(strm, windowBits, window, zlibVersion(), @import("std").zig.c_translation.cast(c_int, @import("std").zig.c_translation.sizeof(z_stream)));
}
pub extern fn adler32(uLong, ptr: ?[*]const u8, len: uInt) uLong;
pub extern fn crc32(uLong, ptr: ?[*]const u8, len: uInt) uLong;

View File

@@ -119,10 +119,10 @@ pub extern fn gzclose_r(file: gzFile) ReturnCode;
pub extern fn gzclose_w(file: gzFile) ReturnCode;
pub extern fn gzerror(file: gzFile, errnum: [*c]c_int) [*c]const u8;
pub extern fn gzclearerr(file: gzFile) void;
pub extern fn adler32(adler: uLong, buf: [*c]const Bytef, len: uInt) uLong;
pub extern fn adler32(adler: uLong, buf: ?[*]const Bytef, len: uInt) uLong;
pub extern fn adler32_z(adler: uLong, buf: [*c]const Bytef, len: z_size_t) uLong;
pub extern fn crc32(crc: uLong, buf: [*c]const Bytef, len: uInt) uLong;
pub extern fn crc32_z(crc: uLong, buf: [*c]const Bytef, len: z_size_t) uLong;
pub extern fn crc32(crc: uLong, buf: ?[*]const Bytef, len: uInt) uLong;
pub extern fn crc32_z(crc: uLong, buf: ?[*]const Bytef, len: z_size_t) uLong;
pub extern fn crc32_combine_op(crc1: uLong, crc2: uLong, op: uLong) uLong;
pub extern fn deflateInit_(strm: z_streamp, level: c_int, version: [*c]const u8, stream_size: c_int) ReturnCode;
pub extern fn inflateInit_(strm: z_streamp, version: [*c]const u8, stream_size: c_int) ReturnCode;

View File

@@ -901,3 +901,42 @@ pub const ZlibCompressorArrayList = struct {
}
}
};
pub const adler32 = @import("zlib-internal").adler32;
pub const crc32 = @import("zlib-internal").crc32;
fn HasherFn(comptime function: anytype) type {
return struct {
state: uLong = 0,
const Hasher = @This();
pub fn init(this: *Hasher) void {
this.state = function(0, null, 0);
}
pub fn update(this: *Hasher, data: []const u8) void {
this.state = function(this.state, data.ptr, @intCast(data.len));
}
pub fn finish(this: *Hasher) uLong {
return this.state;
}
pub fn hash(data: []const u8) u32 {
var hasher = @This(){};
hasher.init();
hasher.update(data);
return @intCast(hasher.finish());
}
pub fn hashWithSeed(data: []const u8, seed: uLong) u32 {
var hasher = @This(){ .state = seed };
hasher.update(data);
return @intCast(hasher.finish());
}
};
}
pub const Adler32 = HasherFn(adler32);
pub const Crc32 = HasherFn(crc32);