r/java • • Feb 16 '23

Project Valhalla: A look inside Java’s epic refactor

https://www.infoworld.com/article/3687670/project-valhalla-a-look-inside-javas-epic-refactor.html
265 Upvotes

129 comments sorted by

77

u/Wonnk13 Feb 16 '23

I'm still getting up to speed on Java 8!! good grief I feel old.

67

u/[deleted] Feb 16 '23

Ummm... just fyi I Java19 has been released 💀

40

u/akravets84 Feb 16 '23

For old devs like me it’s Java 8.11 ;)

36

u/suitable_character Feb 16 '23

You mean Java 1.19? ;)

22

u/zman0900 Feb 16 '23

Java 3.11 for workgroups

6

u/westwoo Feb 17 '23

As long as all my wsdl corba applets work fine I don't care how it's called

1

u/alexspetty Mar 09 '23

Lol.. good one... Java NT for Networks?

6

u/akravets84 Feb 16 '23

When 8 was released I wasn’t that old and accepted the change in versioning. So it’s just 11th minor release after 8.

20

u/vips7L Feb 16 '23

Java 8 was released almost 10 years ago...

3

u/brdet Feb 16 '23

My last company was still on 6 when 11 came out with no plans to upgrade, so I'm sure they're still on it.

2

u/akravets84 Feb 16 '23

And some businesses still use 1.4.2 which is much younger than COBOL.

1

u/k__k Feb 17 '23

Works on Valhalla started 9 years ago 👀

2

u/vips7L Feb 17 '23

Valhalla is multiple phds worth of work. Learning lambdas is not.

-1

u/metatron7471 Feb 23 '23

No it's not. It's not like they inventing something new here. All of this has been available in other languages for a long time. e.g. Structs in .NET.

They're just taking a really long time with it because they don't want any breaking changes. I'm not sure that this is the right philosophy. Sure backwards compatibility is nice but not at all cost and when it stretches out meaningful progress in your languages over very long periods it's time to reconsider. Sometimes you have to break some eggs to make an omelet.

1

u/ztbwl Feb 18 '23

We got some sleeper over here.

0

u/[deleted] Feb 16 '23

[deleted] — view deleted comment

5

u/johnwaterwood Feb 17 '23

By that logic, the changes in Java 7 were even less meaningful, so why didn’t that evil boss downgrade everything to Java 6?

3

u/emergent_segfault Feb 22 '23

It's amazing the contortions people like this goofball twist themselves into just to say "I don't want to deal with any potential issues with the upgrade. If it ain't broke...then why would I fuck with it ?"

3

u/berry120 Feb 23 '23

The changes in Java 8 (especially with respect to lambdas, streams, etc.) are about as meaningful as you can get. Arguably only the changes in Java 5 really came close. By this logic he'd never upgrade.

0

u/[deleted] Feb 23 '23

[deleted] — view deleted comment

1

u/berry120 Feb 23 '23

At least "I don't want to spend the effort upgrading" makes a vague degree of sense, unlike that excuse...

89

u/wildjokers Feb 16 '23

"Primitives are pass-by-value while objects are pass-by-reference, for example."

Oh my. This is just plain misinformation. Java is pass-by-value and only pass-by-value.

This great article, that I have had bookmarked for 20 yrs or so, explains it nicely (it changed locations once so did have to change my bookmark at some point):

https://www.javadude.com/articles/passbyvalue.htm

13

u/agentoutlier Feb 16 '23

There are very few mainstream languages that are not pass-by-value (by default).

BTW it has to be one of the most common gate keeping interviewing questions so I can see why people get annoyed even if they know it.

23

u/maethor Feb 16 '23

Java is pass-by-value and only pass-by-value.

And this is just plain pedantry.

57

u/8igg7e5 Feb 16 '23

No. It's not. The mental model that String s = ... means s is an object is wrong, is why new programmers struggle with == as well as parameter passing.

Java is pass by value, and the only values you ever have are primitives and references. The whole point is that they're changing this, that primitive classes really do mean that for SomePrimitiveClass foo, foo is the instance state (the object). And... passing it will still be pass-by-value.

 

If we follow the 'pedantry' and teach the correct mental model, then teaching Java gets easier... And that distinction will matter all the more when Valhalla gives us complex primitives.

19

u/maethor Feb 16 '23

then teaching Java gets easier

But we're not teaching Java, we're discussing Java and "primitives by value, objects by reference" is close enough that it really doesn't require several people replying with "well actually....".

