Java 20: A Sneak Peek on the Panama FFM API
minborgsjavapot.blogspot.com
minborgsjavapot.blogspot.com
---
One of the coolest things that's been worked on (by this same author, no less!) isn't even in the article!
Per has written a pretty-printer for MemorySegments and ByteBuffers that can hex-dump memory or render memory/buffers as their "struct" representations given some MemoryLayout.
You can also customize it with your own printers, it's wicked cool and helps so much to debug buffers when working with them.
https://github.com/openjdk/panama-foreign/pull/695
What do I mean by that? Have a look at this comment for an image of a raw memory structure layout I represented with MemoryLayout, and the pretty-print rendering of it:
https://github.com/openjdk/panama-foreign/pull/695#issuecomm...
It looks like something I'd expect to either be an external tool (like VisualVM) or part of an IDE
Except when they use @Autowired. (Ackshually aside, I do agree with the sentiment of your post. The standard library is arguably one of Java's greatest assets)
When I saw your top-level comment, I must have mixed up your name with a different Gavin [1] until a quick Google search fixed the mix up :)
1: Gavin King of Hibernate fame.
Sorry for the tangent, it had to get out somewhere and I did not think it was worthy of an email in the actual review thread : - )
And how does this all play with GraalVM native compilation? Could I still compile a native executables after using this FFI?
Then you could go full circle and get a native exectable that's cosmopolitan ...
So, not sure that the same version can be linked, but most libraries have a shared API with platform-specific implementations and one can just link against them, pretty much how it is done today in e.g. Blender.
GraalVM is a bit trickier as it can be used to interpret LLVM bytecode, which will be portable across OSs, both when native compiled or when used on top of JVM. But if it uses Panama it would get the aforementioned limitations as it is basically just a wrapper over dynamically wrapping libs.
As always, thanks for the work!
The last time I poked at it, there were just so many config and generated files and so much complexity that it felt like I must use an IDE that hides a layer of “magic”.
Or am I just confessing ignorance and this isn’t a thing and it’s no more complex and magical than Rust or JavaScript or Python?
Edit: thank you all for the helpful responses.
It's still a fairly verbose language, but it's miles from where it was 10 years ago.
I’m kind of excited now to try it again.
But to actually provide an answer, records shorten the language to a great degree in my opinion, there is also type inference with `var`, so on a local scope you can write really short code. The main method is still chatty, but it’s not like you actually have to write all that many times, plus it is just psvm for most IDE users.
...Although I will admit that whether its feasible or useful to demystify the magic varies from case to case.
As for Java, an IDE helps a lot for Java because the build landscape is quite tedious otherwise.
In terms of complexity, it really depends on what you want to do. Using a powerful framework like Spring can entail some config (albeit much less than it used to). Also you would typically use a build tool like maven or gradle to manage dependencies, build artifacts, etc.. Especially maven can be a little arcane. But another major upside of the aforementioned maturity is that there are reams of docs and stackoverflow answers which will almost certainly contain solutions for any problems you might encounter.
This is both a blessing and a curse. Some answers and docs are decades old and although still work, are not “fashionable java” (think streams, for instance).
Has that gotten better recently? It's been years since I've toyed with it.
A lot of cruft is still there; Date, File, InputStream/OutputStream, sun.misc.Unsafe, and the entire legacy reflection API are the bunch that stick out to me. Newer releases are actively mentioning their legacy status in documentation, and there are certainly JEPs to remove them or make them opt-in/opt-out.
If you look at OpenJDK internals, or even the foreign function/memory APIs as described in the article, it looks almost nothing like the Java I learned on and wrote for several years. Most of the Panama API is piggybacking on additions to the language made post-JDK9 or so, such as MethodHandle, VarHandle and @PolymorphicSignature methods. So I'd say the language and runtime is evolving nicely, even under the thumb of Oracle.
I think I missed the announcement of those being "legacy" APIs? They show up all over the place including in libraries, what is the replacement?
Did I miss something somewhere ? java.lang.reflect is still the standard way of doing Reflection.
This is it. There are things which are better in Java than in the other ecosystems you mentioned (e.g. dependency management) and there are things which are worse (the language is more chatty, maybe the designs tend to be more convoluted, but this differs project to project). Even enterprise Java has come a long way, it's different to what it has been a decade ago, it's definitely more lean.
Every sane developer uses either Jetbrains' product for JS, Python or Rust, or they use VS Code, which, after installing all plugins to reach the same productivity as with Jetbrains, becomes an IDE as well.
You seem to vastly overestimate the usage of IDEs with scripting languages. In my experience, the vast majority of developers working with scripting languages such as JavaScript, Python or Rust, still use plain text editors such as Vim or Sublime Text. They find these editors to be more convenient and faster than IDEs.
Even people who are on VS Code seem to be using only the built-in language support for scripting.
one of these, is not like the others :)
> They find these editors to be more convenient and faster than IDEs.
FWIW, survey responses from 2021 imply that 54% of Rust devs use VSCode, 28% use a vi variant and 21% use IntelliJ[1].
[1]: Page 19, https://raw.githubusercontent.com/rust-lang/surveys/main/sur...
Definitely not in my experience, most people I know use VSCode, which I wouldn't call a plain text editor (not necessarily an IDE either though). Most people will use the plugins for each language since those are auto-recommended when you open a new file in that language, such as Pylance for Python, and most people will click "install recommended plugins" and call it a day.
Vim users are few and far between in my experience, and Sublime Text even more so, most everyone has migrated to VSCode at this point that I know of.
https://lp.jetbrains.com/python-developers-survey-2021/#Deve...
State of JS doesn't seem to include the question any more, but it found in 2019 that about 70% used one of the two (mostly VSCode).
https://2019.stateofjs.com/other-tools/
VSCode may not be a full IDE without extra plugins enabled, but it's a far cry from a plain text editor.
And emacs users never had a plain text editor, never claimed as such, and woe be to you if you call it such in front of them. Lol.
But surely, for advent of code scripting you don’t need it, but neither does Java.
There are all kinds of things that you can pile on top of it, usually for managing big projects. Those can seem arduous when you're at the Hello World level. If you got introduced to those it will seem daunting and unnecessary (and for many projects, it is).
But basic Java is no more onerous than Python or JavaScript. About the only difference at that scale is that Java requires type notation, but no more so than C.
And I wrote Java for over a decade with vi. IDEs make it better, but they are not necessary. (And good ones are free anyway.)
And, yes, I agree 100% about your reference to low cost developers. From an enterprise view, it is cheap and easy to find (low quality and) low cost developers to maintain legacy Java software.
You also wrote: <<... static typing allows for deterministic refactoring of bad code. That's something you can't easily do in JavaScript.>> Most JavaScript devs would immediately reply: "Oh, use TypeScript for that." As I understand, you can incrementally upgrade a legacy JavaScript project to TypeScript, then slowly add types. Also, I have refactored lots of Java code that uses Object as a substitute for C's void*. That type of code is so difficult to understand without a debugger. Also, very difficult to refactor due to limited typing.
- Pattern matching
- Sealed types
- Records (immutable, succinct data-classes)
- Multiline strings
Among other things. It's almost as nice to use as Kotlin, and I say this as a huge Kotlin fan. Its pattern matching in most recent releases is actually more powerful than Kotlins, since it allows deconstruction-bindings in patterns.
Since Java's history is very focused on not breaking things (and folks got really grumpy on the few occasions something has been broken), Java cannot ever match the feature pace of languages like C# either.
But the folks writing Java code (most of you here on HN by all metrics), mostly don't care. They enjoy backwards compatibility and stability far more than new features. The overwhelming majority of newly written Java code is targeting Java8 still - which doesn't use any of these new features anyway.
Backwards compatibility is also why things like lambda's and streams are the way they are in Java (and most FP folks would sneer at), and things like Records took years to release (still technically in the Preview stage).
The advantage Kotlin has, and the reason it's growing in popularity in backend development is you get all the FP tasty features you want right now, but maintain near drop-in compatibility with existing Java libraries and frameworks. This was the most brilliant move by the Kotlin designers in my opinion...
What Kotlin features are possible because of that freedom ? Genetics can be an example I can’t say it’s impossible for Java to add in out refied keywords
That's by deliberate choice. They are going down the route of having the last mover advantage. Lots of people have complained about C#'s kitchen sink type of approach for example. And the more features you have, the more chance that they won't fit together neatly and cleanly.
A couple of examples that come to mind, Java's pattern matching/destructoring and upcoming string templates (JEP 430) are much more fully fledged compared to their Kotlin counterparts.
It also has the advantage of being the platform language. Now that we have virtual threads on the JVM, async/await immediately becomes irrelevant. However, Kotlin now has to maintain coroutines even though the platform offers a better way of doing things.
The amount of syntax I see literally 1:1 taken over by dozen other languages is really impressive and evidence for that.
A lot of its features for functional programming got swallowed up in later C# versions, and it's the Red-Headed Stepchild of the CLR (let's pretend VB.NET doesn't exist).
MS allows it to exist but it's a second-class citizen on the CLR, and, I think, always will be =(
And F# was created as a research project and is a lot more community driven than C#. So if Microsoft starts putting more people onto that, I think a part of that community will also scream that they should stay out.
I know those numbers, and can even refer to the blog post they were presented, if a big company like Microsoft wants people to use language XYZ, they can make it happen if management cares about it.
However they aren't caring enough to sort out the GUI civil war, so why would the .NET languages by any different, regarding coherent management across the board.
That would be the case for pretty much any language that has lots of features (provided that most of those features aren't completely terrible). A good design (at least for a mainstream language) also strives to minimise the number of features, not adding everything you can but only adding things you absolutely need. In the case of C#, many of its features are not widely adopted, and Java in particular will never adopt them.
Java's philosophy has always been to wrap an innovative runtime in a conservative language that only adds features late, after they've proven their worth elsewhere and when most mainstream programmers are ready for them, with the realisation that every added feature adds a cost to learning the language. That strategy has worked very well for Java, but .NET has a different strategy (a more adventurous language built on top of a less adventurous runtime). We'd rather spend two years thinking about how to avoid adding a new language feature than spend six months adding it. This "last-mover" strategy has helped us maintain our philosophy and keep the number of features small compared to C#. We strive to only add features that have a big bang-for-the buck and that, preferably, solve many problems at once, rather than add many features, each addressing a relatively small problem. For example, virtual threads have allowed us to avoid adopting async/await; records are allowing us to avoid adopting properties, and will likely help us avoid adding named and default parameters as a separate feature (which, in languages like C#, Swift, and Kotlin breaks binary compatibility/separate compilation).
Very cool. Is there any material available about this for further reading?
Thank God. Don’t get me wrong, C# is a cool language but languages suffocate under so many features, most devs will simply never ever learn most of them. And some of these features while invaluable in certain rare cases, I would rather prefer a better, more fundamental feature that takes more time and single-handedly solves for many situations.
Unfortunately modern languages really like to throw in everything they have seen from other languages ever, and even with very smart language designers these features will inevitably tangle (khm, swift).
CsWinRT and win32metadata are still quite far from a great usability experience.
I recently wrote a database buffer pool with it, allocating raw aligned memory and directly casting bytes from DB files into in-memory struct pointers.
It's very nearly a systems language in terms of capabilities.
Java 8 users are now a minority. Some libraries still target it (although Spring 6 targets 17 as the baseline), but most Java code is in applications.
> Java can never be Kotlin
Java doesn't want to be Kotlin because it aims at a much wider audience. Back in the nineties James Gosling laid out a strategy of an innovative runtime wrapped in a conservative language that only adds the most beneficial features, and only after they've been successfully tried in other languages, and this has worked very well.
Also, the Java language has the advantage of controlling the runtime, which allows us to add a smaller number of features, which are more powerful (e.g. compare records with data classes, or virtual threads with syntactic coroutines), while Kotlin can neither change the runtime nor have a significant impact on the ecosystem, and so is more limited in what it can achieve. Moreover, Kotlin's features are increasingly mismatches with the evolution of the platform.
Nevertheless, we are happy that the platform offers more feature rich languages to the minority of developers that do prefer them (or languages with radically different approaches, such as Clojure), so the platform can cater to programmers with different language preferences.
The last things missing are properties (which should be coming with reconstructors), and nullable types. For me, nullable types are not worth the switch.
All that being said, I don't think the languages are mutually exclusive. As a JVM developer you retain all your knowledge of the JVM and it's standard library. Lastly, a language doesn't have to be the dominate language on a platform to be usable and employable (just look at Scala).
I think the whole config/generated files thing is for old-school J2EE "enterprise Java". Most of the frameworks I've used have just involved writing code. Sure, sometimes you need a config file, but that's pretty normal -- most of the time I'd rather specify things like port numbers and logging configuration in a separate config file, rather than hard-code them in the app themselves. If you stay away from the old-school Java Servlet frameworks -- or use a framework that builds on top of servlets and hides the complexity -- then you don't have to deal with that crap.
The build ecosystem is a bit more fragmented than Rust (maven or gradle or sbt or...), and the build tools do require a bit more boilerplate to configure, for the most part (especially maven), but it's not too bad. JavaScript, IMO, has some of the most inscrutable build systems I've ever seen, so I don't think that's comparable to Rust, or even Java. Maybe the complexity of Java build systems is similar to Python, though in different ways.
The recent releases of Java have added a lot of quality-of-life improvements to the language, so it's a lot more pleasant (and less verbose) to write Java than it used to be. I still prefer something like Scala (2; haven't tried 3 yet), but it's a lot easier to build a team around Java than Scala.
I worked take a second look at IntelliJ and Eclipse; learn the keyboard shortcuts. Modern Java is incredibly well put together.
Most of the tooling is provided, and it's coherent and robust.
Ultimately - both Maven and Groovy, which are used for production level build and packaging, are a 'weird'. But getting going with them is relatively easy.
Java has introduced the concept of 'Modules' - which if you don't get straight in your head, can be confusing, but ultimately it's not that bad.
Compilation is relatively fast as well.
The 'IDE' tends to 'hide a lot of things' in ugly ways - that is true. All of the JAVA IDEs are weird about that. However - because Java is a well maintained language, the IDE's can be powerful and are actually worthwhile. Jetbrains being one of them.
Sadly, Java on VSCode is a bit of a mess.
On Android, Google has introduced a bunch of odd conventions and complexities which I feel are unnecessary.
If anything I’d say JS has a more complex configuration story than Java.
I'll take that a a major plus. Sometimes things are improved by what is taken away.
> I don't see any real use of Java in 2022 and beyond.
Well, you see what you see; and the cloud infrastructures sees what it sees.
Also, sprint is not slow at all, especially that servers are very famously IO-heavy. Nonetheless, you can expect better performance for these with project loom.
So the API assumes pointers can't be larger than 64 bit?
One of the things that jumped out to me about that changes, was that I assume that this will allow for unsafe pointer math. I suppose this is a low level api, but I thought that was an interesting choice.
Not many machines currently, but CHERI has 129-bit pointers (yes, 129), and there's already some work on for instance Rust to be able to work with that (see https://faultlore.com/blah/fix-rust-pointers/ and https://faultlore.com/blah/tower-of-weakenings/), and also on GCC and glibc (https://lwn.net/Articles/909265/), and it's not completely improbable that this becomes more common. Creating a brand-new API today which cannot handle pointers wider than 64 bits seems a bit short-sighted.
See this paper for details: https://www.cl.cam.ac.uk/research/security/ctsrd/pdfs/201904...
2^64 can address over 16 petabytes. I’m not sure when I’ll be working on that scale, but n by the time it happens, Java can introduce new data types like BigLong to handle it.
https://github.com/GavinRay97/panama-liburing/blob/6a80673e7...
In Java 19, there was a notion named MemoryAddress used for “pointers to memory” and function
addresses. In Java 20, MemorySegment::address returns a raw memory address in the form of a
long rather than a MemoryAddress object.You can see that a bit further down below when ".toRawLongValue()" is called:
long iovecs_ptr = fi.address().toRawLongValue() + file_info.sizeof();
liburing.io_uring_prep_readv(sqe_ptr, file_fd, MemoryAddress.ofLong(iovecs_ptr), blocks, 0);
liburing.io_uring_sqe_set_data(sqe_ptr, fi.address());
int ret = liburing.io_uring_submit(ring_ptr);Finally had somewhere to post this obscure project, haha
I thought a potential CPU of the future could have 128 bit "word size" for integer calculations and as a side effect also 128 bit pointers to reuse registers/instructions for both of them (just like today with 64 bit CPUs, indeed you don't need to map the entire address space but pointers are still 64 bit because it's the CPU's word size). And then address space layout randomization in a future OS could choose to map very high address ranges for the process, which can't be represented with Java's long.
Things have gotten exponentially larger in the meantime... with your average home computer going from < 1GB of RAM to 8GB and ever increasingly larger (my cell phone has 12GB of RAM and 512GB of storage for what it's worth).
Having storage ranging in the Terabyte category is simply the norm, and things like Petabytes of storage are rapidly becoming more accessible for average users.
Saying "X is enough" - ever - in computing is foolhardy.
This is not true, no one seriously thought 65 KB was ever going to be any sort of a limit. If you're talking about the Bill Gates quote, it's basically a myth that never happened or was taken WAY out of context. Bill Gates himself has given interview where he talks about the entire state of released and upcoming computers of that time and exactly what everyone thought was going to happen and what actually did happen.
Every time someone in tech has said "that number is large enough", the number gets surpassed in no time.
So while 64 bit memory addresses seems way more than ample today, it will not be forever. That was the point...
It truly is in so many cases. Not stupidity, but perhaps hubris. The same hubris that says nobody will need more than 64 bits today. Given time, 64 bits will fall waste to 128 bits and more.
The people who designed IPv4 truly thought the number of IP's would be more than enough forever. They literally had no concept that one day your average person would own multiple computing devices and require multiple IP's just to get through a normal day. They were very wrong, and found out in only a few year's time.
The only constant in tech is our assumptions today will be wrong tomorrow. Assuming "X" is big enough is foolhardy in just about every scenario.
Every year was another iteration of these limits being updated, no one ever thought any of these numbers were going to stay static. It took 1MB of memory just to have a 24 bit color frame buffer at NTSC resolution.
IP addresses were set at 4 billion when you could print out a list of known computers and put it in a binder and we still use it today.
This idea that people had a general lack of foresight was completely untrue. What people did with limitations like 3,510 transistors is astounding.
That said, I completely agree with your sentiment about "X is enough". What has changed in the last 20 years: When we double RAM/storage/network now, the increase is enormous. It does feel like GPUs are still in the insane growth period. Very soon they will be more powerful and have more on-board RAM than whole rest of the computer.
* As for photons, this estimate finds 4*10^84 photons since start of universe:
https://cosmosmagazine.com/science/physics/how-many-photons-...
Maybe we need to use 1024-bit types?
The size of memory is a power of two because the bits in a pointer have two possible values.
But why do you need the length of a pointer to also be a power of two? Are you addressing the bits in the pointer?
Indeed. The entanglement code will crawl without easy direct addressing of the bits. We wouldn't want the universe to run even slower, would we now? It would also make the development emulator impossible to run leading to even more bugs and missed deadlines.
6502: 8-bit registers / 16-bit address space (1975-)
68000: 16 / 24 (1985-1992 then 32/32)
ARM1: 32 / 26 (1985- 32/32 in 1992 and finally 64/64 in 2011)
X86: a complete address space mess but lately 57-bit:
https://en.wikipedia.org/wiki/X86The morale of the story is humans are insecure fragile little creatures, half of us are blind and the other half are pessimists because we can imagine what the truth/future looks like. Lies only last the time you can use energy to hide them.
Anyhow the arch of binary evolution is over; to build a MMU that can adress 64-bits of data is probably impossible with electrons. Just going from 64GB to 256GB is hard and makes things slower! Caches appeared in 1985, so since then the bottle neck has been RAM latency:
https://gettotext.com/largest-ram-bar-in-the-world-consumes-...
The biggest server from Oracle currently has 16 TB of RAM, good luck multiplying that by the difference in energy content per mass difference between oil and uranium (1.000.000x).
And good luck paying your energy bill. 64-bit RAM would consume 5120W * 1.000.000 = 5GW (you want 5x nuclear power plants with that server?)
Personally I'm staying on 32-bit for my ARMv8 cluster, my processes are limited to 4GB RAM each but since I only have 8GB of total RAM there is ZERO difference in switching to 64-bit as it effectively halves the RAM.
I made a comment about the virtual memory space of most desktop CPUs in use. I have no idea what you are talking about, did you mean to reply to me?
Anyhow the arch of binary evolution is over;
I don't think these words together mean anything.
to build a MMU that can adress 64-bits of data is probably impossible with electrons.
It's strange to call something impossible when it has already been done.
More likely: If 128-bit address space really takes off, they can create a new largest integral type long128 that is 16 bytes/128 bits, and either extend the existing MemorySegment, or create MemorySegmentLarge to allow for bizarrely large address spaces, like up to 1024-bit.
Another one that people like to pick on Java about: Arrays are limited to max size of 2^31-1 (non-negative part of a 32-bit signed int). To rephrase your question: <<So the API assumes arrays can't be larger than a 32 bit signed int?>> Yeah, I guess.
Point is, your reply is exactly right. Arrays can't be 64-bit large? Yeah, I guess.
I thought of something like IntPtr in C# (native size int). Although, to be portable, JVM would have to disallow doing pointer math on it, to make IntPtr fully opaque.
Plus Panama won’t go stable until Valhalla, which is necessary to actually have a “long long” in the language.
From the link:
> Oracle's sales arbitrarily selects some feature versions for which to offer an LTS service, and other companies follow their choice
This does not seem to be strictly true. The release cadence is scheduled, with mostly predictable release times.[1][2]
The idea was, if some feature didn't make it's way into this release, it'll just get into the next release (which is only a few months away). Very similar to how the Linux Kernel does releases - arbitrary in features, but not arbitrary in time.
And while Oracle may retain a great deal of influence over the OpenJDK project, Oracle is not the OpenJDK project anymore. There are a lot of other voices to be heard that decide when and what is done.
> BTW, [other companies] can choose to offer LTS even for releases that have already been made and retroactively make them "LTS releases." There's absolutely nothing special about them
He's right about this. However, the reality is most companies offer the same LTS as was scheduled, with extra paid support for anyone willing to pay (and be stuck on) a regular release.
[1] https://www.java.com/releases/
[2] https://www.oracle.com/java/technologies/java-se-support-roa...
While there is surely (very welcome) other voices as well, I am not sure I can agree with this statement given that 95+% of OpenJDK development is done by people working for Oracle.
For _many_ enterprise users, there is indeed something particularly special about the LTS releases. Even if it's just a marketing term, it's still important to realize that organizations value stability, and the LTS releases can offer that at a different comfort level (and price point).
Yes, LTS was dreamed up as a way for Oracle to make money on open source Java. But the other non-Oracle vendors (equally important to the ecosystem) very much have similar offers of support that Oracle provides.
I'm glad for the model, frankly. And I'm excited about Java's future. LTS releases helps smooth out discussions with CTO types about how to live with Java. Developers can use the latest-greatest and be sure to target a particular LTS version for deployment. Finally Java seems to be moving again.
When pron comes in here and smacks us down with his replies, that's OK, I'm comfortable with my perspective. :)
And glad you are excited about the future of Java and the model itself! Most of this work is funded by the LTS model which was designed to give companies the flexibility to decide their upgrade strategy. Lots of teams want to move quickly and get new features, while others just want the app in the corner to be stable for years to come.
Unless your business is keen on upgrading the JDK every 1.5 quarters it’s simply easier to rely on LTS versions.
The LTS support guarantees are even true for non-Oracle JDK builds. For example, Amazon Coretto also follows the LTS cycle.
There's also a tool that can generate all the bindings from a C header file automatically: https://github.com/openjdk/jextract
Then it doesn't really matter whether annotations are used or not, or some more low-level linker API (like FFM went with). As a user you just call into the generated bindings.
That's the philosophy: the JDK provides the low-level capabilities, and jextract provides the 'civilization', i.e. a usability focused layer on top. One of the advantages is that the JDK doesn't compete with other existing solutions, and those existing solutions can benefit from the new linking runtime APIs as well.
But it is certainly a much better way to interact with native APIs.
It's similar to the original Calendar API (as old as Java 1.1) versus the new Date/Time API introduced in Java 8.
``` var segment = MemorySegment.ofAddress(somePointer, sizeofStruct, memorySession); segment.set(ValueLayout.JAVA_LONG, offset, value) ```
Appreciate the example. Thank you.
Of course you are not required to do FFI, you may want to use this API to speed up some very memory-sensitive operation. Then you can define your own memory layout, specifying precisely endianness, alignment, padding, anything, and access it comfortably through these APIs.
Do you happen to know if the `get` causes allocation in the Java side there?
The JEP is really elegantly written!