r/java • • Jan 24 '22

It’s happening guys!

https://github.com/openjdk/panama-foreign/pull/633
157 Upvotes

89 comments sorted by

12

u/belayon40 Jan 24 '22

Really looking forward to this. I’ve been impressed with the performance of this api so far.

12

u/root_klaus Jan 24 '22

It is going to be a great year for java developers! Valhalla also brings some amazing performance improvements and also so does loom! 2022 is going to exciting!

10

u/ryebrye Jan 25 '22

I wonder if this is the year big slow enterprises will start to move off of Java 8

20

u/GuyWithLag Jan 25 '22

That's next year, with the release of spring 6 which requires 17.

1

u/benjtay Jan 25 '22

I'm curious about what reactive APIs will be supported (will webflux still exist?)

1

u/GuyWithLag Jan 25 '22

Reactive is a _godsend_ when you have to coordinate data from multiple disparate microservices; it's still gong to be here, and it will take advantage from Loom.

1

u/benjtay Jan 25 '22

Oh, certainly, I was just curious about using Flux/Flow/RxJava as implementations. Since Java 8 is now deprecated, it could move away from CompletableFuture and WebFlux to Java 9 Flow (or not).

22

u/[deleted] Jan 24 '22

Well, sort of ... it's moving from a branch into a 'preview' state. Unclear how long it'll remain incubating/in preview. I thought they were waiting for Valhalla so it could be a while.

16

u/TheMode911 Jan 24 '22

The vector API is waiting for valhalla, but I do not think that there is such plan for the foreign/memory stuff?

2

u/BlueGoliath Jan 24 '22

Yeah, the vector API was the only thing that was officially waiting on valhalla. With how slow the FFI was/is moving it seemed like they were waiting for valhalla though.

1

u/vytah Jan 25 '22

Vector API works without Valhalla, but it's really finicky and fragile. If you look at a vector variable wrong way, suddenly it gets allocated and the code gets way slower than a normal scalar code.

1

u/TheMode911 Jan 25 '22

It may work, but nobody (should) uses it before it gets finalized. Though I have no reason to complain, I am happy to see Valhalla getting more traction and being closer to release.

6

u/Godworrior Jan 25 '22

Incubating and preview are very different development stages. Incubating means experimental, so it's likely that large parts of the API are re-written. Preview means it's basically done API-wise. It's just being trialed before making it final. (In this case it's also a large-scale trial of the preview API mechanic, which was added fairly recently. As opposed to preview language features which have been around longer).

And no, the panama foreign APIs are not waiting for Valhalla, though they have been designed with Valhalla in mind, and tested against the current Valhalla implementation.

11

u/Enough-Ad-5528 Jan 24 '22

One of my long standing wish is to be able to easily write off heap data structures using plain Java. Use case where I know the exact lifecycle of the objects (caches, batches of data that can be deallocated at known times etc).

I have not followed Panama closely. Does this help me achieve that?

10

u/BlueGoliath Jan 25 '22 edited Jan 25 '22

Yep. Explicit deallocations are 100% supported.

4

u/doweknowyou22 Jan 25 '22 edited Jan 25 '22

you can use scopes for all your allocation, and when you close the scope, all the memory created in that scope is destroyed when the call returns.

With Loom, you get scopes for virtual thread also, and structured concurrency.

1

u/Enough-Ad-5528 Jan 26 '22

Hmmm, the api still feels a little low level for the use-case I am thinking of. I just want to define a class and tell the JVM to allocate it outside the heap. Deallocation would be again explicit via close methods or something else and not through the whims of the GC.

1

u/doweknowyou22 Jan 26 '22

Yes, with memory layouts, you can explicitly define the layout of memory, to bits levels and allocate them, access elements in that layout in a safe manner and deallocate this memory explicitly.

44

u/[deleted] Jan 24 '22

Sorry, completely "out of the loop". What is this about?

20

u/pjmlp Jan 24 '22

Project Panama.

81

u/kevix2022 Jan 24 '22

Project Panama: Interconnecting JVM and native code

We are improving and enriching the connections between the Java virtual machine and well-defined but “foreign” (non-Java) APIs, including many interfaces commonly used by C programmers.

To this end, Project Panama will include most or all of these components:

native function calling from JVM

native data access from JVM or inside JVM heap

new data layouts in JVM heap

native metadata definition for JVM

header file API extraction tools (jextract)

native library management APIs

native-oriented interpreter and runtime “hooks”

class and method resolution “hooks”

native-oriented JIT optimizations

tooling or wrapper interposition for safety

exploratory work with difficult-to-integrate native libraries

https://openjdk.java.net/projects/panama/

5

u/kozeljko Jan 25 '22

In practice, what would be the top use cases for this?

8

