131 points · by mfiguiere · 3y ago · 174 comments
---
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)
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 : - )
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.
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.
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.
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.
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.
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.
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.)
- 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...
It's still a fairly verbose language, but it's miles from where it was 10 years ago.
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.
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.
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.
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.
...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.
If anything I’d say JS has a more complex configuration story than Java.
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.
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.
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.
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.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.
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.
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?
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.
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.
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.
Plus Panama won’t go stable until Valhalla, which is necessary to actually have a “long long” in the language.
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.
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.
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.
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.
The JEP is really elegantly written!