Fix Closure Issues in 10 Minutes and Boost Performance

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

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

  • @git-amend
    @git-amend  19 дней назад +16

    Happy Holidays! Hope everyone is having a break of some kind! Link in description to Blog post about Heap Allocations Viewer! 👍

  • @klevialushi571
    @klevialushi571 18 дней назад +4

    Thanks a lot man, literally the only channel who posts videos about advanced stuff that i didn't even know they exist. Keep it up bro!

    • @git-amend
      @git-amend  17 дней назад +1

      Appreciate that! I'm glad you found it useful.

    • @klevialushi571
      @klevialushi571 17 дней назад

      @git-amend Of course, you post high quality stuff bro, you deserve more.

  • @dezikso9358
    @dezikso9358 19 дней назад +10

    Awsome as always. Have you considered making a series in which you make a simple game architecture and explain how different patterns coexist together?

    • @git-amend
      @git-amend  19 дней назад +14

      Yes, I have. It's a good idea and probably something we can do in the coming year!

    • @johnanthony44
      @johnanthony44 18 дней назад +1

      I'd love to have a series of videos showing how to create robust modules to power any game you wish to make? Audio Manager, Save System, Health/Weapon/Inventory Manager, etc.

  • @jackieczech1777
    @jackieczech1777 19 дней назад +2

    Adam, Thank You for all the great videos and sharing your knowledge. Please keep it up! It's awesome!

  • @albertogr91
    @albertogr91 2 дня назад

    Your content is always amazing! I love your channel ♥

  • @ankitwarkade6920
    @ankitwarkade6920 17 дней назад

    Like always, so informative and insightful video about advanced programming.🤩🤩

    • @git-amend
      @git-amend  17 дней назад

      Thanks for the kind words!

  • @raymk
    @raymk 18 дней назад

    Another tool for my arsenal. THANKS!!!

  • @nexgen.graphics
    @nexgen.graphics 19 дней назад +6

    Dude, how are you writing code at this GOD level 🔥🔥... It looks so clean and neat to read even without comments. You literally playing around with it just like that. Thanks for explaining the closure thing in detail. I can't find a better tutorial than this.

    • @git-amend
      @git-amend  19 дней назад +2

      Thank you so much for the kind words! I’m really glad you found the explanation clear and helpful!

  • @dragonsnackproductions
    @dragonsnackproductions 19 дней назад +2

    I will say it as simple as I can -> "Best YT content GOTY+ Award" :D thank you!

    • @git-amend
      @git-amend  19 дней назад +1

      Wow, thanks!

    • @dragonsnackproductions
      @dragonsnackproductions 19 дней назад

      @@git-amend I constantly upgrade my passion project based on your tips :D Have a backlog of improvements now ;) It has to run on Quest 3 Android and PC VR. All good so far but will get better. Thanks!

  • @kpm25
    @kpm25 19 дней назад

    Thanks as always!!

  • @rohitsarkar3650
    @rohitsarkar3650 17 дней назад

    Hi! I have been watching your videos for quiet some time now. It's very useful. I have a request for the new year if possible. Can you make a video on how to properly use Unit testing in Unity and what are the different ways of test automation ?

    • @git-amend
      @git-amend  17 дней назад +1

      I have one video about testing already, here: ruclips.net/video/Wh27sG0DXzU/видео.html
      But we'll probably cover it again in 2025.

    • @rohitsarkar3650
      @rohitsarkar3650 16 дней назад

      @git-amend great! Thank you so much. And Merry Christmas to you and all your members :). Happy Holidays!

  • @MrDcx23
    @MrDcx23 17 дней назад

    Thank you, thank you, thank you! Great video, but i have one question.
    Question:
    When you say: "Compiler generates a closure class at runtime to store the captured variable on the Heap"
    I was curious, isn't compiler smart enough to generate closure struct like we did to keep the things stack allocated?

    • @git-amend
      @git-amend  17 дней назад

      Great question! The compiler's default behavior opts for a class because closures often need to persist beyond their declaring scope, such as when stored in delegates or other objects, which requires heap allocation. In our example, the Closure struct works because it explicitly encapsulates the captured variable (context) within the struct itself, and as long as the struct remains unboxed (e.g., stored in a List), it avoids heap allocation while still maintaining the correct behavior. The compiler can't assume this is always safe or feasible for all closure use cases, such as when the closure outlives its declaring scope or is passed around as a delegate, so it uses a class by default.

  • @ryaz4704
    @ryaz4704 17 дней назад

    I think there was a few c# proposals that are similar to this. Have they been accepted for future c# versions?

    • @git-amend
      @git-amend  17 дней назад

      Yes, that's correct - but I think they will always be just proposals, unfortunately. Most people, even in .NET, are still rolling their own when it comes to real-time or high-performance systems.

  • @정동우-n2x
    @정동우-n2x 19 дней назад +1

    In other words, you mean to manage the captured variables in the form of structures, right?
    So what are the benefits of using the Lambda? I actually have a little bit of a question.

    • @정동우-n2x
      @정동우-n2x 19 дней назад

      teacher's code is to build a structure to replace the "closures," right? I understood it as the concept of avoiding the GC.
      Then, can I pool this, ltoo like object pool?

    • @git-amend
      @git-amend  19 дней назад +1

      Yes, you’re correct-the approach involves managing captured variables explicitly in a structure to replace standard closures, thereby avoiding GC allocations caused by implicit heap allocations. Lambdas are still useful because they offer concise, readable syntax and integrate seamlessly with C#'s delegate and LINQ features. However, in performance-critical code, like Unity’s `Update` loop or frequent callbacks, avoiding heap allocations is essential, which is why we replace lambdas with explicit structures. And yes, you can pool these structures, much like object pooling, to reuse them efficiently and further reduce GC pressure, but maybe add a 'Reset' method to the struct so you can set new values.

    • @정동우-n2x
      @정동우-n2x 19 дней назад

      @@git-amend Thank you so much. You always give me new horizons. I'm studying using your code, and I really respect you.
      You and Smack are my teachers.

    • @정동우-n2x
      @정동우-n2x 19 дней назад

      @@git-amend You have a GOOOD talent who can easily explain it to a bad-headed person like me.

  • @gladiko2364
    @gladiko2364 19 дней назад +1

    ctrl+shift+f: "event" - 113 matches, "delegate" - 34 matches, "Action

    • @git-amend
      @git-amend  19 дней назад

      Nothing wrong with that many delegates... just review to see if there are any hidden closures!

  • @leos-clockworks335
    @leos-clockworks335 19 дней назад +1

    This is nice, but I'm not sure about adding all of this complexity because of closure.
    Did you benchmark this on some heavy delegate usage? I do wonder if this is something noticeable.
    I am working on a pretty big project and the current bottlenecks are order of magnitude larger than this.
    Just wondering if, for example, starting a new project with such a struct would be actually worth it. I do worry more about anonymous delegates and memory leaks.
    And great content as usual! Though I think having a benchmark for this specific case could prove helpful, as I am sure newer programmers would take a few things as gospel, but it's important to understand the pros and cons.

    • @marlonruvalcaba386
      @marlonruvalcaba386 19 дней назад +1

      Profile is the answer each project is different and sometimes the compiler does a lot of optimizations.
      Because of how my projects are architecture, I use a lot of delegates and they aren't my bottlenecks.

    • @leos-clockworks335
      @leos-clockworks335 18 дней назад

      @@marlonruvalcaba386
      Of course, each project is different, and you need to profile to understand your needs.
      I doubt any project will ever have this as the actual bottleneck, I am just wondering how much you could actually get from doing this vs the complexity it will add.

  • @damonfedorick
    @damonfedorick 19 дней назад

    Nice!

  • @Fitz0fury
    @Fitz0fury 19 дней назад +1

    6:00 - OK FINE! * begins downloading rider*
    any other plugins i should start with?

    • @git-amend
      @git-amend  19 дней назад +3

      The CognitiveComplexity can be useful, it will tell you if your code is getting overly complex. I also use the Classic UI plugin because I prefer the old look of the IDE. Key Promoter X plugin will help you learn the keyboard shortcuts, but you will want to disable it after a while. If you are coming from VS Code, I would also go to Settings > Keymap and choose "VS Code" there.

    • @Fitz0fury
      @Fitz0fury 19 дней назад

      @@git-amend Keypromoter is a huge tip thanks!

  • @nicolasgerard2476
    @nicolasgerard2476 14 дней назад

    Im not sure I understand the purpose of this. Storing a structs as a member of a class will still allocate memory on the heap. So unless you destroy and re-create new actions each frames, the memory automatically allocated by the standard closure will not be thrown away by the GC. I guess it would be a better solution if your code was running the the update loop, but as it stand, it doesnt really bring much to the table, or am I missing something ? I could see one or two use-cases, but it would be fairly situational don't you think ?
    Regardless, pretty good video, always makes me stop to reconsider what I think I know about programming, which is always a good think!

  • @Bankoru
    @Bankoru 19 дней назад

    In my experience it's not really going to affect performance unless we're talking allocations in the thousands.
    It's like the whole debate on whether or not it's ok to use LINQ, those monads cost.

  • @Damian_DH
    @Damian_DH 3 дня назад

    Nah bro but how good u are my man. Can u explain what does invoke() do internally?. Because i remember my teacher got mad at me for using it but it is been years and i couldn't figure it out why it was. I think he said it was expensive and i was like bro if it is expensive dont blame me blame the one who made it that way im just using what i have on my hands.

  • @Fitz0fury
    @Fitz0fury 19 дней назад

    "compulsory first comment" comment