When to use ValueTask instead of Task and save precious memory in C#

Поділитися
Вставка
  • Опубліковано 30 вер 2024
  • Become a Patreon and get source code access: / nickchapsas
    Check out my courses: dometrain.com
    Hello everybody I'm Nick and in this video I show you how you can use ValueTask instead of Task in your .NET code and save memory. I will talk about one of the most common usecases that pretty much everyone can benefit from and release pressure from the garbage collector and the heap.
    Understanding the Whys, Whats, and Whens of ValueTask: devblogs.micro...
    Don't forget to comment, like and subscribe :)
    Social Media:
    Follow me on GitHub: bit.ly/ChapsasG...
    Follow me on Twitter: bit.ly/ChapsasT...
    Connect on LinkedIn: bit.ly/ChapsasL...
    #dotnet #await #async

КОМЕНТАРІ • 43

  • @aliascross
    @aliascross 3 роки тому +35

    Happy Birthday Nick 😁👍

  • @TedFanat
    @TedFanat 3 роки тому +40

    Finally, I found someone who uses a youtube channel to "walk walks and not just talk talks". Really interesting and good explained

  • @fr3ddyfr3sh
    @fr3ddyfr3sh 2 роки тому +10

    Damn, I’m doing C# for 10 years, and Nick knows his stuff.
    Short, to the point Videos, perfect for learning or bringing something back you knew once.

  • @bilbobaggins601
    @bilbobaggins601 3 роки тому +14

    Another banger of a video! Im still a beginner to C#, but i always watch your videos because they are instructional and interesting. One question, im not sure if you did it already but can you maybe make a video on delegates and events and where to use them. Im pretty sure i understand what they are but i struggle when to use them exactly and correctly. Cheers

  • @davidwilliss5555
    @davidwilliss5555 3 роки тому +4

    I have a question: You call the MemoryCache.Set method and pass a TimeSpan. I don't see a Set that takes a TimeSpan. Do you have an extension method that wraps that into a CacheItemPolicy with a sliding expiration or is there some other hidden magic involved?

  • @kumar315
    @kumar315 9 місяців тому

    cool stuff...just wanted to check if using a value task on unit tests would be a good idea?..Since we always mock the external API calls is it a good place to use value tasks everywhere with unit tests ?

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

    I don't understand when you say you can only await it once. If the cache expires you have to await it again... Isn't that "more than once"?

  • @roflex2
    @roflex2 3 роки тому +7

    I know you linked the why and how. But it would be nice to explain why you can't await it twice in the video.

    • @nickchapsas
      @nickchapsas  3 роки тому +4

      Yeah originally I thought the video would be bigger so I didn't explain it because I thought it would inflate it more but you're right, I could give a short explanation. Noted.

    • @roflex2
      @roflex2 3 роки тому +3

      @@nickchapsas love your videos and channel. Keep it up, you are the only person I've seen doing regular c# content

    • @OlegKosmakov
      @OlegKosmakov 3 роки тому

      As far as I am aware, since the ValueTask is used in high-performance low-overhead kind of scenarios, it is implemented in a way that it will reuse the ValueTask structure after the result is awaited, and later it might contain results of completely different calculation. That's why awaiting twice is undefined behavior.

    • @roflex2
      @roflex2 3 роки тому +1

      @@nickchapsas I'd love to see a topic on WPF/data binding with uncommonly used things eg BindingOperations.EnableCollectionSynchronization

    • @protox4
      @protox4 3 роки тому +3

      Short answer: because ValueTask can be backed by a custom-implemented IValueTaskSource which may not support multiple awaits.

  • @HuntsWorkshop
    @HuntsWorkshop 3 роки тому +1

    Great video man. Whoever thumbs down your videos just doesn't understand your genius. Their loss.

  • @kreide847
    @kreide847 3 роки тому +3

    Would it be possible for you to open a Discord server for your community?

    • @nickchapsas
      @nickchapsas  3 роки тому +4

      I am currently thinking about how I can set it up without being too much of a hassle. Once I sort that out I will let everyone know

  • @Netpercentage
    @Netpercentage 3 роки тому +1

    Hey dude... Your videos are the bomb... I have noticed the way you use benchmarks and how your code is refactored, its awosome. Man... I have been trying to get these benchmarks on my business logic layer but no luck... I have the business layer dll inside the console app of the same solution. There are no videos showing real scenario use cases. I want to refactor code in the business logic... Are you able to cook us one of them videos my guy... I believe that would be the first

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

    Hello Nick. Thanks for another amazing video.
    I want to ask, do you use Aspect Oriented Programming?

    • @nickchapsas
      @nickchapsas  3 роки тому +1

      I don't mainly because C# doesn't natively support it. I know there are a few ways around that but I think in general there are different ways to tackle what AOP brings to the table, which I find more appropriate in C#.

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

    Great Video ! thanks

  • @shustypl
    @shustypl 3 роки тому +1

    I am curious about the result of the same test if you would remove the caching. How much performance affects the use of ValueTask, where Task should be used for I/O operations (calling api or db).

    • @nickchapsas
      @nickchapsas  3 роки тому +1

      ValueTask is slightly slower by nature but always using An actually awaitable task will allocate a bit more if you used ValueTask there.

  • @rmcgraw7943
    @rmcgraw7943 28 днів тому

    I usually do…await vt.ConfigureAwait(false);
    or true, depending on my SyncContext needs.

  • @InshuMussu
    @InshuMussu 9 місяців тому

    ValueTask was always confusing for me but from 8:44 at your video solved my problem. thanks

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

    great job

  • @SealedKiller
    @SealedKiller 3 роки тому +1

    What font do you use in the editor? And if it's JetBrains Mono, how do you get the 0 with the line and not the dot?

  • @lincolntx98
    @lincolntx98 3 роки тому +1

    Great video Nick, as expected. I know that doesn't have any relation to coding, but would be cool a video showing your setup.

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

      Yeah, btw we already know that he is using Ryzen 9 3900X, haha.

    • @nickchapsas
      @nickchapsas  3 роки тому +4

      I don't think I will be making a dedicated video on this but I have create an amazon page with all my current gear in case you're interested in that: www.amazon.co.uk/shop/nickchapsas?listId=1E7ERJL49JJW

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

    I would say, that perhaps, if in your class, you have multiple private methods that are returning a Task, you can switch them up to a ValueTask, as the methods themselves will be used inside the object's lifecycle only.

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

    Thank you Nick 💕💕 wonderfull explanation.

  • @twiksify
    @twiksify 3 роки тому

    Would you say that using ValueTuple over Tuple has similar memory benefits? Assuming that it's used in a context that allows for stack allocation.

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

    Thanks for the video again NIck! We found several places on our codebase where we can avoid non necessary memory allocations

  • @MadMathMike
    @MadMathMike 3 роки тому

    Excellent demonstration! Thanks for sharing! 😊

  • @oganovdavid
    @oganovdavid 3 роки тому

    Happy birthday, Nick! Keep up 💪

  • @eliranbl
    @eliranbl 3 роки тому

    Thank you for all videos

  • @vasugupta1
    @vasugupta1 3 роки тому

    Great work

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

    Nice

  • @ValueLevit
    @ValueLevit 3 роки тому

    Thanks