offbynull / coroutines

Java toolkit that allows you to write coroutines.
GNU Lesser General Public License v3.0
346 stars 53 forks source link

Why the argument Continuation object not supported in constructor? And not set it as a field in the class #96

Open forchid opened 3 years ago

forchid commented 3 years ago

The version info

coroutines java-agent-1.5.3-shaded.jar, java 11, Ubuntu 20.04.1 LTS

The test case

import com.offbynull.coroutines.user.*; 

// 7,500,000 ops/s
public class Test implements Coroutine {

    final int times;

    public Test(int times) {
        this.times = times;
    }

    @Override
    public void run(Continuation c) {
        System.out.println("started");
        for (int i = 0; i < this.times; i++) {
            echo(c, i);
            new CoObject(c, i % 10).execute(c);
        //new CoObject(i % 10).execute(c);
        }
    }

    private void echo(Continuation c, int x) {
        if (x % 1000000 == 0 || x + 1 == this.times) {
            System.out.println(x);
        }
        c.suspend();
    }

    public static void main(String[] args) {
        int times = 10000000;
        if (args.length > 0) times = Integer.decode(args[0]);

        CoroutineRunner r = new CoroutineRunner(new Test(times));
        long ts = System.currentTimeMillis();
        for (int i = 0; i < times << 1; ++i) {
            r.execute();
        }
        long te = System.currentTimeMillis();

        System.out.println(String.format("time %sms, %d times", te - ts, times));
    }

    static class CoObject {
        final int n;

        public CoObject(Continuation co, int n) {
        this.n = n;
        init(co);
    }

    public CoObject(int n) {
        this.n = n;
    }

        public void execute(Continuation co) {
            init(co);
        for (int i = 0; i < n; ++i);
    }

        private void init(Continuation co) {
        co.suspend();
    }
    }

}

The stack trace

