mirror of
https://github.com/oven-sh/bun
synced 2026-02-09 10:28:47 +00:00
15% faster crypto.randomUUID (#7824)
* Fixes #7811 * [autofix.ci] apply automated fixes * Update src/bun.js/webcore.zig Co-authored-by: Markus Staab <maggus.staab@googlemail.com> --------- 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 <maggus.staab@googlemail.com>
This commit is contained in:
7
bench/snippets/crypto-randomUUID.mjs
Normal file
7
bench/snippets/crypto-randomUUID.mjs
Normal file
@@ -0,0 +1,7 @@
|
||||
import { bench, run } from "./runner.mjs";
|
||||
|
||||
bench("crypto.randomUUID()", () => {
|
||||
return crypto.randomUUID();
|
||||
});
|
||||
|
||||
await run();
|
||||
@@ -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 {
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user