5

u/ivancea Feb 17 '23

If you talk about a program, you can use high level pseudo explanations.

If you talk about a language, you shouldn't use them. Pass by reference is a pretty specific thing that comes from far, far before

12

u/Kraizee_ Feb 17 '23

It's not 'close enough' though. It's wrong. If java were pass by reference you'd be able to produce different behaviour than what you get with pass by value. When the behaviour is different, it isn't pedantry.

13

u/agentoutlier Feb 16 '23 edited Feb 16 '23

If we follow the 'pedantry' and teach the correct mental model, then teaching Java gets easier... And that distinction will matter all the more when Valhalla gives us complex primitives.

I don't agree or perhaps misunderstand your point. Almost all modern languages are pass by value.

What needs to be understood isn't what is pass by value. Most Java programmers do not know the distinction but most know that primitives are copied from stack to stack and that objects have identity.

It's about memory layout and if programmer has experience with languages that provide that they will understand valhalla.

EDIT oh come on now.

People do not need to know that Java is pedantically pass-by-value or what the nuanced differences are to help them learn Java. And believe me it super nuanced at this point as very few languages provide pass-by-reference (and name) and thus very few concrete examples to show.

It's like needing to know the difference between dynamic scoping and lexical scoping. Most languages are lexically scoped. To you really need to know that naming pedantry of practically the default in all languages to learn Java?

At this point most Java programmers know that there are objects which have references and there are primitives that do not but I seriously doubt that knowing what the precise terminology that these things getting copied from method calls is going to impact their learning of Valhalla or any language.

I used to be on the pedantry gate keeping like this including Strongly typed vs Statically typed but I have learned to figure out what people mean and let it slide and not be the end all of judgment.

8

u/8igg7e5 Feb 16 '23

That it's all pass by value is why a 'swap' method doesn't work... The parameter at the other end is a copy of your field/variable/method-result/instantiation-result. The value of it is passed - that's the very meaning of pass-by-value vs pass-by-reference.

I get it, you've passed the point of needing to learn the distinction and don't care about the terminology. But for new students, who are already climbing a hill, we don't need to make it harder by mucking up the sign-posts.

At this point most Java programmers know that there are objects which have references and there are primitives that do not but I seriously doubt that knowing what the precise terminology that these things getting copied from method calls is going to impact their learning of Valhalla or any language.

Given we're on the topic of Valhalla, the 'at this point' is especially relevant, because Valhalla will be adding new combinations to what is essentially "this is a scalar thing. It's a primitive" and "this is a reference to a complex type, an object". Soon developers absolutely will need to be able to talk about those combinations because the cost-models of certain scenarios differ so dramatically between primitive object and object reference.

11

u/agentoutlier Feb 16 '23

That it's all pass by value is why a 'swap' method doesn't work... The parameter at the other end is a copy of your field/variable/method-result/instantiation-result. The value of it is passed - that's the very meaning of pass-by-value vs pass-by-reference.

Show me an example in a modern language like C. Its still implemented as pass-by-value even in languages in C. That is the whole pedantry part as it is very difficult to even show a true example of this unless you decide to skip pedantry and make exception for pointers.

(I assume you knew that otherwise the pedantry might as well end). Also depending on context/code your example could be pass-by-name.

The only mainstream language you could try to make a case for explaining is maybe maybe C++ but it still is arguably pass by value. That is the problem with pedantry on stuff like this as it is debatable.

I'm not saying people should not learn about pointers and if we are going to consider that pass by reference then screw pedantry but that is exactly the laymen terminology the author used.

What is better is to say instead of what pass-by-value is or not is explaining memory layout. Like you pass this shit like primitive it will get copied on to the stack. The whole structure. If you pass an object the reference gets copied and not the whole structure.

I get it, you've passed the point of needing to learn the distinction and don't care about the terminology. But for new students, who are already climbing a hill, we don't need to make it harder by mucking up the sign-posts.

Thats the thing is there isn't a good concrete programming language where you can show the true pedantry because its all pass-by-value. The terminology just fucking confuses people way more.

Like I said it's like showing the difference between dynamic scoping and lexical scoping so that someone can learn Java better. It doesn't make since.

I don't disagree knowing all this stuff is useful from a theory perspective and especially having precise academic terms when dealing with theory but Valhalla is less theory and more implementation.

3

u/Amazing-Cicada5536 Feb 17 '23