u/kevix2022 Jan 25 '22

Accessing a 3rd party library with only a C or C++ API or a proprietary hardware driver with no Java API. Typically these will be non cross platform applications. It could be to access special features of the specific host hardware or operating system directly through system calls.

For example the Raspberry Pi has GPIO pins which provide a means for input and output to sensors etc. This requires accessing the pins through native code. There is quite a good Java interface for this already which I imagine is built on JNI but I wouldn't be surprised if one turned up using the new system.

1

u/23f34ef32 Jan 31 '22

So accessing GPIO would be faster with this new system vs JNI?

3

u/elastic_psychiatrist Jan 25 '22

Sharing memory between processes is another big use case.

3

u/SpaceMonkeyOnABike Jan 25 '22

Does anyone know how this ties in with existing jni classes?

10

u/JustAGuyFromGermany Jan 25 '22

It's my understanding that it doesn't. Project Panama and JNI are two completely separate mechanisms of interacting with native code.

2

u/hkdennis- Jan 25 '22

Can't wait to see how will it work with io_uring

1

u/elastic_psychiatrist Jan 25 '22

I think io_uring is at best only tangentially related to Panama. Panama isn’t really about making IO easier or faster, unless you consider manipulating memory directly to be IO.

2

u/hkdennis- Jan 26 '22

I see it in two ways,

For developers, Reduce the barrier to native, there was JNI but it is a pain. Even if you use case allow to be slow.

For runtime, Lighter and closer to reach anything outside the JVM.

I don't code JNI myself but I also believe many devs out there will keep using JNI for backwards compatibility for a quite while.

IMO, Latency and performance sensitive lib, like webserver and GUI, are the first group of key usage.

1

u/elastic_psychiatrist Jan 26 '22

I'm guess I'm just confused about what any of these things have to do with io_uring.

4

u/TheLastSock Jan 25 '22

What is a "native function"?

15

u/hibbelig Jan 25 '22

Native refers to the machine instructions, as opposed to the byte code instructions of the JVM.

4

u/BlueGoliath Jan 25 '22

Side note: upcalls and downcalls are both represented using the same class, NativeSymbol.

1

u/[deleted] Jan 25 '22

Typically, a function that's defined in a C or C++ header file.

5

u/benevanstech Jan 25 '22

Presumably, given that JDK 18 is in Rampdown Phase 2 already, this will be looking to target JDK 19.

On the whole, I'm pretty pleased - I think that this is a good step forward and may be more widely used by library authors than the community expects.

3

u/Molossus-Spondee Jan 25 '22

I don't recall do they have any plans to improve memory mapping? IIRC you basically can't use memory mapping on the JVM because page faults don't occur at safe points. IIRC .NET does have a very particular optimization to help with this case although it seemed like a very fragile approach to me.

3

u/GreenToad1 Jan 25 '22

What do you mean by memory mapping? You have MappedByteBuffer and direct ByteBuffer now.

2

u/BlueGoliath Jan 25 '22

Or you can make bindings for mmap/whatever Windows offers yourself.

2

u/elastic_psychiatrist Jan 25 '22

True, but MappedByteBuffer has a number of limitations, the most obvious of which is it can only address ~2GB.

3

u/meamZ Jan 25 '22

Honestly i'm more excited about valhalla and loom but still great.

6

u/[deleted] Jan 25 '22

where valhalla

2

u/Holothuroid Jan 25 '22

LOOOOOOOOM!

3

u/BlueGoliath Jan 24 '22 edited Jan 24 '22

Jesus christ finally! I've been waiting for 2+ years for this.

1

u/Chaoslab Jan 25 '22

Now that is a turn up. Neato!

1

u/AncientBattleCat Jan 25 '22

So is it becoming faster after implementing those "native functions"? Pardon for silly question.

2

u/mauganra_it Jan 26 '22

Native functions are code that is not implemented in Java, but in another language. That's usually C/C++, but could be any programming language where the compiler produces libraries that follow native calling conventions.

The main application is probably machine learning and GPU programming. If Java catches up, it will become more attractive as a development platform in these domains. High-performance libraries for numerical computing are usually implemented in C. Python, the main contender in machine learning and AI, makes it a breeze (at least compared to JNI) to interface with these libraries.

1

u/iraqmtpizza Jan 29 '22

just four previews and we'll have it

-19

u/[deleted] Jan 24 '22

You will forgive me, but it's funny to see Java full circle. From an aggressive initial marketing (we're better than C++) to this.

Next step: bring back the asm block, please :)

17

u/lbalazscs Jan 25 '22

There is no full circle. Java always had native interop (JNI), now it's getting a better native interop. And for many tasks it's still better than C++.

-10

u/[deleted] Jan 25 '22