Exception Details:
  Location:
    Test.run(Lcom/offbynull/coroutines/user/Continuation;)V @234: JBinvokespecial
  Reason:
    Type 'Test$CoObject' (current frame, stack[0]) is not assignable to 'uninitializedThis'
  Current Frame:
    bci: @234
    flags: { }
    locals: { 'Test', 'com/offbynull/coroutines/user/Continuation', top, 'com/offbynull/coroutines/user/MethodState', top, top, '[I', '[Ljava/lang/Object;', '[Ljava/lang/Object;' }
    stack: { 'Test$CoObject' }
  Stackmap Table:
    same_frame(@32)
    same_frame(@35)
    same_frame(@45)
    full_frame(@88,{Object[#18],Object[#23],top,Object[#28],top,top,top,top,Object[#32]},{})
    full_frame(@142,{Object[#18],Object[#23],top,Object[#28],top,top,Object[#0],Object[#32],Object[#32]},{})
    full_frame(@193,{Object[#18],Object[#23],top,Object[#28],top,top,top,top,Object[#32]},{})
    full_frame(@247,{Object[#18],Object[#23],top,Object[#28],top,top,Object[#0],Object[#32],Object[#32]},{})
    full_frame(@306,{Object[#18],Object[#23],top,Object[#28],top,top,top,top,Object[#32]},{})
    full_frame(@345,{Object[#18],Object[#23],top,Object[#28],top,top,top,Object[#32],Object[#32]},{})
    full_frame(@396,{Object[#18],Object[#23],top,Object[#28],top,top,top,top,Object[#32]},{})
    full_frame(@406,{Object[#18],Object[#23]},{})
    same_frame(@416)
    append_frame(@426,integer)
    full_frame(@646,{Object[#18],Object[#23],integer,top,top,top,Object[#0],Object[#32]},{})
    same_locals_1_stack_item_frame_extended(@871,top)
    full_frame(@1038,{Object[#18],Object[#23],integer,top,top,top,top,Object[#32]},{})
    full_frame(@1044,{Object[#18],Object[#23],integer},{})
offbynull commented 3 years ago
  1. You can set the Continuation object to a field but you won't be able to use it. The Continuation object is only valid if it was created by the runner and passed down the callstack. Once it leaves the runner, the Continuation object is no longer valid, so there's no point in keeping it.

    It was done this way because it keeps things fast. If you need a more flexible but slower alternative, try tascalate-javaflow.

  2. For constructors, the bytecode for calling a constructor is different than invoking a normal method (it's a 2 step process -- first allocate the object, then invoke the constructor), so implementing support for it it is more complicated and can become wasteful.

This project is in maintenance mode. I don't have the resources to keep updating it with newer/better features. I think Project Loom will get released soon -- it might give you what you need directly in the JVM.

forchid commented 3 years ago

I think Project Loom will get released soon -- it might give you what you need directly in the JVM

But the loom project is too late, and how long do we need to wait for it's GA?

offbynull commented 3 years ago

I'm not sure, but the activity on the loom mailing list has slowed down so I can't imagine that it'll be too much longer before it enters preview. Maybe about a year or so?

They're incredibly slow people over there.

On Sun, Apr 4, 2021, 9:19 PM Peter pan @.***> wrote:

I think Project Loom will get released soon -- it might give you what you need directly in the JVM

But the loom project is too late, and how long do we need to wait for it's GA?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/offbynull/coroutines/issues/96#issuecomment-813134228, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABQPQIVAO5OKE7WOVA2U6O3THEF25ANCNFSM42K653JQ .

jalr4ever commented 2 years ago
  1. You can set the Continuation object to a field but you won't be able to use it. The Continuation object is only valid if it was created by the runner and passed down the callstack. Once it leaves the runner, the Continuation object is no longer valid, so there's no point in keeping it. It was done this way because it keeps things fast. If you need a more flexible but slower alternative, try tascalate-javaflow.
  2. For constructors, the bytecode for calling a constructor is different than invoking a normal method (it's a 2 step process -- first allocate the object, then invoke the constructor), so implementing support for it it is more complicated and can become wasteful.

This project is in maintenance mode. I don't have the resources to keep updating it with newer/better features. I think Project Loom will get released soon -- it might give you what you need directly in the JVM.

Hi, mister. You say this project is no longer support new features, which now is in maintenance model. What I wondering is if there's bug or CVE Problem, would you fix those issue?

offbynull commented 2 years ago

I would make a best effort to fix the types of problems you mentioned (no promises).

Ultimately, if you rely on this software, the best thing you can do is fork it and maintain your own version internally. It's not that complicated to maintain and enhance once you learn about JVM bytecode and the ASM library.

If the license is a problem for you / your organization, I don't mind changing it to something more permissive.

On Sun, May 1, 2022, 8:17 PM Wayland Zhan @.***> wrote:

  1. You can set the Continuation object to a field but you won't be able to use it. The Continuation object is only valid if it was created by the runner and passed down the callstack. Once it leaves the runner, the Continuation object is no longer valid, so there's no point in keeping it. It was done this way because it keeps things fast. If you need a more flexible but slower alternative, try tascalate-javaflow.
  2. For constructors, the bytecode for calling a constructor is different than invoking a normal method (it's a 2 step process -- first allocate the object, then invoke the constructor), so implementing support for it it is more complicated and can become wasteful.

This project is in maintenance mode. I don't have the resources to keep updating it with newer/better features. I think Project Loom will get released soon -- it might give you what you need directly in the JVM.

Hi, mister. You say this project is no longer support new features, which now is in maintenance model. What I wondering is if there's bug or CVE Problem, would you fix those issue?

— Reply to this email directly, view it on GitHub https://github.com/offbynull/coroutines/issues/96#issuecomment-1114456606, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABQPQIQO4BGXPFXGRZRSB7DVH5CLNANCNFSM42K653JQ . You are receiving this because you commented.Message ID: @.***>