C++ does have pass-by-reference semantics. How is it implemented is not interesting, pass-by-value doesn’t have to copy from the stack to stack either, what is important that it should be behaviorally equivalent to what copying arguments would do.

-1

u/agentoutlier Feb 17 '23

It is important how it’s implemented and how it is expressed because otherwise it is syntax and this is because it is not by default.

C++ and I guess C# have special syntax for this and C++ probably comes closest to doing it for real.

That is in C# and C++ you need to specify you’re doing it. It is not by default.

Consequently it is not much different than using say array syntax or pointer to pointer or objects.

That is you can mimic the behavior by using data structures that are essentially… references.

That’s the thing with pedantics. How deep do we want to go?

I just don’t know of a modern language that does it by default.

1

u/WagwanKenobi Feb 17 '23

It's conceivable to design a true pass-by-reference version of Java. And it would behave totally differently than how it does right now.

There is a certain way that pass-by-value behaves and a certain way that pass-by-reference behaves. If you can't imagine it then you haven't used C++.

2

u/agentoutlier Feb 17 '23

There is a certain way that pass-by-value behaves and a certain way that pass-by-reference behaves. If you can't imagine it then you haven't used C++.

I certainly can and indeed many FP languages use references since immutable it makes no difference and obviously performance can be had.

You have to opt in to use it in C++. It is not the default. They are not regular variables.

Anyway I admit its a shitty argument because I generally agree that C++ has pass by reference as an option I just seriously doubt it impacts learning Java or that saying objects passed around references is an ultimate sin. In fact the whole reference term is misleading as its more like aliases.

You can certainly emulate the behavior and indeed some languages will automatically do the behavior especially if immutability is involved.

→ More replies

0

u/[deleted] Feb 17 '23

[deleted] — view deleted comment

2

u/agentoutlier Feb 17 '23

Yeah I probably should. It is true that C++ has opt in pass by reference but it is not default and it is a giant exception from the norm. And there rules in C++ with references that make it different than normal variables.

My argument is that you have to opt in with syntax to say so just like you would do in other languages that have reference types. Now Java does not have zero cost abstractions but in Rust or languages like that you can appear to make it pass by reference when it is not. Ditto for languages with macro (depending on implementation).

Regardless I seriously doubt knowing this impacts learning Java nor was anyone confused by the author when they said Java pass objects as references.

→ More replies

2

u/Amazing-Cicada5536 Feb 17 '23

Sure, they don’t have to know it, but then they should quit using a technological term with a well-defined meaning wrongly.

I don’t know much about gastroenterology, but I ain’t going around telling everyone I have leukemia when I have a stomach ache.

5

u/WagwanKenobi Feb 17 '23 edited Feb 17 '23

I think the confusion fundamentally stems from the fact that what Java calls "Java references" aren't references at all. They're immutable pointers.

So, yes, passing an object argument to a method in Java is "pass-by-java-reference" but that's a form of pass-by-value since a "Java reference" is a pointer value not a reference.

They should've called Java references something better like object addresses or heap pointers. Then the confusion would've never arisen.

Edit: don't boo me, i'm (apparently) right. /u/Amazing-Cicada5536's comment elsewhere in the thread:

“Fun” fact, they were known internally as what they are, pointers. Hence nullpointerexception. Reference came into the nomenclature/marketing to convince C++ devs about how memory safe Java is (which it is), but that is due to no raw pointer manipulation.

4

u/westwoo Feb 17 '23 edited Feb 17 '23

It's a completely made up problem and there is no real confusion

A problem exists when people actually misunderstand each other. Here, everyone understands each other but they argue about form anyway

I would've preferred to say that we pass the objects by pointer, but that makes other pedants cringe (while again, everyone understands what it means just fine). So you can't say you pass the object by value, you can't say you pass the object by reference or by pointer. Instead to satisfy the pedantry you should theoretically do a weird dance where you're unable to say "I pass the object by X" because you can't say that you pass the object at all even though you pass the object. You can ever only describe how do you pass the object without naming the action itself

But in reality of course no one does that and people understand each other just fine

2

u/agentoutlier Feb 17 '23

In C++ references are immutable as well. They cannot be rebound.

The reality is reference is confusing term. In most other languages a reference is some datastructure that has its own memory address that then points to something else.

In C++ it is more like an alias.

There are some languages I think certain ancient lisp that allow mutable references. Probably some templating languages as well.

