diff options
author | 2022-03-26 20:35:37 -0700 | |
---|---|---|
committer | 2022-03-26 20:35:37 -0700 | |
commit | 29a759a65512278f1c20d1089ba05dbae268ef24 (patch) | |
tree | 8f922320d54f6a99639bcfe8b53d971cab6a029c | |
parent | b54a51adb96b3eea6aacff4f83f3ac39ddbc933e (diff) | |
download | bun-29a759a65512278f1c20d1089ba05dbae268ef24.tar.gz bun-29a759a65512278f1c20d1089ba05dbae268ef24.tar.zst bun-29a759a65512278f1c20d1089ba05dbae268ef24.zip |
improve performance of accessing `Bun.Transpiler` and `Bun.unsafe`
-rw-r--r-- | src/javascript/jsc/api/bun.zig | 40 | ||||
-rw-r--r-- | src/javascript/jsc/bindings/bindings.cpp | 19 | ||||
-rw-r--r-- | src/javascript/jsc/bindings/bindings.zig | 10 | ||||
-rw-r--r-- | src/javascript/jsc/bindings/headers.h | 2 | ||||
-rw-r--r-- | src/javascript/jsc/bindings/headers.zig | 2 |
5 files changed, 69 insertions, 4 deletions
diff --git a/src/javascript/jsc/api/bun.zig b/src/javascript/jsc/api/bun.zig index acb8daf2b..0eaab1dbb 100644 --- a/src/javascript/jsc/api/bun.zig +++ b/src/javascript/jsc/api/bun.zig @@ -1165,7 +1165,15 @@ pub fn getTranspilerConstructor( _: js.JSStringRef, _: js.ExceptionRef, ) js.JSValueRef { - return Transpiler.Constructor.constructor(ctx); + var existing = ctx.ptr().getCachedObject(&ZigString.init("BunTranspiler")); + if (existing.isEmpty()) { + return ctx.ptr().putCachedObject( + &ZigString.init("BunTranspiler"), + JSC.JSValue.fromRef(Transpiler.Constructor.constructor(ctx)), + ).asObjectRef(); + } + + return existing.asObjectRef(); } pub fn getTOMLObject( @@ -1175,7 +1183,15 @@ pub fn getTOMLObject( _: js.JSStringRef, _: js.ExceptionRef, ) js.JSValueRef { - return js.JSObjectMake(ctx, TOML.Class.get().?[0], null); + var existing = ctx.ptr().getCachedObject(&ZigString.init("TOML")); + if (existing.isEmpty()) { + return ctx.ptr().putCachedObject( + &ZigString.init("TOML"), + JSValue.fromRef(js.JSObjectMake(ctx, TOML.Class.get().?[0], null)), + ).asObjectRef(); + } + + return existing.asObjectRef(); } pub fn getUnsafe( @@ -1185,7 +1201,15 @@ pub fn getUnsafe( _: js.JSStringRef, _: js.ExceptionRef, ) js.JSValueRef { - return js.JSObjectMake(ctx, Unsafe.Class.get().?[0], null); + var existing = ctx.ptr().getCachedObject(&ZigString.init("Unsafe")); + if (existing.isEmpty()) { + return ctx.ptr().putCachedObject( + &ZigString.init("Unsafe"), + JSValue.fromRef(js.JSObjectMake(ctx, Unsafe.Class.get().?[0], null)), + ).asObjectRef(); + } + + return existing.asObjectRef(); } pub const Unsafe = struct { @@ -1630,7 +1654,15 @@ pub const EnvironmentVariables = struct { _: js.JSStringRef, _: js.ExceptionRef, ) js.JSValueRef { - return js.JSObjectMake(ctx, EnvironmentVariables.Class.get().*, null); + var existing = ctx.ptr().getCachedObject(&ZigString.init("Bun.env")); + if (existing.isEmpty()) { + return ctx.ptr().putCachedObject( + &ZigString.init("Bun.env"), + JSValue.fromRef(js.JSObjectMake(ctx, EnvironmentVariables.Class.get().*, null)), + ).asObjectRef(); + } + + return existing.asObjectRef(); } pub const BooleanString = struct { diff --git a/src/javascript/jsc/bindings/bindings.cpp b/src/javascript/jsc/bindings/bindings.cpp index e4dbdc949..3a155f5dd 100644 --- a/src/javascript/jsc/bindings/bindings.cpp +++ b/src/javascript/jsc/bindings/bindings.cpp @@ -317,6 +317,25 @@ JSC__JSValue JSC__JSValue__parseJSON(JSC__JSValue JSValue0, JSC__JSGlobalObject* return JSC::JSValue::encode(result); } +JSC__JSValue JSC__JSGlobalObject__getCachedObject(JSC__JSGlobalObject* globalObject, const ZigString* arg1) +{ + JSC::VM& vm = globalObject->vm(); + WTF::String string = Zig::toString(*arg1); + auto symbol = vm.privateSymbolRegistry().symbolForKey(string); + JSC::Identifier ident = JSC::Identifier::fromUid(symbol); + JSC::JSValue result = globalObject->getIfPropertyExists(globalObject, ident); + return JSC::JSValue::encode(result); +} +JSC__JSValue JSC__JSGlobalObject__putCachedObject(JSC__JSGlobalObject* globalObject, const ZigString* arg1, JSC__JSValue JSValue2) +{ + JSC::VM& vm = globalObject->vm(); + WTF::String string = Zig::toString(*arg1); + auto symbol = vm.privateSymbolRegistry().symbolForKey(string); + JSC::Identifier ident = JSC::Identifier::fromUid(symbol); + globalObject->putDirect(vm, ident, JSC::JSValue::decode(JSValue2), JSC::PropertyAttribute::DontDelete | JSC::PropertyAttribute::DontEnum); + return JSValue2; +} + void JSC__JSGlobalObject__deleteModuleRegistryEntry(JSC__JSGlobalObject* global, ZigString* arg1) { JSC::JSMap* map = JSC::jsDynamicCast<JSC::JSMap*>( diff --git a/src/javascript/jsc/bindings/bindings.zig b/src/javascript/jsc/bindings/bindings.zig index 64ee478c6..6b45fdb1b 100644 --- a/src/javascript/jsc/bindings/bindings.zig +++ b/src/javascript/jsc/bindings/bindings.zig @@ -1330,6 +1330,14 @@ pub const JSGlobalObject = extern struct { return cppFn("generateHeapSnapshot", .{this}); } + pub fn putCachedObject(this: *JSGlobalObject, key: *const ZigString, value: JSValue) JSValue { + return cppFn("putCachedObject", .{ this, key, value }); + } + + pub fn getCachedObject(this: *JSGlobalObject, key: *const ZigString) JSValue { + return cppFn("getCachedObject", .{ this, key }); + } + pub fn vm(this: *JSGlobalObject) *VM { return cppFn("vm", .{this}); } @@ -1339,6 +1347,8 @@ pub const JSGlobalObject = extern struct { } pub const Extern = [_][]const u8{ + "putCachedObject", + "getCachedObject", "createAggregateError", "objectPrototype", "functionPrototype", diff --git a/src/javascript/jsc/bindings/headers.h b/src/javascript/jsc/bindings/headers.h index 369f34ad9..dddfb5cb4 100644 --- a/src/javascript/jsc/bindings/headers.h +++ b/src/javascript/jsc/bindings/headers.h @@ -412,6 +412,7 @@ CPP_DECL JSC__FunctionPrototype* JSC__JSGlobalObject__functionPrototype(JSC__JSG CPP_DECL JSC__JSValue JSC__JSGlobalObject__generateHeapSnapshot(JSC__JSGlobalObject* arg0); CPP_DECL JSC__GeneratorFunctionPrototype* JSC__JSGlobalObject__generatorFunctionPrototype(JSC__JSGlobalObject* arg0); CPP_DECL JSC__GeneratorPrototype* JSC__JSGlobalObject__generatorPrototype(JSC__JSGlobalObject* arg0); +CPP_DECL JSC__JSValue JSC__JSGlobalObject__getCachedObject(JSC__JSGlobalObject* arg0, const ZigString* arg1); CPP_DECL JSC__IteratorPrototype* JSC__JSGlobalObject__iteratorPrototype(JSC__JSGlobalObject* arg0); CPP_DECL JSC__JSObject* JSC__JSGlobalObject__jsSetPrototype(JSC__JSGlobalObject* arg0); CPP_DECL JSC__MapIteratorPrototype* JSC__JSGlobalObject__mapIteratorPrototype(JSC__JSGlobalObject* arg0); @@ -419,6 +420,7 @@ CPP_DECL JSC__JSObject* JSC__JSGlobalObject__mapPrototype(JSC__JSGlobalObject* a CPP_DECL JSC__JSObject* JSC__JSGlobalObject__numberPrototype(JSC__JSGlobalObject* arg0); CPP_DECL JSC__ObjectPrototype* JSC__JSGlobalObject__objectPrototype(JSC__JSGlobalObject* arg0); CPP_DECL JSC__JSPromisePrototype* JSC__JSGlobalObject__promisePrototype(JSC__JSGlobalObject* arg0); +CPP_DECL JSC__JSValue JSC__JSGlobalObject__putCachedObject(JSC__JSGlobalObject* arg0, const ZigString* arg1, JSC__JSValue JSValue2); CPP_DECL JSC__RegExpPrototype* JSC__JSGlobalObject__regExpPrototype(JSC__JSGlobalObject* arg0); CPP_DECL JSC__SetIteratorPrototype* JSC__JSGlobalObject__setIteratorPrototype(JSC__JSGlobalObject* arg0); CPP_DECL JSC__StringPrototype* JSC__JSGlobalObject__stringPrototype(JSC__JSGlobalObject* arg0); diff --git a/src/javascript/jsc/bindings/headers.zig b/src/javascript/jsc/bindings/headers.zig index 509249095..d42eaf7e5 100644 --- a/src/javascript/jsc/bindings/headers.zig +++ b/src/javascript/jsc/bindings/headers.zig @@ -222,6 +222,7 @@ pub extern fn JSC__JSGlobalObject__functionPrototype(arg0: [*c]JSC__JSGlobalObje pub extern fn JSC__JSGlobalObject__generateHeapSnapshot(arg0: [*c]JSC__JSGlobalObject) JSC__JSValue; pub extern fn JSC__JSGlobalObject__generatorFunctionPrototype(arg0: [*c]JSC__JSGlobalObject) ?*JSC__GeneratorFunctionPrototype; pub extern fn JSC__JSGlobalObject__generatorPrototype(arg0: [*c]JSC__JSGlobalObject) ?*JSC__GeneratorPrototype; +pub extern fn JSC__JSGlobalObject__getCachedObject(arg0: [*c]JSC__JSGlobalObject, arg1: [*c]const ZigString) JSC__JSValue; pub extern fn JSC__JSGlobalObject__iteratorPrototype(arg0: [*c]JSC__JSGlobalObject) ?*JSC__IteratorPrototype; pub extern fn JSC__JSGlobalObject__jsSetPrototype(arg0: [*c]JSC__JSGlobalObject) [*c]JSC__JSObject; pub extern fn JSC__JSGlobalObject__mapIteratorPrototype(arg0: [*c]JSC__JSGlobalObject) ?*JSC__MapIteratorPrototype; @@ -229,6 +230,7 @@ pub extern fn JSC__JSGlobalObject__mapPrototype(arg0: [*c]JSC__JSGlobalObject) [ pub extern fn JSC__JSGlobalObject__numberPrototype(arg0: [*c]JSC__JSGlobalObject) [*c]JSC__JSObject; pub extern fn JSC__JSGlobalObject__objectPrototype(arg0: [*c]JSC__JSGlobalObject) ?*JSC__ObjectPrototype; pub extern fn JSC__JSGlobalObject__promisePrototype(arg0: [*c]JSC__JSGlobalObject) ?*JSC__JSPromisePrototype; +pub extern fn JSC__JSGlobalObject__putCachedObject(arg0: [*c]JSC__JSGlobalObject, arg1: [*c]const ZigString, JSValue2: JSC__JSValue) JSC__JSValue; pub extern fn JSC__JSGlobalObject__regExpPrototype(arg0: [*c]JSC__JSGlobalObject) ?*JSC__RegExpPrototype; pub extern fn JSC__JSGlobalObject__setIteratorPrototype(arg0: [*c]JSC__JSGlobalObject) ?*JSC__SetIteratorPrototype; pub extern fn JSC__JSGlobalObject__stringPrototype(arg0: [*c]JSC__JSGlobalObject) ?*JSC__StringPrototype; |