From 2ba3a6fbbcce3402884ccb6d3e93523782234b79 Mon Sep 17 00:00:00 2001 From: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Date: Mon, 4 Jul 2022 08:04:21 -0700 Subject: Improve the performance of performance --- src/bun.js/bindings/ZigGlobalObject.cpp | 48 ++++++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 4 deletions(-) (limited to 'src/bun.js/bindings/ZigGlobalObject.cpp') 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); + // 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); + // 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::Initializer& init) { - auto* prototype = createJSSinkControllerPrototype(init.vm, init.owner, WebCore::SinkID::HTTPResponseSink); - init.set(prototype); + m_JSHTTPResponseController.initLater( + [](const JSC::LazyProperty::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::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::Initializer& init) { auto jsClass = reinterpret_cast(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); @@ -2035,6 +2068,12 @@ void GlobalObject::installAPIGlobals(JSClassRef* globals, int count, JSC::VM& vm JSC::PropertyAttribute::DontDelete | JSC::PropertyAttribute::ReadOnly); } + { + 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); @@ -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); -- cgit v1.2.3