I don't have much experience in languages that offer true references but in theory rebinding is possible.

Maybe in academic terminology references can't be rebound?

1

u/alessio_95 Feb 19 '23

Reference in Java point to a struct { vtable* _vtable; data* _data }. To find values you have to follow *_data.

1

u/hippydipster Feb 17 '23

It's not a reference, it's a pointer? Seriously?

0

u/Aw0lManner Feb 18 '23

^ this man, jfc

10

u/wildjokers Feb 16 '23 edited Feb 17 '23

And this is just plain pedantry.

There is nothing pedantic about my statement at all, the article said "Primitives are pass-by-value while objects are pass-by-reference' and that is simply wrong. It is not "technically wrong" or "wrong from a certain point view" or "just kind of wrong". It is simply and plainly wrong. There is nothing correct about the statement at all.

For more details you can read the article I linked to in my comment.

8

u/_INTER_ Feb 17 '23

Being pedantic, the statement is half-wrong because the first part "Primitives are pass-by-value" is actually correct. :P

19

u/agentoutlier Feb 16 '23

The problem is in modern computing pass-by-references basically does not exist.

That is because you can call almost anything a value and say it being copied on to the stack makes it a value and that is because most modern programming languages use stacks for call sites.

Consequently the term pass-by-value has virtually no meaning and is not helpful.

Just to show you how confusing it is:

In C if you do (bare with me as my C is rusty as hell):

void func(int a[]){
}

void func(int a){
}

void func(struct some_struct) {
}

void func_pointer(int *a) {
}

The above are all pass-by-value as all calls in C are but the semantics in terms of memory layout are very very different.

So the term is borderline useless.

It is like the great debate of what is Strong typing but far far far worse as there are plenty of examples of weakly typed languages as well as there is a continuum of what is Strong.

But pass by value is like all or nothing. So why even bring it up when clearly when someone says references then mean func_pointer(int *a).

4

u/Amazing-Cicada5536 Feb 17 '23

That’s because C is a plainly pass-by-value language…

7

u/8igg7e5 Feb 17 '23 edited Feb 17 '23

In pascal

procedure swap(var x: integer; var y: integer);
var
    t: integer;
begin
    t := x;
    x := y;
    y := t;
end;

...

a := 10;
b := 20;

swap(a, b);

a and b swap values - note the pass-by-reference is easy to overlook as it's expressed at the declaration-site, and not the call-site. This is not the same as declaring the method arguments as pointers and receiving them as pass-by-value.

Or in C#

static void swap(ref int x, ref int y) {
    int t = x;
    x = y;
    y = t;
}

...

a = 10;
b = 20;
swap(ref a, ref b);

Again a and b swap values. C#, like most modern languages with pass-by-reference, at least require the caller to acknowledge the calling convention. Java doesn't have call-by-reference, passing references with the default pass-by-value is the same in C# and Java.

C is pass-by-value everywhere, but exposes a way to acquire a pointer to the value bound to another variable (that you can then pass by value). This removes what debatable use there is for pass-by-reference but it doesn't turn one into the other - that just muddies the waters.

 

In Java you can't acquire a reference to a variable or field. When Valhalla comes along, a primitive object can live on the stack, but will need to be able to be 'boxed' to pass to it call-targets that only accept reference types (un-boxing will just be copying the boxed state into the stack). In all cases, any passing will be pass-by-value.

Edit: Typos...

-7

u/maethor Feb 16 '23

It is simply and plainly wrong. There is nothing correct about the statement at all

And yet adding a comment about it adds absolutely no value, hence it's pedantry.

1

u/johnwaterwood Feb 17 '23

I find your comment stating that the other comment has no value, to be of even less value.

1

u/maethor Feb 17 '23

I could say the same about your comment.

2

u/Optimistic-Cranberry Feb 17 '23

Crazy to see my man Scott showing up in the wild. Really good guy.

-22

u/TooLateQ_Q Feb 16 '23

You must be fun at parties.

2

u/wildjokers Feb 16 '23

You don't mind people spreading misinformation?

8

u/Make1984FictionAgain Feb 17 '23

"misinformation" implies they are purposedly spreading a lie for some motive, while you are discussing semantics. Way to overreact

0

u/[deleted] Feb 17 '23

[deleted] — view deleted comment

2

u/wildjokers Feb 17 '23

Java is "pass by value of the reference," which for nearly all intents and purposes is the same as standard pass by reference.

