Giter Club home page Giter Club logo

Comments (6)

lukasstadler avatar lukasstadler commented on September 13, 2024 1

The false sharing is fixed with cb7b3fd.

Indeed, the context initialization should be faster, at least after a couple of warmup runs.
There's some issues around how classes are handled when multiple contexts are used, I'm taking a look at that at the moment. One effect of that is that the compiled code size gets very large, which causes the high load on the compiler threads you're seeing.

from graalpython.

lukasstadler avatar lukasstadler commented on September 13, 2024

Hi!
The false sharing between the context is definitely a bug, we will take a look at that.
It was likely introduced with some recent changes for sharing ASTs between contexts.

The context creation should get significantly faster after a couple of contexts have been created and the JVM had some time to warm up.
You could also try and create a native image for your Java application, which removes the JVMs warmup altogether.

from graalpython.

revintec avatar revintec commented on September 13, 2024

@lukasstadler Hi, thx for the quick reply

my use case is like the CGI model:
the Java program acts like a HTTP server,
when a request is received from client it creates a new context,
some python code runs inside the newly created context and generates a response

as you can see, creating a new context each time is kinda essential so that different requests won't get mess up, but as I've discovered that creating many short-lived context is very heavy, maybe this is not how contexts're supposed to be used? how can I do better?


I've created a new file for a quick poor-man's profiling:
it runs very slow, GC, mem and CPU are all very high
after 4 minutes, it still costs about 400ms each run

import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Engine;
import org.graalvm.polyglot.Source;

/** Created by revin on Oct.1,2018. */
public class BugReport{
    public static void main(String...args)throws Exception{
        Engine engine=Engine.create();
        Source source=Source.newBuilder("python","pass","bugReport.py").build();
        for(int i=0;i<0x10000;++i){
            long nano0=System.nanoTime();
            Context.newBuilder().allowIO(true).engine(engine).build().eval(source);
            System.out.printf("%7d ms%n",(System.nanoTime()-nano0)/1000_000);
        }
    }
}

the following images are gathered from Java Mission Control

image
image

from graalpython.

revintec avatar revintec commented on September 13, 2024

@lukasstadler Any update on this?

from graalpython.

lukasstadler avatar lukasstadler commented on September 13, 2024

Uh, sorry that I didn't provide an update on this.
With the latest release, context initialization time goes below 100ms after a while on my (rather slow) machine, so much better but still not perfect.
There may be theoretical limits on how fast this can be, since context initialization does lots of things.
Starting up a context in CPython (time python -c 'print("foo")') takes ~65ms.
I guess the quicker the better, but what amount of time would make sense in your case?
Are the scripts very short-lived?

from graalpython.

revintec avatar revintec commented on September 13, 2024

@lukasstadler thanks for your reply, I'll redo my tests asap with the newest version

my use case is like the CGI model:
the Java program acts like a HTTP server,
when a request is received from client it creates a new context,
some python code runs inside the newly created context and generates a response

the scripts are indeed very short-lived, ideally all of them are less than 100ms
I'm currently using thread-local cached contexts, but that is a compromise I think, because some scripts may leak to or even change global scope which may affect other following scripts or cause memory leaks. using a fresh context each time and close each of them after each script has been run should be cleaner I think

I'm not very familiar with python (especially its initialization process), but after reading (some of) the source code, it seems that a lot of initialization is done when the python context is being created. maybe we can remove some of the code, or is there a way like to create a templateContext and shallow copy them to newly created contexts? without affecting context isolation of course

from graalpython.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.