diff options
author | 2022-07-04 08:04:21 -0700 | |
---|---|---|
committer | 2022-07-04 08:04:21 -0700 | |
commit | 2ba3a6fbbcce3402884ccb6d3e93523782234b79 (patch) | |
tree | 4af3d122cdabbb6e0ed3b69a94f7b7341604dda1 | |
parent | 027d64c78fa42bf137a06c3875cdea718614ed9a (diff) | |
download | bun-2ba3a6fbbcce3402884ccb6d3e93523782234b79.tar.gz bun-2ba3a6fbbcce3402884ccb6d3e93523782234b79.tar.zst bun-2ba3a6fbbcce3402884ccb6d3e93523782234b79.zip |
Improve the performance of performance
-rw-r--r-- | src/bun.js/bindings/ZigGlobalObject.cpp | 48 | ||||
-rw-r--r-- | src/bun.js/bindings/ZigGlobalObject.h | 7 |
2 files changed, 49 insertions, 6 deletions
diff --git a/src/bun.js/bindings/ZigGlobalObject.cpp b/src/bun.js/bindings/ZigGlobalObject.cpp index 1e73da3ed..138267cd6 100644 --- a/src/bun.js/bindings/ZigGlobalObject.cpp +++ b/src/bun.js/bindings/ZigGlobalObject.cpp @@ -1487,6 +1487,29 @@ JSC_DEFINE_HOST_FUNCTION(functionConcatTypedArrays, (JSGlobalObject * globalObje return flattenArrayOfBuffersIntoArrayBuffer(globalObject, arrayValue); } +extern "C" uint64_t Bun__readOriginTimer(void*); + +JSC_DECLARE_HOST_FUNCTION(functionPerformanceNow); + +JSC_DEFINE_HOST_FUNCTION(functionPerformanceNow, (JSGlobalObject * globalObject, JSC::CallFrame* callFrame)) +{ + auto* global = reinterpret_cast<GlobalObject*>(globalObject); + // nanoseconds to seconds + uint64_t time = Bun__readOriginTimer(global->bunVM()); + double result = time / 1000000000.0; + return JSValue::encode(jsNumber(time)); +} + +JSC_DECLARE_HOST_FUNCTION(functionBunNanoseconds); + +JSC_DEFINE_HOST_FUNCTION(functionBunNanoseconds, (JSGlobalObject * globalObject, JSC::CallFrame* callFrame)) +{ + auto* global = reinterpret_cast<GlobalObject*>(globalObject); + // nanoseconds to seconds + uint64_t time = Bun__readOriginTimer(global->bunVM()); + return JSValue::encode(jsNumber(time)); +} + JSC_DECLARE_HOST_FUNCTION(functionConcatTypedArraysFromIterator); JSC_DEFINE_HOST_FUNCTION(functionConcatTypedArraysFromIterator, (JSGlobalObject * globalObject, JSC::CallFrame* callFrame)) @@ -1663,10 +1686,10 @@ void GlobalObject::finishCreation(VM& vm) init.set(prototype); }); - m_JSHTTPResponseControllerPrototype.initLater( - [](const JSC::LazyProperty<JSC::JSGlobalObject, JSC::JSObject>::Initializer& init) { - auto* prototype = createJSSinkControllerPrototype(init.vm, init.owner, WebCore::SinkID::HTTPResponseSink); - init.set(prototype); + m_JSHTTPResponseController.initLater( + [](const JSC::LazyProperty<JSC::JSGlobalObject, JSC::Structure>::Initializer& init) { + auto* structure = createJSSinkControllerStructure(init.vm, init.owner, WebCore::SinkID::HTTPResponseSink); + init.set(structure); }); m_JSHTTPSResponseControllerPrototype.initLater( @@ -1675,6 +1698,13 @@ void GlobalObject::finishCreation(VM& vm) init.set(prototype); }); + m_performanceObject.initLater( + [](const JSC::LazyProperty<JSC::JSGlobalObject, JSC::JSObject>::Initializer& init) { + JSC::JSObject* object = JSC::constructEmptyObject(init.owner, init.owner->objectPrototype(), 1); + object->putDirectNativeFunction(init.vm, init.owner, JSC::Identifier::fromString(init.vm, "now"_s), 1, functionPerformanceNow, NoIntrinsic, 0); + init.set(object); + }); + m_processEnvObject.initLater( [](const JSC::LazyProperty<JSC::JSGlobalObject, JSC::JSObject>::Initializer& init) { auto jsClass = reinterpret_cast<Zig::GlobalObject*>(init.owner)->m_dotEnvClassRef; @@ -1878,6 +1908,9 @@ void GlobalObject::addBuiltinGlobals(JSC::VM& vm) putDirectCustomAccessor(vm, JSC::Identifier::fromString(vm, "process"_s), JSC::CustomGetterSetter::create(vm, property_lazyProcessGetter, property_lazyProcessSetter), JSC::PropertyAttribute::CustomAccessor | 0); + putDirect(vm, JSC::Identifier::fromString(vm, "performance"_s), this->performanceObject(), + 0); + putDirectCustomAccessor(vm, JSC::Identifier::fromString(vm, "URL"_s), JSC::CustomGetterSetter::create(vm, JSDOMURL_getter, nullptr), JSC::PropertyAttribute::DontDelete | 0); @@ -2036,6 +2069,12 @@ void GlobalObject::installAPIGlobals(JSClassRef* globals, int count, JSC::VM& vm } { + JSC::Identifier identifier = JSC::Identifier::fromString(vm, "nanoseconds"_s); + object->putDirectNativeFunction(vm, this, identifier, 1, functionBunNanoseconds, NoIntrinsic, + JSC::PropertyAttribute::Function | JSC::PropertyAttribute::DontDelete | 0); + } + + { JSC::Identifier identifier = JSC::Identifier::fromString(vm, pathToFileURLString); object->putDirectNativeFunction(vm, this, identifier, 1, functionPathToFileURL, NoIntrinsic, @@ -2135,6 +2174,7 @@ void GlobalObject::visitChildrenImpl(JSCell* cell, Visitor& visitor) thisObject->m_requireMap.visit(visitor); thisObject->m_processEnvObject.visit(visitor); thisObject->m_processObject.visit(visitor); + thisObject->m_performanceObject.visit(visitor); visitor.append(thisObject->m_readableStreamToArrayBufferResolve); visitor.append(thisObject->m_readableStreamToText); diff --git a/src/bun.js/bindings/ZigGlobalObject.h b/src/bun.js/bindings/ZigGlobalObject.h index 47449321b..3832eb9c2 100644 --- a/src/bun.js/bindings/ZigGlobalObject.h +++ b/src/bun.js/bindings/ZigGlobalObject.h @@ -179,7 +179,7 @@ public: JSC::Structure* HTTPResponseSinkStructure() { return m_JSHTTPResponseSinkClassStructure.getInitializedOnMainThread(this); } JSC::JSObject* HTTPResponseSink() { return m_JSHTTPResponseSinkClassStructure.constructorInitializedOnMainThread(this); } JSC::JSValue HTTPResponseSinkPrototype() { return m_JSHTTPResponseSinkClassStructure.prototypeInitializedOnMainThread(this); } - JSC::JSValue JSReadableHTTPResponseSinkControllerPrototype() { return m_JSHTTPResponseControllerPrototype.getInitializedOnMainThread(this); } + JSC::Structure* JSReadableHTTPResponseSinkController() { return m_JSHTTPResponseController.getInitializedOnMainThread(this); } JSC::Structure* HTTPSResponseSinkStructure() { return m_JSHTTPSResponseSinkClassStructure.getInitializedOnMainThread(this); } JSC::JSObject* HTTPSResponseSink() { return m_JSHTTPSResponseSinkClassStructure.constructorInitializedOnMainThread(this); } @@ -189,6 +189,8 @@ public: JSC::JSMap* readableStreamNativeMap() { return m_lazyReadableStreamPrototypeMap.getInitializedOnMainThread(this); } JSC::JSMap* requireMap() { return m_requireMap.getInitializedOnMainThread(this); } + JSC::JSObject* performanceObject() { return m_performanceObject.getInitializedOnMainThread(this); } + JSC::JSObject* processObject() { return m_processObject.getInitializedOnMainThread(this); @@ -228,11 +230,12 @@ private: LazyProperty<JSGlobalObject, JSObject> m_JSArrayBufferControllerPrototype; LazyProperty<JSGlobalObject, JSObject> m_JSHTTPSResponseControllerPrototype; - LazyProperty<JSGlobalObject, JSObject> m_JSHTTPResponseControllerPrototype; + LazyProperty<JSGlobalObject, Structure> m_JSHTTPResponseController; LazyProperty<JSGlobalObject, JSObject> m_processObject; LazyProperty<JSGlobalObject, JSObject> m_processEnvObject; LazyProperty<JSGlobalObject, JSMap> m_lazyReadableStreamPrototypeMap; LazyProperty<JSGlobalObject, JSMap> m_requireMap; + LazyProperty<JSGlobalObject, JSObject> m_performanceObject; // LazyProperty<JSGlobalObject, WebCore::JSEventTarget> m_eventTarget; JSClassRef m_dotEnvClassRef; |