Java Value Objects in Action with Valhalla - JEP Café #15

Поділитися
Вставка
  • Опубліковано 1 січ 2025

КОМЕНТАРІ • 111

  • @jorsol
    @jorsol 2 роки тому +45

    A lot of people keep talking about Loom and virtual threads, I personally find Project Valhalla more interesting, yet I fully agree that the mix of Loom, Valhalla, Amber, Leyden and Panama, is going to be a game changer for the Java ecosystem. Thanks for all of this!

    • @ambarishkapil8004
      @ambarishkapil8004 7 місяців тому +1

      Also going to make Java a whole lot complex.But I suppose, Java was never meant for the faint hearted!

  • @Talaria.School
    @Talaria.School 2 роки тому +17

    Merci José. Great job!
    Music settings are perfect and better than previous.👌

  • @prdoyle
    @prdoyle 2 роки тому +8

    Can't wait for the second part on primitive classes!

  • @MykolaPavluchynskyi
    @MykolaPavluchynskyi 2 роки тому +2

    As always - very useful and nicely presented. Thanks

  • @officialraylong
    @officialraylong Рік тому +1

    Value Objects are exciting!!!

  • @munawarabbas1636
    @munawarabbas1636 2 роки тому +1

    Thanks so much for sharing such a valuable update.After project Loom, Valhalla will make java more faster

  • @techtech7756
    @techtech7756 2 роки тому +1

    Contenu très utile !
    Merci beaucoup José !
    Je constate que les concepteurs des certifications OCP auront de la matière pour les prochains examens 🙂

  • @kostiapereguda
    @kostiapereguda Рік тому +2

    I didn’t quite get one point. With current restrictions on value abstract classes, how do they differ from value interfaces with default methods?

    • @roge0
      @roge0 Рік тому

      static non-final fields? idk

    • @pro_gamer29569
      @pro_gamer29569 Місяць тому

      Maybe multiple inheritance. You can implement many interfaces at same time

  • @Markus_included
    @Markus_included 7 місяців тому

    At 14:45 identity is mispelled as "identiy"

  • @hosseinmahdavipur7203
    @hosseinmahdavipur7203 2 роки тому +2

    is the benchmarking code available anywhere?

  • @KangoV
    @KangoV 2 роки тому +1

    Excellent as always. I think I'll have a play around with Valhalla. Looks interesting :)

  • @Wovi10
    @Wovi10 2 роки тому +2

    Very important question: where can I find that Java coffee cup? I'd love it but can't seem to find it.

  • @pablourbanohernandezvizcarra
    @pablourbanohernandezvizcarra 2 роки тому +4

    Great video, thanks for the effort, Valhalla looks like very interesting, I need to learn first Java 17 xD

    • @java
      @java  2 роки тому +2

      You should!
      PS: Thanks!

  • @davidhsv2
    @davidhsv2 2 роки тому +3

    Cool, I will finally be able to write readable code in LeetCode :P

  • @carldea
    @carldea 2 роки тому +1

    Great job José,
    Question: At 8:08, Why use a primitive keyword on the record type? Couldn't the compiler infer the Product's primitive attribute types as ints?

    • @JonHaa87
      @JonHaa87 2 роки тому +2

      The main point of primitives is that they can't be null as you're not using references anymore. So even if a record only contains primitives, the compiler can't set the record automatically to primitive itself, as that would cause a huge behaviour change. For example an empty array of that record type would no longer contain nulls.

    • @JosePaumard
      @JosePaumard 2 роки тому +4

      You can. The compiler doesn't do this inference.

  • @svalyavasvalyava9867
    @svalyavasvalyava9867 2 роки тому +1

    As always, great content, great video! After watching, I have two questions:
    1) Could you make a tutorial (or provide a link to some articles/videos) on how to measure performance, as you do?
    2) Why should we not use instance initializers in our code? Is it a performance hit or something else?
    Thanks in advance.

  • @bizoitz86
    @bizoitz86 2 роки тому +1

    I recall certain abstract classes and interfaces being neither identity or value unless explicitly specified; is this still the case?

  • @aharonha
    @aharonha 2 роки тому +1

    Thanks for a good and comprehensive video. I have a question about the behavior of generic classes: Is there a way to mark that a generic parameter is considered a value type?

    • @JosePaumard
      @JosePaumard 2 роки тому +1

      Good idea, I haven't try it. I guess that declaring a value based interface and then T extends ValueInterface would work?

  • @redcrafterlppa303
    @redcrafterlppa303 2 роки тому +2

    What is the use of "value abstract class" if you can't declare fields? Aren't they effectively less useful interfaces!? What reason lies to the no field inheritance rule? I know it has to do something with the object memory layout but I don't get the reason for the restriction.

    • @zhamed9587
      @zhamed9587 2 роки тому +2

      An abstract class can have static members as well as protected methods.

  • @bloody_albatross
    @bloody_albatross 4 дні тому

    I assume if you need to mutate an array of value objects you have to assign the array index with a newly constructed object. If the value object has multiple files, will the compiler optimize out the unnecessary copy of all the fields that didn't change? I still have to write all of them out manually?

  • @IllidanS4
    @IllidanS4 2 роки тому +4

    Finally Java could get close to C#.

    • @gsit80
      @gsit80 Рік тому

      I didn't know C# had value objects.

  • @AndreyLomakin
    @AndreyLomakin Рік тому

    If I got correctly, value classes are inlined in case of usage if C2 compiler if they are passed as an arguments. It means that we have got overhead of copying of data . So likely this optimisation will depend on size threshold. What about passing the stack pointer then with ability to convert value object into heap object if passed reference leaks the called method ? Are such optimizations considered ?

  • @pradimark
    @pradimark 2 роки тому +2

    can someone pleaes explain why the two loops approach is faster than the one loop way?

    • @JosePaumard
      @JosePaumard 2 роки тому +8

      It has to do with pointer chasing. The loops version keeps switching from one array to the other, which is an overhead.

    • @pradimark
      @pradimark 2 роки тому

      Makes a lot of sense! Thanks for the explaination

    • @vtvitibttboybobtottbo
      @vtvitibttboybobtottbo 2 роки тому +3

      After you loaded the first value from the array A, the rest of that array (or at least a part of it) goes to the CPU cache. Now if you switch to another array B, you basically will discard all that CPU cached data from the array A in order to load data from array B.
      So basically CPU tries to help you with performance by loading the next portion of the array that you currently reading. But as soon as CPU loads data in cache, you're just switching to another array. Now CPU needs to load data from another array to the cache. And so on. So performance is awful.
      This is basically the case when you do non-linear reads from the RAM. Which is always will come with hit to the performance. In order to avoid that, the most simple solution, is to make sure your program is always reading data from memory in linear and predictable way. For example, reading all data from the one array first (just doing subsequent reads from the one portion of the memory) and then switch to reading all data from another array (which is in another region of memory).
      You can learn about it from the this cppcon talk: ua-cam.com/video/BP6NxVxDQIs/v-deo.html - it talks about memory access patterns and how they're important for performance.

    • @vtvitibttboybobtottbo
      @vtvitibttboybobtottbo 2 роки тому

      And please don't believe me or anyone. Always trust information that you can verify. So I will recommend you to take code examples from that video (or from the talk I sent to you) and run a profiler and try to explain to yourself why it performs in a different way. There are a lot of surprises that you can find.
      P.S. CPU in reality doesn't have notion of arrays. It just loads in cache whatever is nearby in memory. Or tries to predict your read pattern and loads the "next data". That "next data" will not always be what you expected. But in the most cases, it is just whatever is nearby in memory towards direction of your reads.

    • @vtvitibttboybobtottbo
      @vtvitibttboybobtottbo 2 роки тому

      And keep in mind that any other READ operation from any other thread or the process on the same CPU core might affect which is currently in the CPU cache. For example, In Java there is Garbage Collector running in the background which can just stop execution of your program at any given time and "pollute" the CPU cache with its data.

  • @almazglaz
    @almazglaz 2 роки тому +1

    Are these benchmarks for "value record" or "primitive record" classes?

    • @bororobo3805
      @bororobo3805 2 роки тому +1

      He said primitives will be discussed next time.
      This discussion was about value record

    • @almazglaz
      @almazglaz Рік тому

      @@bororobo3805 Indeed it was said. Though I when I run the very same demonstrated benchmarks I get very different numbers, hence the question.

  • @engineersunit
    @engineersunit 2 роки тому +2

    Thanks for the video. Really helpful

  • @mathieuS276
    @mathieuS276 2 роки тому

    Thank you for these content
    How de you test it ? And do you use for test ?

    • @JosePaumard
      @JosePaumard 2 роки тому +1

      Tests and benches are done using JMH. More details -> openjdk.org/projects/code-tools/jmh/

  • @cobrab1978
    @cobrab1978 6 місяців тому

    Jose, why not use records as value objects? If value objects and records are inmutables.

  • @danhoward7697
    @danhoward7697 2 роки тому +2

    What's that error number in the benchmarks?

    • @31redorange08
      @31redorange08 2 роки тому +3

      Probably "standard error", a statistical metric. This has nothing to do with logical errors.

    • @JosePaumard
      @JosePaumard 2 роки тому +2

      Yes. That's exactly that. More details -> openjdk.org/projects/code-tools/jmh/

  • @gsit80
    @gsit80 Рік тому +1

    The ultimate Java evolution.
    WTF.

  • @pro_gamer29569
    @pro_gamer29569 Місяць тому

    I downloaded early access builds of Project Valhalla but can not use primitive classes or value classes. Did i something wrong or there are some configuration for using that classes.

    • @jack9171
      @jack9171 2 дні тому

      i think you have to enable early access features from your ide or pass arguments gor it when you compile your code

  • @VisruthCV
    @VisruthCV 2 роки тому

    Interesting!💚💙💜🤍❤🧡

  • @AleksandarT10
    @AleksandarT10 2 роки тому

    Great video and explanation! Keep up the good work and give us some more :D

  • @IndellableHatesHandles
    @IndellableHatesHandles 2 роки тому +2

    Is this just allocating on the stack, which is a feature that Java should've had from the beginning?

    • @stdprocedure
      @stdprocedure 2 роки тому

      primitive classes in this case, I don't think so about value classes
      also, if you put primitive types (even the primitive primitives like boolean, int) in a class field, you are always allocating

  • @alexandre8869
    @alexandre8869 2 роки тому

    Thanks a lot for this presentation. Does it means that the value objects will live in the Stack instead of the Heap?

    • @JosePaumard
      @JosePaumard 2 роки тому +3

      They can. This is for the JVM and optimizers to decide. For primitive types, they could live on the heap and in registers.

  • @xKeray
    @xKeray 2 роки тому

    Jose, do you think that for graphics and game development purposes vecmath classess such as Vec2 Vec3 Mat4 etc should be defined as value classes or primitive classes?

    • @bororobo3805
      @bororobo3805 2 роки тому +1

      Well, given the stated performance features, I think it'd be better to have them as primitives.

  • @danielgolan
    @danielgolan 2 роки тому

    Question, what about the record class, for example? Will it allow both value and identity?
    It'll make sense for it to be value only. What about backwards compatibility tho?

    • @JosePaumard
      @JosePaumard 2 роки тому

      Record is not a value based class as of now, in this EA release. Maybe in the future?

  • @mihirsawant8817
    @mihirsawant8817 2 роки тому

    Hello,
    What if I do Arrays.sum() for both arrays and then do division so any change will be seen as I am using jdks in built functions

  • @StephenBuergler
    @StephenBuergler 2 роки тому

    what's wrong with instance initilizers?

  • @nv-systems
    @nv-systems Рік тому

    Great video!

  • @kitkarson4226
    @kitkarson4226 2 роки тому

    Thanks Jose sir.

  • @Markus_included
    @Markus_included 7 місяців тому

    Why doesn't valhalla provide a mutable equivalent to primitive classes? I can understand why value classes are immutable, but I can see many cases where mutable non-reference/primitive classes could be useful. And it wouldn't cause the same problems as mutable value classes because there is only one reference to that object at any time because they're always implicitly copied

  • @NachtmahrNebenan
    @NachtmahrNebenan 2 роки тому

    I always wondered if a *primitive value record can have String fields*? Doesn't matter if there would be a new primitive String class.

    • @JosePaumard
      @JosePaumard 2 роки тому +3

      The problem is that String wraps an array that is not limited in size. So it would become impossible to inline if it's too big.

    • @zhamed9587
      @zhamed9587 2 роки тому

      @@JosePaumard I'm a bit confused. Why does it prevent inlining since String itself is a reference, and would take up a one word field in the record?

  • @victorpinasarnault9135
    @victorpinasarnault9135 2 роки тому

    Thank you

  • @arnaudclaudel9774
    @arnaudclaudel9774 2 роки тому

    thanks a lot!

  • @_whywhathow
    @_whywhathow 2 роки тому

    value object(value record) get better performance than reference objects(identity record)
    1. value objects are non modifiable
    2. can't use synchronization

  • @VuLinhAssassin
    @VuLinhAssassin Рік тому

    The new "value" keyword might break a lot of codes, I hope it ends up like var reserved keyword

  • @kitkarson4226
    @kitkarson4226 2 роки тому +1

    so, is record not value class? Should we say "value record" ?? 😢

  • @Valder1989
    @Valder1989 Рік тому +1

    Yeah, great stuff. On the other hand C# has had value types since the very beginning.

    • @HAAAAAA614
      @HAAAAAA614 Місяць тому

      I mean ... c# was released 5 years after Java and they pretty much copied all the good parts of Java and had the privilege of knowing what would be problematic or not. They've also broken backwards compatibility multiple times so if they didn't have value types, that would have been really bad

  • @tarearnaut3502
    @tarearnaut3502 2 роки тому

    if java could be a human it would be this guy

  • @jNayden
    @jNayden 2 роки тому

    I would like more btw if you do not pause/skip the java sip :) it is strange that you are almost going to drink the coffee then a little blink and you return the cup :+)) don't know :) maybe me personally will like to still have the same music but not the video gap :)

  • @edmaphis9805
    @edmaphis9805 2 роки тому +1

    I think I've found José's cafe.
    ua-cam.com/video/MYPVQccHhAQ/v-deo.html

  • @ChrisBrown-dn3tf
    @ChrisBrown-dn3tf 2 роки тому

    I laughed when he said never to use instance initializers, I didn't think they were that bad

  • @yerlibilgin2
    @yerlibilgin2 2 роки тому

    Thanks.
    But I would leave that cup down, and stop drinking coffee between breaks (or cut it from video)..

  • @brixomatic
    @brixomatic 2 роки тому +7

    I don't really like this at first glance.
    Performance is cool, but the constraints on class construction and inheritance feel like an inconsistency in the language.
    Plus, I feel that between Records and Primitive classes the Value classes don't really feel necessary.
    If I can have primitive classes and records, what is the big benefit of value classes between the two? Records give up mutability and enforce a canonical constructor for language elegance, pattern matching, etc. Primitive classes will give up mutability and identity for a high performance, dense memory layout and probably put constraints on inheritance, like value classes do. That feels like it leaves value objects as some in-between solution that does a bit of everything, but nothing properly, is hardly needed, but makes the language more cluttered with unimportant things one needs to learn in order to understand it properly.

    • @andrewlalis
      @andrewlalis 2 роки тому +2

      I think they should have just added a C-style struct to Java and called it there. The complex constraints really limit the applicability of these new language features, when so many people are familiar with the classic struct format.

    • @zhamed9587
      @zhamed9587 2 роки тому +1

      @@andrewlalis C-style structs come with their own challenges. See what C# for example how they have structs, ref structs, readonly structs, etc. which also give you different behaviors not dissimilar from value and primitive classes.

    • @bobbycrosby9765
      @bobbycrosby9765 Рік тому +1

      My understanding is they have both primitive and value classes primarily for two reasons: tearing and default values.
      Value objects let you use null, primitive objects don't. Not every object is going to have a "sensible" default. 0 kinda makes sense for integers. But what about a Date? What is a "sensible" default? January 1st, 1970?
      Primitive objects also will re-introduce tearing as a problem. 64 bit values used to be able to "tear": reading and writing wasn't atomic, so a concurrent thread could see partially written primitive types. This isn't a problem on the modern JVM, but it's a tradeoff of performance here, one they weren't willing to make with primitive objects - for performance reasons, they can tear. Value objects won't have this problem, as reading and writing a reference is atomic.
      As far as just using C-style structs... the design of this seems particularly tailored towards opening up options for optimization. I'm not sure if they could get the same guarantees with C-style structs.

    • @brixomatic
      @brixomatic Рік тому

      @@bobbycrosby9765 well, I can't comment on tearing, but since records are already immutable (and I expect primitives to be too), should that not eliminate tearing in the first place?
      And as for defaults, we have the "Optional" monad for that. It might not be perfect for parameters, but it's a better concept than null references and method overloading can avoid some of the ugly regarding optional parameters.

    • @bobbycrosby9765
      @bobbycrosby9765 Рік тому +1

      @@brixomatic that only works if you write your whole program as immutable. A plain old object with a primitive field will be subject to tearing - e.g. when you assign that field to a new value.
      Note that each primitive type will also have a value type automatically created for you.
      For value objects, part of the concern may have also been backwards compatibility. My understanding is they plan to migrate several existing classes to value objects. Value types are much more backwards compatible than primitive types.
      I'm also not sure if they're quite so willing to abandon null.
      Bringing this together a bit, if Optional were changed to a primitive type, then assigning it could be subject to tearing - an identity object in a multi threaded environment could be accessed in a way such that one of its Optional fields wasn't internally consistent.

  • @andy_lamax
    @andy_lamax 2 роки тому

    and if you don't know what is an instance initializer, thats perfect. Never use that any way.
    🤣🤣🤣🤣🤣🤣🤣

  • @romank9482
    @romank9482 2 роки тому

    damn, it is really hard to understand this type of english, can u please provide somebody else? i'm not native speaker to listen this without subtitles

    • @adrianrostkowski2862
      @adrianrostkowski2862 2 роки тому +19

      I am also not a native speaker and my English is not very good.
      But his English is perfect and easy to understand.

    • @kitkarson4226
      @kitkarson4226 2 роки тому +13

      I am not a native english speaker. I can understand just fine. His english is too good!

    • @brixomatic
      @brixomatic 2 роки тому +6

      Not a native speaker here, but I think his English is comprehensible enough. You just need to get a bit used to it.

    • @romank9482
      @romank9482 2 роки тому

      ​@@brixomatic definitely disagree, intonation is really nasty, even when i'm listen to black people where their english is really too much native with swallowing a lot of words :-) i can understand them much clearly then this speaker. probably you know this accent because you are from the same country. but this english is really bad to hear.

    • @brixomatic
      @brixomatic 2 роки тому

      @@romank9482 accents really are something you learn to understand and not every accent is equally hard to understand for everyone, I get that. I have a lot of Indian co-workers and it took years for me to understand some of them properly (you know, the type of calls us where you say "could you send this to me via email, so I won't forget", so you don't have to admit you understood absolutely nothing?) This accent here is a piece of cake in comparison. And also some Brits are near impossible for me to understand. Yes he's flowing some words together and his intonation is very soft, and sometimes he pronounces the last syllable instead of the first one, but that's something that you can get used to very quickly, once you understand the pattern, because that's pretty consistent.