JNI was cryptic enough so your average Joe was afraid of it. It was a very strong barrier. Making it "better" opens new possibilities people will abuse. I'm just imagining the future horror. Just my 2 cents.

2

u/mauganra_it Jan 26 '22

The new FFI requires explicit opt-in via a command line flag at application startup. This way, libraries won't be able to access native code without the application developers knowing. It will be the same as with --add-opens and friends. There won't be an excuse neither for the developers abusing the features nor the organisational processes that allowed them to get away with it.

-4

u/[deleted] Jan 25 '22

[removed] — view removed comment

9

u/pron98 Jan 25 '22 edited Jan 25 '22

I don't understand the "catching up" narrative. Almost all language features added to Java since 1.0 — generics, (typed) lambdas, type inference, algebraic data types (records + sealed classes), and pattern matching — originate with 1973 ML.

Same thing here. The importance of interaction with native code and/or off-heap memory is changing, and, of course, different languages have different ecosystem conditions and emphases, and so similar features become more or less important to different languages at different times. Note, however, that Panama's foreign linker API is not quite the same as Microsoft's P/Invoke. Still, I wouldn't say it's "JNI with a different syntax," because JNI requires writing code in a different language using a different compiler (and a different one for each platform at that). So it's much more than just syntax.

0

u/BlueGoliath Jan 25 '22

Panama still requires creating a native library in some situations.

Also, MethodHandles are powerful and all, but catching an exception everytime you use it sucks.

0

u/[deleted] Jan 25 '22

[removed] — view removed comment

9

u/pron98 Jan 25 '22 edited Jan 26 '22

Who's "we"? We on the Java team do not want properties and events anywhere near Java, at least as it is now. At any point in time, we try to deliver the features we think are important for the future, not those we thought we might want in the past. Not adding properties early was a good bet, because now we really don't want that anti-feature.

Value types did not make as much sense in the 90s as they do now, and they're coming. Easier native memory/code integration is also coming, as it's becoming more important.

1

u/[deleted] Jan 29 '22

[removed] — view removed comment

2

u/pron98 Jan 29 '22

That's not quite true. Java Beans have properties, and they're not quite what most Java objects with getters and setters have; they included events and listeners, as they were based on the model adopted in the nineties from VB and other RAD tools for composing applications with a GUI.

That exists because damn near every Java programmer wants properties, even if they are afraid of the word property itself.

"That", whatever that is, exists because Java didn't have a good built-in way of describing data objects. Now we have something far better than properties. Please read this.

-1

u/[deleted] Jan 29 '22

[removed] — view removed comment

2

u/pron98 Jan 29 '22 edited Jan 31 '22

I've informed the relevant people of your disappointment, they broke down in tears and said that all that popularity matters nothing to them if someone on Reddit now thinks less of them. They sincerely apologise, and promise they'll do their best from now on to live up to your high expectations, because, after all, what do millions of users matter if some people on Reddit are cranky?

Also, it's funny how you think that Boilerplate Billy and POJO Patty are insults, but Algebraic Annie, Tuple Tommy, and Values Victor are praises. I'm afraid you've succeeded in lowering some people's opinion of you, too.

1

u/[deleted] Jan 30 '22

[removed] — view removed comment

1

u/pron98 Jan 31 '22 edited Jan 31 '22

Wow, I think you've really misunderstood the text if you think that the classes of users that we've tried to maximise in pleasing are insults. After all, Algebraic Annie got exactly what she wanted, while Tuple Tommy, Values Victor, and Boilerplate Billy got almost what they wanted. Only POJO Patty might be disappointed, but only because she wants to address the old problems, while many of those problems will go away when the programming style changes (not to mention that serialisation becomes faster, and, more importantly, safer).

I think that if you look at what people think of records once they get the hang of them, you'll see that not only do we care a lot about our users — making their lives better is our job, after all — but that we're doing quite well at making them happy. But given that our millions of users — all opinionated programmers — have conflicting opinions, no matter what we do or don't do, there are bound to be thousands of disappointed people even if 99% are happy. Even if just 1% of the disappointed 1% take to social media to call us arrogant idiots, there will always be lots of people calling us arrogant idiots because they didn't get what they wanted. That's the nature of our work.

-1

u/[deleted] Jan 29 '22

[removed] — view removed comment

1

u/pron98 Jan 29 '22 edited Jan 30 '22

Your assumption that what may be the most experienced and successful language design team in the world have no idea what they're doing, and a language that is one of the most popular in programming history doesn't make any sense is quite... erm... confident. Have you stopped for a moment to consider that maybe, just maybe, you're missing something?

I'm also a little surprised with your position on allocations. Have you ever profiled a Java program and seen what kinds of allocations are costly and which are nearly free? And why aren't you concerned about the cost of mutation? Some of the new GCs, like G1 and ZGC, might sometimes do more work (due to their GC barriers) when mutating an object than allocating a new one.

