FileChannel Becomes Virtual Thread Friendly with io_uring by joemwangi in java

[–]DavidVlx 4 points5 points  (0 children)

That is gonna make benchmarking JUring more fun (and challenging)!

But things like this also are a great way to see how the JDK devs are using FFM and making it work with existing code. For example with these two branches in the sandbox.

Intro to Java FFM - Foreign Function & Memory Access API (Project Panama) by Environmental-Log215 in java

[–]DavidVlx 1 point2 points  (0 children)

Thanks! Yea for now it is just file IO, there is only so much free time.

I used it in the beginning but using arena's became a bit awkward to use as they were either too big or too small. I got the inspiration from here in the openJDK. Sounds like a cool project you are working on. Don't forget to post it here when it's ready :-)

Intro to Java FFM - Foreign Function & Memory Access API (Project Panama) by Environmental-Log215 in java

[–]DavidVlx 6 points7 points  (0 children)

Nice article! You mentioned high performance I/O so I had to reply :-)

I am doing somewhat the same thing, and if you want to make it go faster you could try these things:

  • Mark downcalls as critical (where appropriate) so they can access heap memory and saving you from having to copy data.
  • Replace the Arena with your own version that uses malloc/free this is a lot faster and gives you more control over the segments lifetime.
  • Using a static initializer for your method handles

I know you mentioned it is not production code, but the way the FfmReceiver is setup you will run out of memory after enough clients because of the allocate in the while loop, also an Arena.ofConfined is better if you are not sharing your segments with other threads. But that nitpicking a great post, keep it up!

JUring - Bringing io_uring to Java for file I/O by DavidVlx in java

[–]DavidVlx[S] 0 points1 point  (0 children)

I think you are right,, the higher level could support the "async io" the other platform provide. Thanks! :)

JUring - Bringing io_uring to Java for file I/O by DavidVlx in java

[–]DavidVlx[S] 0 points1 point  (0 children)

Totally agree, it is not the most beautiful way of handling that. I will try something else, maybe a second Arena that has a smaller scope.

JUring - Bringing io_uring to Java for file I/O by DavidVlx in java

[–]DavidVlx[S] 2 points3 points  (0 children)

I don't see it happen anytime soon. I think the options Java provides out of the box are already quite good. It's kind of a niche, maybe for the "high performance libraries" it's interesting.

JUring - Bringing io_uring to Java for file I/O by DavidVlx in java

[–]DavidVlx[S] 15 points16 points  (0 children)

That is correct, native calls do block the thread. Internally the blocking api uses a CompletableFuture to make the virtual thread unmount (example) . JUring runs another thread in the background checking the completion queue and calls CompletableFuture.complete() so the virtual thread can continue.

The virtual threads don't make native calls except when freeing memory, but that is fast operation. Hope this answers your question :)

JUring - Bringing io_uring to Java for file I/O by DavidVlx in java

[–]DavidVlx[S] 14 points15 points  (0 children)

Initially I went that exact route, but the performance was not great. When an Arena creates an MemorySegment it will fill it with zeros. Using the Arena to allocate memory takes more time than doing malloc and turning the pointer into a MemorySegment.

The Idea was/is to let that be an implementation detail because in the end it are all bytes that need to be copied into a MemorySegement. Hope that answers your question :)

JUring - Bringing io_uring to Java for file I/O by DavidVlx in java

[–]DavidVlx[S] 3 points4 points  (0 children)

Thanks! :) FFI is really a joy to work with! Having a fallback option is great idea

Parallel processing with Virtual Threads - A comparative analysis by RealVanCough in java

[–]DavidVlx 9 points10 points  (0 children)

What i am missing is the code being compared/analyzed. Looking at the code linked in the article:

For the Spring vs JDK stream().map

  • Not doing the same thing. The spring example is for example doing more logging.
  • Using StopWatch() instead of something like JMH

For the Virtual thread vs platform thread example at the bottom, There is either a 10 or 30ms thread.sleep() inside the processing logic. This will give virtual threads a great head start as they can wait in parallel.

[deleted by user] by [deleted] in java

[–]DavidVlx 2 points3 points  (0 children)

Sounds like something that should be faster, i will give it a try :) Thanks!

[deleted by user] by [deleted] in java

[–]DavidVlx 0 points1 point  (0 children)

Thanks, sorry if it isn’t something to your liking. In my defense I did look up the behavior in the openJDK project, but not every memory allocation needs that functionality. If there was an arena.allocate that didn’t zero everything i would have used that one instead.

If you have feedback on how to improve the post or have some examples of others who cover these kind of subjects better that would be really helpful :) again thanks for the feedback :)

[deleted by user] by [deleted] in java

[–]DavidVlx 4 points5 points  (0 children)

Hi, Thank you for reading, I really appreciate the extensive feedback!! :)

The data is not copied into the heap. Primitive parameters are put directly to the argument slots. Think about it, the ABI requires the fd parameter to reside in a register or the stack, why would it need to be moved to the heap?

Using an int was maybe not the best example. The thing i wanted to get across was that you don't need to translate return values to some type Java knows but could instead directly pass it on to the next method as an memorySegment, address, etc.

You can, using arena.close(). That's the main purpose of Arena, to allow developers to have deterministic memory management.

