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

Поделиться
HTML-код
  • Опубликовано: 15 ноя 2024

Комментарии • 106

  • @jorsol
    @jorsol Год назад +43

    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 5 месяцев назад +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 Год назад +17

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

  • @prdoyle
    @prdoyle Год назад +8

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

  • @MykolaPavluchynskyi
    @MykolaPavluchynskyi Год назад +2

    As always - very useful and nicely presented. Thanks

  • @techtech7756
    @techtech7756 Год назад +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 🙂

  • @munawarabbas1636
    @munawarabbas1636 Год назад +1

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

  • @officialraylong
    @officialraylong Год назад +1

    Value Objects are exciting!!!

  • @hosseinmahdavipur7203
    @hosseinmahdavipur7203 Год назад +2

    is the benchmarking code available anywhere?

  • @KangoV
    @KangoV Год назад +1

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

  • @svalyavasvalyava9867
    @svalyavasvalyava9867 Год назад +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.

  • @Wovi10
    @Wovi10 Год назад +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 Год назад +4

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

    • @java
      @java  Год назад +2

      You should!
      PS: Thanks!

  • @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 11 месяцев назад

      static non-final fields? idk

  • @aharonha
    @aharonha Год назад +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 Год назад +1

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

  • @IllidanS4
    @IllidanS4 Год назад +3

    Finally Java could get close to C#.

    • @gsit80
      @gsit80 11 месяцев назад

      I didn't know C# had value objects.

  • @davidhsv2
    @davidhsv2 Год назад +2

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

  • @engineersunit
    @engineersunit Год назад +2

    Thanks for the video. Really helpful

  • @mathieuS276
    @mathieuS276 Год назад

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

    • @JosePaumard
      @JosePaumard Год назад +1

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

  • @AleksandarT10
    @AleksandarT10 Год назад

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

  • @carldea
    @carldea Год назад +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

      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 Год назад +4

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

  • @gsit80
    @gsit80 11 месяцев назад +1

    The ultimate Java evolution.
    WTF.

  • @alexandre8869
    @alexandre8869 Год назад

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

    • @JosePaumard
      @JosePaumard Год назад +3

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

  • @bizoitz86
    @bizoitz86 Год назад +1

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

  • @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 ?

  • @cobrab1978
    @cobrab1978 5 месяцев назад

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

  • @Markus_included
    @Markus_included 5 месяцев назад

    At 14:45 identity is mispelled as "identiy"

  • @almazglaz
    @almazglaz Год назад +1

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

    • @bororobo3805
      @bororobo3805 Год назад +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.

  • @redcrafterlppa303
    @redcrafterlppa303 Год назад +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

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

  • @danhoward7697
    @danhoward7697 Год назад +2

    What's that error number in the benchmarks?

    • @31redorange08
      @31redorange08 Год назад +3

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

    • @JosePaumard
      @JosePaumard Год назад +2

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

  • @nv-systems
    @nv-systems Год назад

    Great video!

  • @VisruthCV
    @VisruthCV Год назад

    Interesting!💚💙💜🤍❤🧡

  • @xKeray
    @xKeray Год назад

    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 Год назад +1

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

  • @kitkarson4226
    @kitkarson4226 Год назад

    Thanks Jose sir.

  • @StephenBuergler
    @StephenBuergler Год назад

    what's wrong with instance initilizers?

  • @Valder1989
    @Valder1989 Год назад +1

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

  • @mihirsawant8817
    @mihirsawant8817 Год назад

    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

  • @tarearnaut3502
    @tarearnaut3502 Год назад

    if java could be a human it would be this guy

  • @Markus_included
    @Markus_included 5 месяцев назад

    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

  • @danielgolan
    @danielgolan Год назад

    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 Год назад

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

  • @pradimark
    @pradimark Год назад +2

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

    • @JosePaumard
      @JosePaumard Год назад +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 Год назад

      Makes a lot of sense! Thanks for the explaination

    • @vtvitibttboybobtottbo
      @vtvitibttboybobtottbo Год назад +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: ruclips.net/video/BP6NxVxDQIs/видео.html - it talks about memory access patterns and how they're important for performance.

    • @vtvitibttboybobtottbo
      @vtvitibttboybobtottbo Год назад

      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 Год назад

      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.

  • @arnaudclaudel9774
    @arnaudclaudel9774 Год назад

    thanks a lot!

  • @victorpinasarnault9135
    @victorpinasarnault9135 Год назад

    Thank you

  • @IndellableHatesHandles
    @IndellableHatesHandles Год назад +2

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

    • @stdprocedure
      @stdprocedure Год назад

      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

  • @ChrisBrown-dn3tf
    @ChrisBrown-dn3tf Год назад

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

  • @_whywhathow
    @_whywhathow Год назад

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

  • @NachtmahrNebenan
    @NachtmahrNebenan Год назад

    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 Год назад +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 Год назад

      @@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?

  • @VuLinhAssassin
    @VuLinhAssassin Год назад

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

  • @jNayden
    @jNayden Год назад

    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 Год назад +1

    I think I've found José's cafe.
    ruclips.net/video/MYPVQccHhAQ/видео.html

  • @yerlibilgin2
    @yerlibilgin2 Год назад

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

  • @kitkarson4226
    @kitkarson4226 Год назад +1

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

  • @andy_lamax
    @andy_lamax Год назад

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

  • @brixomatic
    @brixomatic Год назад +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

      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 Год назад +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.

  • @romank9482
    @romank9482 Год назад

    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 Год назад +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 Год назад +13

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

    • @brixomatic
      @brixomatic Год назад +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 Год назад

      ​@@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 Год назад

      @@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.