12 ms·
Java 20: A Sneak Peek on the Panama FFM API
- kgeist 4y ago>In Java 20, MemorySegment::address returns a raw memory address in the form of a long rather than a MemoryAddress object So the API assumes pointers can't be larger than 64 bit?
- lbhdc 4y agoThis is perhaps a naive question, but are there many machines that need more than 64bits of address space? 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.
- deleted 4y ago[deleted]
- bitwize 4y agoEven leaving alone the fact that we may wish for a computer architecture that could theoretically mmap(), say, the internet... things like CHERI are gonna bring fat pointers back.
- cesarb 4y ago> This is perhaps a naive question, but are there many machines that need more than 64bits of address space? 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/ https://faultlore.com/blah/fix-rust-pointers/ and https://faultlore.com/blah/tower-of-weakenings/ https://faultlore.com/blah/tower-of-weakenings/), and also on GCC and glibc (https://lwn.net/Articles/909265/ 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.
- lbhdc 4y agoThat is very interesting, I hadn't heard of CHERI. 129 bit pointers sound like a bit of an oddity to me. What is the story behind choosing that size?
- solomatov 4y agoIt's not just a pointer, but a pointer + metadata. See this paper for details: https://www.cl.cam.ac.uk/research/security/ctsrd/pdfs/201904-asplos-cheriabi.pdf https://www.cl.cam.ac.uk/research/security/ctsrd/pdfs/201904...
- skitter 4y agoMemorySegment::adress only returns an address, which fits in 64 bit even on CHERI.
- TedDoesntTalk 4y ago> 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.
- gavinray 4y agoIt does allow for unsafe pointer math, I have some examples here where I've ported io_uring to use native Java Foreign Memory API with no external headers/bindings: https://github.com/GavinRay97/panama-liburing/blob/6a80673e73cfcf773212ee068b808f2b9d628df6/lib/src/test/java/panama/liburing/LibraryTest.java#L65 https://github.com/GavinRay97/panama-liburing/blob/6a80673e7...
- lbhdc 4y agoIt seems like you are using the `MemoryAddress` object. The article says that api was removed in 20. I may be misunderstanding what you are doing though :) 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.
- gavinray 4y agoSame difference, the "MemoryAddress" was a wrapper class around a long that pointed to the underlying address 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);
- throwaway2037 4y ago
- kgeist 4y ago>This is perhaps a naive question, but are there many machines that need more than 64bits of address space? 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.
- bullen 4y ago16 billion gigabytes not enough for you?
- littlestymaar 4y agoCHERI[1] anyone? [1] https://www.cl.cam.ac.uk/research/security/ctsrd/cheri/ https://www.cl.cam.ac.uk/research/security/ctsrd/cheri/
- Ygg2 4y ago2 EiB to be precise.
- Alupis 4y agoThis is probably posted in jest, but there was a time - not so long ago - where folks firmly believed nobody would need more than 16 memory bits, and even 1 Gigabyte of storage was considered outlandish. 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.
- CyberDildonics 4y agobut there was a time - not so long ago - where folks firmly believed nobody would need more than 16 memory bits 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.
- CyberDildonics 4y agoMost CPUs in common use can only address 48 bits of memory, which is about 280 terabytes.
- bullen 4y agoHere's a few: 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/X86 https://en.wikipedia.org/wiki/X86 The 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-80-watts/ 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.
- CyberDildonics 4y agoThe 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. 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.
- throwaway2037 4y agoReal question: What is a reasonable solution? Java has strictly defined primitive types: The largest integral type (long) is 8 bytes/64 bits. Please remember that Java does not have typedef, so it is not as simple as hiding the solution behind an opaque typedef that is #define'd at compile time. 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.
- taftster 4y agoThe Array limitation is easy to deal with (in my opinion). If you're needing arrays larger than Integer.MAX_VALUE, then you're probably doing something wrong to begin with. Usually you want to partition your data and maintain multiple arrays in some reasonable way hiearchical way, if you're getting that big. Or more likely, at that point, you're building your own memory structures using a ByteBuffer (and possibly in the future, MemorySegment). Point is, your reply is exactly right. Arrays can't be 64-bit large? Yeah, I guess.
- throwaway2037 4y agoI read this post a couple of times before it hit me: Your comment about Arrays and "doing it wrong" actually works without trolling the original question. Your main point: The question isn't hardly useful vis-a-vis arrays. However, for pointers -- if you really have 128-bit pointers -- then, yes, you are screwed and that interface/impl won't work. Excellent reply!
- kgeist 4y ago>Real question: What is a reasonable solution? 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.
- kaba0 4y agoBut that is just a raw representation of a pointer, what you actually pass and use as a pointer is not long values, but MemorySegments (formerly MemoryAddresses) — so the actual size is just an implementation detail. Plus Panama won’t go stable until Valhalla, which is necessary to actually have a “long long” in the language.
- usrusr 4y agoOutside of actually understanding, in detail and not just in rough strokes, what they are going on about, are there more educated layman's interpretations possible other than the polar opposites of "why do java people insist on making everything that complicated when others have so much simpler FFI" and "wow, it seems like they are addressing nuances that others haven't even thought of (or chose to knowingly ignore)"?
- plandis 4y agoWhy not read the JEP for this: https://openjdk.org/jeps/424 https://openjdk.org/jeps/424
- iamchp 4y agoThank you for this pointer. I was also wondering what the motivation for FFM was (I'm not a Java person), but reading the JEP answered my question! The JEP is really elegantly written!
- usrusr 4y agoDoes not change at all my uniformed perception that others would not bother formalizing that wide variation of nuanced differences.
- grishka 4y agoI hope this, along with pattern matching, will get released as non-preview in Java 21, which is supposed to be an LTS release.
- cgh 4y agoThere's nothing particularly special about LTS releases. According to Ron P, they are basically arbitrarily decided by sales and marketing: https://news.ycombinator.com/item?id=33039539 https://news.ycombinator.com/item?id=33039539
- grishka 4y agoThere is a bit: GraalVM bases itself on LTS releases in terms of features, and Android also somewhat follows them but lags behind way too much (Android 13 came out this year and prides itself on getting up to date with JDK 11).
- cgh 4y agoAh right okay, I wasn't aware of those things. I've never used GraalVM or written for Android. Thanks for the info.
- Alupis 4y ago> According to Ron P, they are basically arbitrarily decided by sales and marketing: https://news.ycombinator.com/item?id=33039539 https://news.ycombinator.com/item?id=33039539 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/ https://www.java.com/releases/ [2] https://www.oracle.com/java/technologies/java-se-support-roadmap.html https://www.oracle.com/java/technologies/java-se-support-roa...
- Waterluvian 4y agoI touched Java in school and rarely since. Is it the kind of language where you can be production-level productive with just a text editor and CLI, or do you need an IDE to manage everything? 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.
- RHSeeger 4y agoUsing an IDE for Java will, in general, make you orders of magnitude more productive. Not because of generated files or anything like that, but because it can recommend methods, auto-fill in details, add imports, etc. That kind of things saves a lot of time over the long run.
- Waterluvian 4y agoAh. I mean that’s somewhat true for any language. Is Java still super boilerplatey?
- papercrane 4y agoDepends on when you last touched Java. Lambda's (Java 8) have significantly reduced the ceremony needed to do things you'd use anonymous inner classes for. Java 14 introduced records, which makes building simple immutable data objects trivial. It's still a fairly verbose language, but it's miles from where it was 10 years ago.
- Waterluvian 4y agoMy textbook says Java 6. So I guess so! I’m kind of excited now to try it again.
- xahrepap 4y ago
- gavinray 4y agoGoing to copy my Reddit comment over to HN because I think it's valuable: --- 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 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#issuecomment-1237447761 https://github.com/openjdk/panama-foreign/pull/695#issuecomm...
- geokon 4y agoGenuine question, not trying to be dismissive - but why should something like a pretty printer be part of the JVM? It looks like something I'd expect to either be an external tool (like VisualVM) or part of an IDE
- jitl 4y agoHow would this work with an IDE? As part of a debugger in the IDE? Isn’t it nice that you could throw an Error with this stuff?? I don’t write Java but I do end up writing pretty printers whenever I work on a project with interesting binary data zooming around between memory, network, and disk, and it’s cool that this framework builds it in. I would love if all languages had a pretty printer this nice for their struct types.
- ohgodplsno 4y agoBecause language usefulness pretty much scales with stdlib features. Tools then get built around that pretty printer, instead of everyone having their bespoke implementation. The Java ecosystem is many things, but you cannot say it isn't homogenous. Dependency injection ? Every single project that has gone on to use annotations will be using the same @Inject. And so on, for many, many featyres.
- renewiltord 4y agoIs it possible for me to type-pun a struct from shared memory? I don't want to read the bytes and convert them to a long or whatever. I want to apply a long operation to some memory space. Is that doable in Java with Panama?
- kaba0 4y agoI’m not sure I understand you, but you can do ``` var segment = MemorySegment.ofAddress(somePointer, sizeofStruct, memorySession); segment.set(ValueLayout.JAVA_LONG, offset, value) ```
- renewiltord 4y agoIs it possible to lay out some struct-like thing into that memory segment and just read the whole thing for convenience? Appreciate the example. Thank you.
- kaba0 4y agoYes, for FFI jextract can also auto-generate helper classes, so some C struct named tb_cell with a field named ch typed int, you could access its fields like tb_cell.ch$get(memorySegment) and tb_cell.ch$set(memorySegment, value). 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.
- renewiltord 4y agoWell, wow, I'm going to take a look at this. Much appreciated. Do you happen to know if the `get` causes allocation in the Java side there?
- emmanueloga_ 4y agoApparently, this thing is part of the project codenamed "Panama", that should eventually replace JNI [0]. 0: https://openjdk.org/projects/panama/ https://openjdk.org/projects/panama/
- brunoborges 4y agoJNI won't be removed. Therefore, it won't replace it. 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.
- rebeccaskinner 4y agoThe last time I seriously used Java, JNI was the only way to access code outside of the JVM. I haven't kept up much, but this does seem cool. Anything that lets me use code I didn't have to write in Java is a feature of Java I'm excited about.
- geokon 4y agoI've never played with Java's FFI, but now that we have these "cosmopolitan" native executables that run on all platforms - would it be possible to link one copy of a large native library (ex: OpenCV or Intel MKL) and then not worry or think about platform specifics? 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 ...
- kaba0 4y agoAfaik cosmopolitan is only meaningful for executable startups, the actual x86 binaries can be platform-agnostic, but only if they basically do no communication with the outside world (or very little), since otherwise they have to use OS APIs. 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.
- kaba0 4y agoI almost wrote to the mailing list (for the first time) asking to better support nested lifetimes, and then they just drop a new API that does it so much better :D I honestly don’t know why I thought that such an experienced team didn’t think of this little detail! As always, thanks for the work!
- stevefan1999 4y agoAs a .NET guy, I want to say to following to Java folks: You're welcome to copy P/Invoke!
- kaba0 4y agoI don’t get your point, isn’t it pretty similar to Java’s Panama in its goals? Is it considered very good or you are being sarcastic and it’s bad?
- Godworrior 4y agoThis is one level below P/Invoke. It just offers a linking runtime API. There's also a tool that can generate all the bindings from a C header file automatically: https://github.com/openjdk/jextract 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.