Single Thread Continuation

Robert Engels rengels at ix.netcom.com
Wed Jul 5 11:33:24 UTC 2023


I claim the opposite - Java was polluted when async was added - VT brings Java back to the original design. 

As for the other elements - if you have a thread first + queue design you don’t need these other features as most were added in a similar vain to Javas async support.  Generators is one of them as I’ve proven. There is nothing a native implementation would provide over what I’ve delivered. VT are continuations. It is the same construct written in the existing language features. 

If you create a kitchen sink language you get fragmentation in the community and the code bases and needlessly complicate the tools - delaying the release of critical features. Look how many releases we’ve had and VT is still in preview. This is a direct result of Javas current complexity. Don’t make it more so. 

> On Jul 5, 2023, at 12:58 AM, Paul Bjorkstrand <paul.bjorkstrand at gmail.com> wrote:
> 
> 
> Robert,
> 
> I can agree that Java/the JDK should not *focus* on features that are less broadly useful but, I challenge a statement you made at the very beginning of this thread:
> 
>> I would fully avoid all of these isolated use case. Virtual threads solves the primary need for Java - thread per connection.
>> 
>> All of these other uses can be done in other languages - don’t pollute Java with less useful idioms that solves peoples need to not have to learn CS.
>  
> Using this argument, Java didn't need virtual threads at all! They are a nicety to simplify *the code of* concurrent systems, akin to syntactic sugar.
> 
> We have had perfectly good, highly performant concurrent systems in Java for a very long time. We didn't *need* virtual threads to make it work. Sure, they are needed for thread-per-request, but thread-per-request is not needed to write concurrent systems.
> 
> Your argument "don’t pollute Java with less useful idioms that solves peoples need to not have to learn CS" could be easily extended to virtual threads too:
> 
> "Don't pollute Java with" a thread-per-request nicety to simplify the programming model for concurrent systems (e.g. HTTP servers). We already have NIO, selectors, and async programming models. Virtual threads only "solves people's need to not learn CS" by making it easier to write concurrent systems.
> 
> I can understand that *you* may not find generators particularly useful, but you are not the only user of Java (or the JDK). Yes, they *can* be written atop virtual threads as they are today (and you have provided a couple good examples of this, thanks!), but that does not diminish the value of generators (or primitives upon which to build generators) being provided by the JDK.
> 
> I wholeheartedly agree with Ron's message just prior to yours:
>  
>> However, it’s important to remember that our priorities are primarily shaped by Java’s role as a *mainstream* platform and the demands of *mainstream* usages. In other words, what drives up the priority of the feature is not its hypothetical power but its potential use in mainstream applications because that’s where most of the value is, considering the majority of Java’s users.
> 
> How does something become mainstream when it is a completely new thing not yet available? One way is by being evangelized, just like in this thread! Another way is to demonstrate the usefulness in a real life situation.
>  
> Instead of dismissing the request without any kind of discourse, I would challenge those looking for continuations or generators in the JDK to show examples of how they are useful but either not possible or extremely difficult to build in Java today. Prove they are worth the engineering effort required to build them.
> 
> //Paul
> 
> 
> 
>> On Tue, Jul 4, 2023 at 8:31 PM robert engels <rengels at ix.netcom.com> wrote:
>> 
>> 
>>>> On Jul 4, 2023, at 6:28 PM, Attila Kelemen <attila.kelemen85 at gmail.com> wrote:
>>>> 
>>>> Robert Engels <rengels at ix.netcom.com> ezt írta (időpont: 2023. júl. 5., Sze, 0:48):
>>>>> I don’t believe any of those statements are true. Even if the language supported generators directly - they are still subject to gc. The JVM needs a way to release the generator and it’s backing resources. If it did that directly - like a destructor - when it goes out of scope it still wouldn’t be able to release the other resources. 
>>>>> 
>>>> 
>>>> It is not about being subject to GC or not. My claim is the following: With JVM implementation, it could be that:
>>>> 
>>>> Iterator is unreachable -> Continuation is unreachable -> Generator is unreachable
>>> 
>>> This is exactly how my implementation works.
>>> 
>>> 
>>> The point is that in custom VT based implementation the JVM cannot know this, because a code will have to terminate the generator loop first, while the JVM doesn't have to (similarly as done in Kotlin, if I'm not mistaken). Potentially delaying the cleanup of some VT related resources is not that big of a deal. Delaying the unreachability of the generator might be.
>>>  
>> 
>> Yes it can - and it does! That is how GC languages work. Even if the JVM supported generators directly - it is still going to rely on GC for cleaning up memory resources - and a generator could use try-with-resource to clean-up other resources more quickly. Which is exactly what the implementation I shared does (the producer can use try-with-resource if it needed too).
>> 
>> Yes, there is a potential delay in when the system determines that the iterator/continuation/producer/generator is unreachable - but that is no different than any memory resource in Java. If you wanted to add a ‘close’ method to the iterator you could - for more immediate clean-up but it is not necessary. If you use the weak-reference queue you will also typically be notified far sooner than the finalizer method I used (since the finalizer needs to be scheduled and competes with other objects needing finalization).
>> 
>>>> 
>>>> You understanding of how OOM and GC works is not correct. 
>>> 
>>> Can you clarify what exactly you are referring to? My claim is that, if the generator retains a large object (like a large array), then in a VM based implementation the JVM can see that the generator is unreachable, if the iterator is unreachable. Thus, in case it needs a lot of memory, it can conclude that there is no need for OOM. In a custom VT bsed case, there is no such chance, because it can't possibly know that the generator will soon be unreachable.
>> 
>> It will know the generator is unreachable the same way it determines that any object is unreachable. If the JVM is unable to allocate memory (eg. capped) it will run full GC cycles to free memory before it triggers an OOM. If the generator (holding the memory) is still reachable then it would be in a JVM specific implementation as well - meaning the memory could not be released.
>> 
>>>  
>>>> 
>>>> My implementation does not require a queue (unless you consider a handoff variable a queue) or exceptions. 
>>>> 
>>> 
>>> Yes, I'm referring to that. You yourself called that a "hand-off queue" as well. It doesn't matter how you implement it, it is still a queue.
>> 
>> It is doubtful that a jvm native implementation would not use a hand-off mechanism - otherwise the generator would need to be 100% synchronous with zero read ahead - no one would want generators to work that way - it is far far less efficient than a hand-off or full queue.
>> 
>>>  
>>>> I think you’ll find the implementation I shared to be very efficient - and it was a super quick effort. An atomic CAS and LockSupport would make it even more so - but any complex generator will dominate the performance over the handoff infrastructure. 
>>>>> 
>>> 
>>> It is very efficient given your possibilities, but the JVM could do better, because it doesn't need a queue. In fact, it can just immediately change the context on the same carrier thread in `Iterator.next`, and doesn't even need to involve the queue for VT (let alone the extra queue a custom implementation needs atop of that).
>> 
>> See above. It is doubtful it wouldn’t use a queue as this would be slower and not using the power/performance of concurrency. The queue size is negligible compared with the other resources probably used by a complex generator.
>> 
>>> 
>>> As for the "super quick effort". Maybe, but your implementation is far from complete, as there are a lot of additional things to deal with in the general case (when you are not just inlining a simple counting loop), obviously needs some cleanup, and also any implementation that is used in serious code would require extensive testing. All of those are non-trivial effort, and there is a lot of chance for bugs, and there would be no point in forcing many-many people to go through that, when the JDK could just provide a good and efficient implementation.
>> 
>> The implementation is 100% complete, but to make it more obvious I created a new branch. https://github.com/robaho/generators/tree/complex
>> 
>> That has arbitrary generators and improves the synchronization.
>> 
>> It can generate and consume 1M values in < 350 ms or 350 nanos per operation.
>> 
>> As I said, the overhead in the framework is negligible. The generators themselves if they have any complexity or IO are going to dominate the performance.
>> 
>> 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/loom-dev/attachments/20230705/c178feb8/attachment-0001.htm>


More information about the loom-dev mailing list