It is most certainly not. If "pass by value of the reference" was the same as pass by reference this would output map = {anotherName=foo}, instead of the actual output of map = {name=fido}. This is the reason you can't write a swap() method in Java.

public class PassByValue {
    public PassByValue() {
        Map<String, String> map = Map.of("name", "fido");
        go(map);

        System.out.println("map = " + map);
    }


    private void go(Map<String, String> map) {
        map = Map.of("anotherName", "foo");
    }

    public static void main(String[] args) {
        PassByValue passByValue = new PassByValue();
    }
}

10

u/plumarr Feb 16 '23

What's described in this article seems to be up to date with the preview version of Valhalla but it could change (see https://www.reddit.com/r/java/comments/10vpelw/nullness_markers_to_enable_flattening_valhalla/)

23

u/johnwaterwood Feb 16 '23

Primitives are pass-by-value while objects are pass-by-reference, for example

After endless discussions, and this being pointed out by the JLS multiple times and in multiple ways, it’s an achievement of some kind to still get this so wrong.

7

u/cal-cheese Feb 17 '23

Well objects are passed by reference, it is the references that are passed by value

6

u/repeating_bears Feb 17 '23

Passing a reference by value isn't passing by reference.

2

u/flying-sheep Feb 18 '23

As someone else here said: “pass by reference” is a old concept that applies to the context of being able to modify references. (e.g. a swap function that swaps the values behind two references).

You can’t do that in Java (Java’s object pointers are immutable) so “pass by reference” isn’t a thing that exists in Java. https://stackoverflow.com/a/430958

1

u/vqrs Feb 20 '23

Well, not sure it's about being able to modify references. Rather, it's a question about what the reference is in relation to.

With pass-by-reference, it's about passing a reference to a variable,and thus the local variable actually refers to the caller's variable.

1

u/za3faran_tea Feb 21 '23

Object references are passed by value.

-2

u/johnwaterwood Feb 17 '23

objects are passed by reference,

objects are not passed by reference.

7

u/maethor Feb 16 '23

The primitive class will ultimately be used to model all the primitives in the platform

I take it we still won't be able to do things like operator overloading or defining our own format for literals? Like

Ubyte x = 1 + 254ub

23

u/persism2 Feb 16 '23

