graaljs icon indicating copy to clipboard operation
graaljs copied to clipboard

Builtin Prototype splitting and inlining issue

Open 2767mr opened this issue 2 years ago • 2 comments

After trying the fix for #634 the compiler still generates non-optimal code. If one runs

function test() {
    const start = {next(){return 1}}
    for (let i = 0; i < 10000000; i++) {
        new Array().test.call(start, i => i * 2)
            .test(i => i + 1337)
            .test(i => '' + i)
            .test(i => ({name: i}))
            .next();
    }
}

for (let i = 0; i < 20; i++) {
    var start = performance.now();
    test();
    var end = performance.now();
    console.log(end - start, 'ms');
}

with https://github.com/2767mr/graaljs/blob/repro/graal-js/src/com.oracle.truffle.js/src/com/oracle/truffle/js/builtins/TestPrototypeBuiltins.java you can see that not everything is inlined causing a 10x slowdown on my machine (compared to recreating the prototype every time). If you use less .test calls it will inline everything but not clone the nodes, causing a 6x slowdown on my machine.

function test() {
    const start = {next(){return 1}}
    for (let i = 0; i < 10000000; i++) {
        new Array().test.call(start, i => i * 2)
            .test(i => i + 1337)
            .test(i => i % 1231)
            .next();
    }
}

for (let i = 0; i < 20; i++) {
    var start = performance.now();
    test();
    var end = performance.now();
    console.log(end - start, 'ms');
}

Graal version: c0fc1c1 using labsjdk ce 11 22.3-b02

2767mr avatar Aug 31 '22 14:08 2767mr

Hi, could you share the following:

  • OS used in testing
  • Output of java -version
  • Do you use GraalJS embedded in GraalVM or as SDK?
  • Steps to reproduce the issue

oubidar-Abderrahim avatar Sep 05 '22 17:09 oubidar-Abderrahim

  • Ubuntu AMD64: Linux user-XPS-13-9310-2-in-1 5.15.0-47-generic #51-Ubuntu SMP Thu Aug 11 07:51:15 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
  • mx java --version:
openjdk 11.0.16 2022-07-19
OpenJDK Runtime Environment (build 11.0.16+7-jvmci-22.3-b02)
OpenJDK 64-Bit Server VM (build 11.0.16+7-jvmci-22.3-b02, mixed mode)
  • I am not sure what the difference is but I used mx js to run the dev build.
  • Steps:
    • git clone https://github.com/oracle/graal
    • git clone https://github.com/2767mr/graaljs -b repro
    • cd graaljs/graal-js
    • mx sforceimports
    • mx build
    • mx --dyn /compiler,/tools --jdk jvmci js --js.ecmascript-version=staging script1.js (the first one in the issue)
    • mx --dyn /compiler,/tools --jdk jvmci js --js.ecmascript-version=staging script2.js (the second one in the isue) (Truffle AST dump args: --vm.Dgraal.Dump=Truffle --vm.Dgraal.PrintGraph=Network)

2767mr avatar Sep 07 '22 10:09 2767mr

We've fixed (worked around) this issue for iterator-helpers by force-splitting %IteratorHelperPrototype%.next.

woess avatar Nov 03 '22 02:11 woess