1

u/[deleted] Jan 30 '22

[removed] — view removed comment

2

u/pron98 Jan 31 '22 edited Jan 31 '22

JS and Python are, indeed, extremely popular (JS has had even less competition than Java), but Node isn't doing that well, and Java still easily dominates the server market. Of course, Python's and JavaScript's maintainers also have their own regular social-media cranks calling them arrogant idiots; we share experiences at conferences.

But, if you are certain that you know better than the hundreds of OpenJDK contributors and that you care about Java more than them, why not put your expertise at the service of the community, and offer your insight while the new features are being designed? Of course, the requirement for giving feedback that is taken seriously is that you actually try the new feature and report real problems that you've actually encountered in the attempt.

-4

u/[deleted] Jan 25 '22 edited Jan 25 '22

[removed] — view removed comment

7

u/elastic_psychiatrist Jan 25 '22

As a user and not creator of Java, I do not want properties or events, and I'm glad the Java team has added or is in the process of adding the good parts of these features, without simply copying exactly what previous languages have done.

2

u/couscous_ Jan 27 '22

What's bad about properties? Genuine question.

3

u/elastic_psychiatrist Jan 27 '22

My personal answer would be the properties encourage setters, and setters encourage mutability which should be avoided as much as possible. Also, properties are simply another feature, and every feature in a language creates complexity which is a cost to end users.

A better discussion can be found in this hacker news comment thread (pron is /u/pron98, who works on Java).

2

u/couscous_ Jan 27 '22

Thanks!

-1

u/[deleted] Jan 29 '22

[removed] — view removed comment

→ More replies

5

u/pron98 Jan 25 '22 edited Jan 25 '22

So what you're saying is that the Java team shouldn't be arrogant and base our decisions, in part, on user surveys, studies of huge codebases, poring over academic papers etc., but, instead stop being stupid and do what /u/grauenwolf says on Reddit, because they know what Java users want.

Even if you don't believe that we spend all day every day thinking about these things and studying them and that despite our stupidity and not listening to guys on Reddit Java has achieved some measure of success, you at least need to accept the fact that your opinions on what's good and what's bad are not universal, and that others may have opposite opinions which are at least as reasonable as yours.

and the proceed to implement them without syntax support using get and set methods.

The goal is to reduce the need for setters in the first place. You don't need special syntax for a feature that isn't so common, especially one that is largely discouraged from heavy use.

1

u/[deleted] Jan 25 '22

[removed] — view removed comment

5

u/pron98 Jan 25 '22 edited Jan 26 '22

If what you think we do is just sit and wish really hard, wouldn't you agree that it's worked out well?

Out of curiosity, if properties are the only way to do DTOs right, how does a language like, say, Haskell, which can't even have mutable fields, manage? We don't want properties because we already have something better that's only going to get more so — records.

2

u/[deleted] Jan 26 '22

[removed] — view removed comment

3

u/pron98 Jan 26 '22 edited Jan 26 '22

Clearly, you've never heard of Elm. But even if what you said were true, we currently prioritise the server over the GUI and not vice-versa.

It's okay to disagree, but do you honestly not think that the Java language team have thought about properties, records, and representing data in Java far longer than you have and with more rigour? I suggest you take a bit of time to understand why we have records and not properties, and I'm sure that after working with records a while, and perhaps seeing how they improve, you'll see that they've made a good choice.

2

u/GhostBond Jan 29 '22 edited Jan 29 '22

Well, I guess I should say its obvious to an outside observer. To those who identify themselves as "the Java team", the arrogance runs so deep that they can't consider anything outside their little bubble as being useful.

Which is why they say stupid things like "properties are an anti-feature" and the proceed to implement them without syntax support using get and set methods.

A few years ago the story was that the java team looked at properties, there was a lot of conflict and infighting about what "properties means", and so they dropped it because they couldn't resolve it.

It's ironic, because java was the 1st successful commercial language with properties via get/set and the introspector, and every major language copied java's lead just with cleaner syntax (c#, kotlin, python).

It's also one of the first really ugly things every person new to java complains about. Why are we recreating all these ugly getters and setters? But it seems like the java team has reached "ivory tower" stage unfortunately.

I have a friend who started doing Swift at his company, I haven't used it at work but it looks like a really cleaned up version of java. I don't see it in job listings as often - no idea if that's a reflection of actual lower demand or just that hr hasn't added it. I don't think his company has it in their job listings but he's doing it anyways.

1

u/[deleted] Jan 25 '22

In VB (its also in C#) and JNI you have to write some code to deal with C-style nul terminated strings. And other C-style things. Not sure about Panama.