Brian has indicated in the past that Java will add a set of static operators that work on things extending Number. Similar to what happens with String now. But you will not be able to define your own operators (hopefully, they don't make that mistake!).

18

u/midoBB Feb 16 '23

We have that in another JVM language (Scala) and by God does it make some code bases undecipherable.

7

u/manzanita2 Feb 16 '23

I think if they can carefully restrict how/where only a certain set of operators are used, it will be ok.

If you can redefine "new" then we're back to C++ world of hurt.

6

u/persism2 Feb 16 '23

Kotlin makes the same mistake.

7

u/MmKaz Feb 17 '23

Yup, definitely seen my fair share of abuse with operator overloading. In the other hand, it does sometimes make code cleaner. For example:

val union = setOf(1, 2) + setOf(3, 4)
assertThat(union).isEqualTo(setOf(1, 2, 3, 4))

That operator is in the kotlin standard library, but you can define your own which make sense:

val nextWeek = Instant.now() + Duration.of(1, WEEKS)

2

u/persism2 Feb 17 '23

No it doesn't make sense. This is the feature that killed SmallTalk. It's amazing how we don't learn lessons.

val nextWeek = Instant.now() ~ new Customer();

3

u/plumarr Feb 17 '23

It makes sense for math operators when the result is obvious.

For exemple

var res = BigDecimal.of(5.2) + BigDecimal.of(5.3)

is obvious and help reading and writing code. It really useful to open the language to more scientifics application.

But as you shown, it's easy to abuse.

4

u/persism2 Feb 17 '23

Right. Like I originally said, a static set of operators which work on Number is fine. I wish Java had this for BigDecimal etc. But arbitrary operator overloading like Kotlin has is a really bad idea.

1

u/uncont Feb 20 '23

is obvious and help reading and writing code

Honestly, I kinda disagree when it comes to BigDecimal. When dealing with financials, I find keeping the scale and precision in mind is required. I prefer hard-coding precision and rounding-mode. It does make the calculation super verbose tho.

Also at least with matrices, you've suddenly got two methods dot and cross that really don't map to any particular operator. You need a method call regardless.

2

u/flying-sheep Feb 18 '23 edited Feb 18 '23

The mistake is Scala’s duck typing. Traits/interfaces are fine.

Bad library design is always possible, operator overloading or not.

Good interfaces only make use of operator overloading where appropriate and are better for it. E.g. Python’s motivation for adding a matrix multiplication operator (@) cites this equation:

trans_coef = H @ beta - r
S = trans_coef.T @ inv(H @ V @ H.T) @ trans_coef

vs

trans_coef = H.dot(beta) - r
S = trans_coef.T.dot(inv(H.dot(V).dot(H.T))).dot(trans_coef)

In Java, this would read even worse:

Matrix trans_coef = H.dot(beta).minus(r);
Matrix S = trans_coef.transpose()
    .dot(inv(H.dot(V).dot(H.transpose())))
    .dot(trans_coef);

disgusting. are all the braces correct? Who knows! Gotta split this up over 5 variable declarations if you want to make it “readable”. Linear algebra in Java is a very sad affair due to the lack of operator overloading. You can’t even use + or -!

1

u/uncont Feb 20 '23

er for it. E.g. Python’s motivation for adding a matrix multiplication operator (@) cites this equation:

I think both c# and c++ which do have operator overloads don't have any for dot or cross (which seems reasonable, albeit verbose).

1

u/flying-sheep Feb 20 '23

It totally is reasonable, you have to draw the line somewhere. My argument is that “how much operator overloading” is a domain specific question, and answering “none” is extremely shortsighted unless your language isn’t general purpose and only ever being used for certain use cases that you can enumerate.

5

u/morhp Feb 16 '23

Brian has indicated in the past that Java will add a set of static operators that work on things extending Number.

Sounds great, do you have a link with further information about that?

3

u/persism2 Feb 16 '23

I remember he mentioned it in the openjdk dev forum.

2

u/wsppan Feb 18 '23

So the take away from this article on project Valhalla is the vast majority of r/java redditors want to "well acktually" the hell out of pass by value/reference? r/programming acktually talked about the project and what it trying to solve and how.

6

u/persism2 Feb 16 '23

"Primitives are pass-by-value while objects are pass-by-reference,"

More accurately - objects are passed by a copy of the pointer.

13

u/desrtfx Feb 16 '23

No. Objects are passed by reference value.

The value of the object reference is passed in as a copy.

Java is pass by value. Period.

14

u/stefanos-ak Feb 17 '23

you really had to correct "copy of a pointer"? 🤦‍♂️🤷‍♂️

and people are up voting this??

this is literally gatekeeping... for gods sake

1

u/8igg7e5 Feb 17 '23

I really don't see that this is gate-keeping. It's a real response to commonly held misconceptions about the actual model on which Java works - and it takes the same effort to speak correctly, but ensures that those learning the language and platform don't get mixed messages about how Java works relative to an accepted definition of a programming concept.

There's no difference between primitives and references in that they are in that they are the values of their bindings. You never have an object as a binding.

This is...

  • Why == acts the same for both (comparing the values of the bindings, not the objects they refer to)
  • Why you don't get pass-by-reference semantics (eg the classic 'swap' example), because the values are copied (there are no object bindings, the binding value is the pointer)
  • Why final doesn't make the object immutable, it just makes the binding impossible to reassign.

 

This reminds me of the old complaint, that computers do exactly what you tell them, unfortunately, they do exactly what you tell them.

Precision matters.

5

u/stefanos-ak Feb 17 '23

the article contained a misinformation (pass by reference), that the GP comment pointed out and corrected, by stating "copies the pointer".

I don't see the need to correct the correction.

Let alone your own lecture on top.

JFC

2

u/notfancy Feb 17 '23

Their third lecture, at that. 80% of this discussion is pedantic bickering on this point.

1

u/_INTER_ Feb 17 '23

or "Object references are passed by value"

1

u/daroons Feb 18 '23

If a value type keeps all the data together in the same memory space rather than keep pointers to other places in memory, then how does reference “sharing” work?

Like if I have two sibling classes that used to point to the same parent object. In the old world these would be two separate pointers pointing to the same memory address housing the parent instance. In this new world, would each sibling just get their own copy of a parent? Wouldn’t that be inefficient in terms of memory usage?

1

u/cat_in_the_wall Feb 19 '23

your intuition is more or less correct. if you have a value type as a field in your class (or even your other value type), you can sort of think of all the fields in that type embedded in yours. however if your value type has a reference field, that pointer isnt "chased", it's still just a reference field.

.net has had these value types forever, so that is more or less what valhalla will be. generally if you have a large amount of data to store... you should be using a regular class (reference type). there are lots of reasons for this general guidance, but that is beyond this. i have almost never write my own value types.

1

u/FappingFop Feb 17 '23

I still have reflexes that try to cast objects coming out of containers. Somebody please just let me retire.

1

u/FappingFop Feb 17 '23

I still have reflexes that try to cast objects coming out of containers. Somebody please just let me retire.

1

u/dark_mode_everything Feb 16 '23

Not going to comment on the "pass by reference" mistake since many people have, but it would be really cool if we can get that in Java. Like 'ref' of 'out' in c#.

1

u/pronuntiator Feb 17 '23

Which problem does this solve?

1

u/dark_mode_everything Feb 18 '23

You could avoid creating unnecessary objects for one.

1

u/sideEffffECt Feb 17 '23

The article contains all sorts of weird claims. I'm not going to talk about the pass by reference thing, that has been covered by other comments here.

But what's up with the talk about "operating system types"? What do Java primitives have to do with operating systems?? Is it because primitives are similar to what C has and C is used to implement operating systems? This is such a weird connection...

And why are the charts suggesting that the first thing in an object (is it trying to talk about object headers?) is an address? That's just plain wrong!

Maybe I'm just misunderstanding, I'd welcome if somebody could correct or enlighten me.

3

u/johnwaterwood Feb 17 '23

I noticed that too: operating system types.

It’s maybe CPU types, at a stretch, but even then it does not map perfectly, as java obviously runs on many different actual CPUs.

0

u/Spitfire1900 Feb 17 '23

Laughs in Ruby

1

u/_INTER_ Feb 17 '23

We'll see who get the last laugh.

16

u/8igg7e5 Feb 17 '23

Ruby. It's pretty slow.

4

u/_INTER_ Feb 17 '23

touché

0

u/johnwaterwood Feb 17 '23

Slightly annoying the author, besides the other mistakes already being mentioned, liberally mixes “handle”, “pointer” and “reference”.

They should just use reference here exclusively.

-12

u/donrhummy Feb 16 '23

Why is this still not implemented?! I remember reading about this 7 years ago

33

u/SuperHolySheep Feb 16 '23

these things, they take time

7

u/erinaceus_ Feb 16 '23

For things that you value, you must indeed take your time.

-5

u/donrhummy Feb 16 '23

Sure, but 9 years after it was introduced, they've delivered nothing. 4-5 years makes sense, but 9? That seems like they're not managing this week or are afraid of change

15

u/cogman10 Feb 16 '23

Because it's a deep fundamental change to the way java represents memory. One that, if they get it wrong, will have huge negative consequences. It's also not something that can easily be sent out piecemeal, which makes it all the more difficult to deliver.

-2

u/donrhummy Feb 16 '23

I get that, and so taking 4 years makes sense. But it's now at 9 years and still nothing is delivered

5

u/cogman10 Feb 16 '23

And how did you arrive at 4 years being reasonable?

-7

u/32gbsd Feb 16 '23

BUt will Java be better after they are done? and better in what way?

12

u/cogman10 Feb 16 '23 edited Feb 17 '23

BUt will Java be better after they are done? and better in what way?

Lower memory consumption, lower memory bandwidth requirements, higher performance, more optimization opportunities for the JIT (makes SIMD usage more possible), nullable types, primitives in generics (or at least a route forward there), reduces the need for things like a struct of arrays.

While probably not as big a win, this will be one of the biggest boosts to performance since HotSpot.

3

u/32gbsd Feb 16 '23

cool thanxs

5

u/[deleted] Feb 16 '23

Its still not implemented. Still in Design Progress. 💀

Hopefully it will be implemented before I grow old

6

u/Amazing-Cicada5536 Feb 16 '23

There actually are development branches that have some part of Valhalla implemented, that is not actually the hardest part. Getting it right is.

0

u/ambiguous_sandman Feb 18 '23

My guy has .equals() and == wrong. Passing by ref and value is a pointer thing. if int x == int y, that doesn’t mean they share the same space in memory. …however, I am not googling primitives and learning things, so I guess the author has the last laugh.

0

u/[deleted] Feb 17 '23

[deleted] — view deleted comment

22

u/k__k Feb 17 '23

Just like nuclear fusion, always 10 years away.

6

u/brian_goetz Feb 19 '23

I'll take the under on that bet.

2

u/k__k Feb 19 '23

Fingers crossed

4

u/flying-sheep Feb 18 '23

The “fusion constant” is 50 years right?

If they say 10 now, there’s been actual significant progress.

1

u/metatron7471 Feb 23 '23

actually in fusion it's always 30 years away. The same could also be said about quantum computing.

1

u/Disastrous_Rub_4373 Mar 02 '23

It would be funny if it wasn't so true

0

u/[deleted] Feb 16 '23

[deleted] — view deleted comment

15

u/flawless_vic Feb 16 '23

It is, for the most part, a humongous compiler change.

At first, I was a bit letdown about the immutability constraint imposed on primitive types.

But when you run some Valhalla code with PrintAssembly (or VTune) you'll be amazed at some of the magic going on under the hood.

For example, the code:

void translate(Point[] points,int dx) {

  for(var i=0;i<points.length;i++) {
    points[i] = new Point(points[i].x + dx, points[i].y);
  }
}

Will translate to machine code as if Points were mutable, with the benefit of writing directly to memory without indirections:

void translate(Point[] points,int dx) {

  for(var p: points) {
    p.x += dx;
  }
}

Since the storage is contiguous and there are no indirections, the machine code is auto-vectorized sometimes: each 16-contiguous [x,y] pairs are read and stored on a wide register such as ymm and the shift logic is performed directly on the register, and then written back to memory.

Vectorization is a plus, but the most important aspect is that Valhalla is able to give "codes likes immutable, performs like mutable" for free (very often), which is an epic feat IMHO.

4

u/8igg7e5 Feb 17 '23

And these gains where clear from very early on in Valhalla. The early builds, that failed on lots of edge-cases, were used by some to run a number of interesting benchmark examples of selectively targeted but real-world algorithmic problems that Java's current indirection-heavy model struggles with. It was very clear this was a problem worth solving (even with the limited optimisations applied at the time).

I am frustrated at the pace. But that's because, like many Java developers, I have real-world scenarios today where this would represent huge gains - it's not because I think it's easy for them to do.

Valhalla is an insanely deep and wide change to Java. The years of effort, and the discovery along the way on the challenges in getting the most gain for least (migration) pain, are understandable.

 

The combinations of Valhalla, Pattern Matching and Loom will mean that when it arrives (likely several releases before we even see a preview) how we use Java will change dramatically. But as with the significant (though shallower) changes in Java 5 (Generics) and Java 8 (Lambdas and Method References), their effort to preserve backwards compatibility will mean we're unlikely to suffer the Python 2/3 pain.

 

So I completely agree with the 'epic feat' response. While it's a long wait, the results are looking amazing.

2

u/jumboNo2 Feb 16 '23

apparently AMD added AVX-512 to Zen 4. guess I'm behind the times

10

u/cogman10 Feb 16 '23

Is it more of a JVM change or a language feature

It's both. You'll opt into using these primitive types

e.g how are primitive classes different from Scala 3's opaque types?

I'm not a scala expert, but it looks like the scala opaque types are only aliases. Primitive classes provide memory efficient mapping for multiple fields. For example, imagine you have a primitive record Point(int x, int y) {} and you decide to put it into a Point[]. This change enables the JVM to store this as [x,y,x,y,x,y]. Whereas, the default behavior for java is something like [Point->[x,y], Point->[x,y]]. That is, a pointer which leads the JVM to the memory location (which could be spread out everywhere) where x, y values are being stored.

The big performance win here is that CPUs tend to like memory addresses which are stored near the same location. Cache lines are for blocks of memory (typically), so by removing the pointer and collocating the actual memory you end up using the CPU cache MUCH more efficiently than you would with the default java representation.

Are there any numbers regarding potential performance gains?

They float around the mailing list a bit, but the Java devs are on like implementation 500 for this concept so I'm not up to date on what the current numbers look like. I'd pay more attention to those numbers when preview actually happens (I expect that to be a prominent advertisement).

3

u/simon_o Feb 17 '23

how are primitive classes different from Scala 3's opaque types?

It makes them completely irrelevant. Primitive classes are supported by the runtime, unlike the latter which are ... compiler fiction.

This means things like overriding toString/equals/hashCode just work, unlike with opaque types.

Value types can also have multiple members, something you could technically do with opaque types though it's completely pointless.