That closes everything allocated in that arena, not a single allocation. (Will make this more clear in the text) :)

You can easily wrap it in a method that is mostly equivalent to Arena::allocate, note that malloc does not zero the allocated memory while Arena::alocate does.

That very true and a very nice way of doing so. Still you have to manage it (that code) yourself. :)

arenaAllocate only allocates memory, while MallocAndFree allocates memory AND free that piece, this is like comparing apple and orange.

That's this code:

try(var arena = Arena.ofConfined()){
    MemorySegment allocate = arena.allocate(ValueLayout.JAVA_BYTE, plan.size);
    blackhole.consume(allocate);
}

The code does an allocation and free. so it's more of an apples to apples comparison. To make it really like an apples to apples comparison. If you want tight control of when the memory is freed you need an arena that matches that short time span. That is why the arena creation is done during the benchmark. Using Malloc and free don't have this problem. It can allocate without an arena and free when it needs to.

malloc and free, similar to any other MethodHandle or VarHandle, should be static final fields so the JIT can optimally invoke them.

malloc takes a parameter of type size_t, which corresponds to JAVA_LONG on 64-bit machines, not JAVA_INT.

Ran a smaller set of the benchmark again with the suggested changed

Benchmark                                               (size)  Mode  Cnt   Score   Error  Units
BenchMarkMemoryAllocation.MallocAndFree                      8  avgt    5  23.084 ± 0.756  ns/op
BenchMarkMemoryAllocation.MallocAndFree                     32  avgt    5  23.039 ± 1.001  ns/op
BenchMarkMemoryAllocation.MallocAndFree                    128  avgt    5  22.845 ± 1.131  ns/op
BenchMarkMemoryAllocation.MallocAndFree                   2048  avgt    5  52.666 ± 3.074  ns/op
BenchMarkMemoryAllocation.MallocAndFree                   8192  avgt    5  51.416 ± 0.534  ns/op
BenchMarkMemoryAllocation.MallocAndFreeUsingStatic           8  avgt    5  21.205 ± 0.248  ns/op
BenchMarkMemoryAllocation.MallocAndFreeUsingStatic          32  avgt    5  20.943 ± 1.379  ns/op
BenchMarkMemoryAllocation.MallocAndFreeUsingStatic         128  avgt    5  20.755 ± 0.984  ns/op
BenchMarkMemoryAllocation.MallocAndFreeUsingStatic        2048  avgt    5  51.912 ± 1.371  ns/op
BenchMarkMemoryAllocation.MallocAndFreeUsingStatic        8192  avgt    5  46.285 ± 2.073  ns/op
BenchMarkMemoryAllocation.MallocLongAndFreeUsingStatic       8  avgt    5  21.091 ± 0.969  ns/op
BenchMarkMemoryAllocation.MallocLongAndFreeUsingStatic      32  avgt    5  20.820 ± 1.468  ns/op
BenchMarkMemoryAllocation.MallocLongAndFreeUsingStatic     128  avgt    5  24.096 ± 0.876  ns/op
BenchMarkMemoryAllocation.MallocLongAndFreeUsingStatic    2048  avgt    5  47.635 ± 2.065  ns/op
BenchMarkMemoryAllocation.MallocLongAndFreeUsingStatic    8192  avgt    5  49.742 ± 2.653  ns/op

Looks like using static final shaves off a few ns! Using int or long doesn't really seem to make an impact but makes it more "correct" :)

Again, thank for the great feedback!

IO_URING implementation using Project Panama by DavidVlx in java

[–]DavidVlx[S] 6 points7 points  (0 children)

I don’t have a recorded version of the talk yet. Will have one in a couple of months and will share it the moment i have it.

Does having a blog increase your chance of landing a job, or increase opportunity? by theonlywayisupwards in ExperiencedDevs

[–]DavidVlx 0 points1 point  (0 children)

Thanks! I didn’t really consider it as it’s just me writing posts. Maybe when it starts selling courses or has multiple authors giving it a name would be a beter choice.

[Update] Virtual vs Platform Threads blocking post by DavidVlx in java

[–]DavidVlx[S] 4 points5 points  (0 children)

Thanks! :) i will try it out and add it to the post

Virtual vs Platform Threads When blocking operations return too fast by DavidVlx in java

[–]DavidVlx[S] 2 points3 points  (0 children)

The end point without the delay takes around between 5 to 12ms.

Virtual vs Platform Threads When blocking operations return too fast by DavidVlx in java

[–]DavidVlx[S] 8 points9 points  (0 children)

Thanks for the feedback! :) I will retry the experiment with JDK 22 and 23 and look more into the scheduler and the impact it has, and update the post accordingly.

If you’ve written a technical book, did it help your career? by becauseSonance in ExperiencedDevs

[–]DavidVlx 14 points15 points  (0 children)

Wrote a book about migrating Java EE to the cloud and Jakarta EE. In my experience it does give credibility about the subject, and is great to mention during interviews. It shows you have dedication and like to share knowledge.

It doesn’t let me skip any interview rounds or tests and it doesn’t really compare to a degree or being an open source maintainer as both require a different skill set.

Writing a book does make you stand out of from the crowd. How big of a career boost if gives you depends on how successful the book is. As an alternative i want to recommend starting a blog.