Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linux performance issues #2299

Closed
ghost opened this issue Dec 29, 2016 · 3 comments
Closed

Linux performance issues #2299

ghost opened this issue Dec 29, 2016 · 3 comments

Comments

@ghost
Copy link

ghost commented Dec 29, 2016

Hi,

We talked about the GC features not yet implemented on Linux. Following is an example of where JsCreateString in a hot path can severely harm perfomance:

#include <ChakraCore.h>
#include <iostream>
#include <cstring>
#include <fstream>

void registerGlobalFunction(const char *name, JsNativeFunction nativeFunction) {
    JsValueRef jsFunction;
    JsCreateFunction(nativeFunction, nullptr, &jsFunction);
    JsPropertyIdRef propertyId;
    JsCreatePropertyIdUtf8(name, strlen(name), &propertyId);
    JsValueRef globalObject;
    JsGetGlobalObject(&globalObject);
    JsSetProperty(globalObject, propertyId, jsFunction, true);
}

JsContextRef context = 0;
JsValueRef cbFunction = 0;

int main(int argc, char **argv) {

    std::string script = "setCb((verb) => {});";

    JsRuntimeHandle runtime = 0;
    JsValueRef result = 0;
    unsigned currentSourceContext = 0;

    JsCreateRuntime(JsRuntimeAttributeEnableExperimentalFeatures, nullptr, &runtime);
    JsCreateContext(runtime, &context);
    JsSetCurrentContext(context);

    registerGlobalFunction("setCb", [](JsValueRef callee, bool isConstructCall, JsValueRef *arguments,
                           unsigned short argumentCount, void *data) -> JsValueRef {
        JsAddRef((cbFunction = arguments[1]), nullptr);
        return nullptr;
    });

    JsValueRef scriptSource = 0, scriptUrl = 0;
    JsCreateExternalArrayBuffer((void *) script.data(), script.length(), nullptr, nullptr, &scriptSource);

    JsCreateString("scriptUrl", 4, &scriptUrl);
    JsRun(scriptSource, currentSourceContext++, scriptUrl, JsParseScriptAttributeNone, &result);

    JsValueRef globalObject = 0;
    JsGetGlobalObject(&globalObject);

    const char string[] = "Something for JS-land";

    // adjust this number for your computer
    for (int i = 0; i < 20000; i++) {
        JsValueRef verb = 0;
        // creation of this string cause major slowdown
        JsCreateString(string, sizeof(string) - 1, &verb);

        JsValueRef arguments[] = {globalObject, verb};
        JsCallFunction(cbFunction, arguments, 2, &result);
    }

    JsSetCurrentContext(JS_INVALID_REFERENCE);
    JsDisposeRuntime(runtime);

    std::cout << "Reached the end" << std::endl;
    return 0;
}
@obastemur
Copy link
Collaborator

I was suspecting from Utf8 encode/decode stuff at first. However, testing the sample above with different inputs show GC is the real cause. (note: we don't have high performance GC features available on xplat yet.)

20K -> original loop

real	0m0.339s
user	0m0.324s
sys	0m0.008s

30K

real	0m0.902s
user	0m0.786s
sys	0m0.018s

50K

real	0m1.718s
user	0m1.702s
sys	0m0.009s

@alexhultman Thanks for the test case.

@digitalinfinity
Copy link
Contributor

@leirocks @jianchun FYI, worth running this test case as part of SWB/Partial GC validation

@dilijev dilijev added this to the Cross-platform milestone Jan 31, 2017
@obastemur
Copy link
Collaborator

This is no longer an issue. @alexhultman could you please contribute this sample as a test case?

Create a new folder under test/native-tests and copy Platform.js and sample.cpp from test-char. Update the sample.cpp with your code above. JsCreatePropertyIdUtf8 -> JsCreatePropertyId

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants