From b399deebddf9cc2af2bc02a5349b11ce447d41d7 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Mon, 25 Dec 2023 10:25:35 +0100 Subject: [PATCH] 15% faster crypto.randomUUID (#7824) * Fixes #7811 * [autofix.ci] apply automated fixes * Update src/bun.js/webcore.zig Co-authored-by: Markus Staab --------- Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Markus Staab --- bench/snippets/crypto-randomUUID.mjs | 7 +++++++ src/bun.js/webcore.zig | 20 +++++++++++++------- src/defines-table.zig | 4 ++++ 3 files changed, 24 insertions(+), 7 deletions(-) create mode 100644 bench/snippets/crypto-randomUUID.mjs diff --git a/bench/snippets/crypto-randomUUID.mjs b/bench/snippets/crypto-randomUUID.mjs new file mode 100644 index 0000000000..f6a4c0aa68 --- /dev/null +++ b/bench/snippets/crypto-randomUUID.mjs @@ -0,0 +1,7 @@ +import { bench, run } from "./runner.mjs"; + +bench("crypto.randomUUID()", () => { + return crypto.randomUUID(); +}); + +await run(); diff --git a/src/bun.js/webcore.zig b/src/bun.js/webcore.zig index 2b6dad6a1c..341ccdb73e 100644 --- a/src/bun.js/webcore.zig +++ b/src/bun.js/webcore.zig @@ -624,11 +624,13 @@ pub const Crypto = struct { globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame, ) callconv(.C) JSC.JSValue { - var out: [36]u8 = undefined; + const str, var bytes = bun.String.createUninitialized(.latin1, 36); + defer str.deref(); + const uuid = globalThis.bunVM().rareData().nextUUID(); - uuid.print(&out); - return JSC.ZigString.init(&out).toValueGC(globalThis); + uuid.print(bytes[0..36]); + return str.toJS(globalThis); } pub fn randomInt(_: *@This(), _: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSValue { @@ -656,11 +658,15 @@ pub const Crypto = struct { _: *Crypto, globalThis: *JSC.JSGlobalObject, ) callconv(.C) JSC.JSValue { - var out: [36]u8 = undefined; - const uuid = globalThis.bunVM().rareData().nextUUID(); + const str, var bytes = bun.String.createUninitialized(.latin1, 36); + defer str.deref(); - uuid.print(&out); - return JSC.ZigString.init(&out).toValueGC(globalThis); + // randomUUID must have been called already many times before this kicks + // in so we can skip the rare_data pointer check. + const uuid = globalThis.bunVM().rare_data.?.nextUUID(); + + uuid.print(bytes[0..36]); + return str.toJS(globalThis); } pub fn constructor(globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame) callconv(.C) ?*Crypto { diff --git a/src/defines-table.zig b/src/defines-table.zig index e7528a825d..1fa85a0b82 100644 --- a/src/defines-table.zig +++ b/src/defines-table.zig @@ -154,6 +154,8 @@ pub const GlobalDefinesKey = [_][]const string{ &[_]string{ "console", "timeLog" }, &[_]string{ "console", "trace" }, &[_]string{ "console", "warn" }, + + &[_]string{ "crypto", "randomUUID" }, }; const pure_global_identifier_define = defines.IdentifierDefine{ @@ -857,6 +859,8 @@ pub const pure_global_identifiers = .{ .{ "top", pure_global_identifier_define }, .{ "webkitURL", pure_global_identifier_define }, .{ "window", pure_global_identifier_define }, + + .{ "crypto", pure_global_identifier_define }, }; pub const pure_global_identifier_map = bun.ComptimeStringMap(defines.IdentifierDefine, pure_global_identifiers);