Task vs ValueTask: When Should I use ValueTask?

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

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

  • @andreikashin
    @andreikashin 3 года назад +27

    It is like answering to question What is ValueTask, others return a Task and only Brian returns a VALUE immediately. Thank you, Brian.

    • @BrianLagunas
      @BrianLagunas  3 года назад

      Very Clever! I love it!

    • @Nk54fr
      @Nk54fr 3 года назад

      Haha I see what you did there ;D

  • @ivandrofly
    @ivandrofly Год назад +6

    Best explanation on the internet in this topic

  • @jacfarjama467
    @jacfarjama467 3 года назад +10

    Finally someone who knows how to explain clearly. I watched other videos and couldn't comprehend why we need this ValueTask. But thanks to your clear explanation in 10 mins it all makes sense. Thanks a lot.

  • @ChrisBoot
    @ChrisBoot 3 года назад +3

    The best explanation I've seen for valuetask, explained in language I understood. Thank you

    • @BrianLagunas
      @BrianLagunas  3 года назад

      I'm glad I could help clarify it. Thanks for watching

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

    I have spend almost the whole evening to understand how they differ through Microsoft blogpost, and I couldn't figure it out. Thanks for your video, I have completely understood.

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

      I’m glad I could help. Thank you for watching

  • @amrelsher4746
    @amrelsher4746 3 года назад +1

    The best explanation I've seen for valuetask

  • @EminoMeneko
    @EminoMeneko 3 года назад +2

    Holy cow!
    I was wondering the difference. So I googled it, found an article on MS blog. Did not get it. At some point there was an exemple but I could not compare because, thought there was a chapter on Task before, there was no equivalent of the example. But probably mostly because it downs clarity in too many details I could not comprehend. Maybe because of a lack of background.
    But here it is exactly what I need to comprehend. Right to the point as I like, good example. Simple definition.
    By 4mn of the video (even before) I got the point of those.
    Very much thank you for that.

    • @BrianLagunas
      @BrianLagunas  3 года назад

      I’m happy I could help. Thanks for watching

  • @xavhow
    @xavhow 3 года назад +1

    You explained the best, out of a few that I have seen on RUclips.

  • @fabrizziocht
    @fabrizziocht 2 года назад +1

    Finally a definitive explanation, thank you very much for your help!

    • @BrianLagunas
      @BrianLagunas  2 года назад

      You’re very welcome. Thank you for watching

  • @CRBarchager
    @CRBarchager 7 месяцев назад

    Awesome explaination. I failed to understand other explainations of this type but yours was clear and easy to understand.

  • @udaysuddhala3512
    @udaysuddhala3512 3 года назад +1

    This is why I consider u as my guru🙏🙏 explanation is crystal clear 👌

    • @BrianLagunas
      @BrianLagunas  3 года назад +1

      Thank you so much for the kinds words sir

  • @eduard.schaefer
    @eduard.schaefer 3 года назад +5

    That was a really good explanation, Brian!

  • @teraformerr
    @teraformerr 6 месяцев назад

    Another brilliant video, hope you will come back to teach us more! Thank you

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

    thank you so much this cleared it up for me. Great explanation right to the point very easy to understand. First time I found your channel. It was the video after a Nick chapsas video in the search. Who I also enjoy. For that reason I have subscribed. I’m looking forward to future videos. Thank you Brian.

  • @ВолодимирДідух-н3р

    What a teacher! Thank you a lot for your excellent explanation!

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

      Thank you so much for the kind words

  • @user-mr-m12312
    @user-mr-m12312 3 года назад +3

    Great explanation Brian, thank you!
    ...But wait a second, where is "roll dat intro"?

    • @BrianLagunas
      @BrianLagunas  3 года назад +3

      Thank you for watching. Yeah, I'm dropping the intro stuff as I dont think it provides any value. I'll just get to the point 😁

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

    Excellent summary.

  • @mykolakriukov1252
    @mykolakriukov1252 2 года назад +1

    Very clear explanation, thank you!

  • @KamelJabber1
    @KamelJabber1 Месяц назад

    This was a great explanation! Thank you!

  • @davidwhite2011
    @davidwhite2011 3 года назад +2

    Compiler hot paths are code execution paths in the compiler in which most of the execution time is spent, and which are potentially executed very often.
    The reason for avoiding (heap) allocations in these code paths is that allocations may trigger a garbage collection, which may cause sudden, extreme performance deteriorations. These should obviously be avoided in very commonly executed code paths. Stack Overflow.

  • @aleckendall1253
    @aleckendall1253 2 года назад +1

    Excellent explanation!

  • @Pocvocem
    @Pocvocem 3 года назад +1

    Thanks for explanation, this is the clearest.

  • @CodingKurzgeschichten
    @CodingKurzgeschichten 3 года назад +1

    Very nice video and great demonstration.

  • @njezichigozie709
    @njezichigozie709 2 года назад +1

    Thanks a lot on this Brian.

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

    Good presentation.

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

    Good explanation. Thank you!!

  • @mahesh_rcb
    @mahesh_rcb 3 года назад +3

    Ctrl +shift + f Task
    Replace all with ValueTask 😅
    Most of the time I use Task
    Nice explanation 👍🏻👍🏻

    • @BrianLagunas
      @BrianLagunas  3 года назад

      Haha! When in doubt, Task is the go to. Thanks for watching

  • @ievheniiierokhin1677
    @ievheniiierokhin1677 3 года назад +1

    Thank you! I don't know that it could bring that performance, especially memory usage

  • @vkg.codefactory
    @vkg.codefactory 3 года назад +1

    Great explaination

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

    Thanks for the great explanation. What happens if we use valuetask when the return type is always a task?

  • @kiranshetty8342
    @kiranshetty8342 3 года назад

    Hi Brian , grt video n best explanation for value task. Thanks for your help .
    I have an question might sound silly n apologise in advance.
    What exactly synchronisation context mean . Why ppl say AspNet core doesn’t support Synchronization context ??

    • @BrianLagunas
      @BrianLagunas  3 года назад +1

      Every thread has a context associated with it. The SynchronizationContext represents the location where your code is executed. It enables you to queue a task onto another context and it facilitates communication between threads. This is common used when communicating between a background thread and a UI thread (for example in WPF or WInForms). ASP.NET doesn't have a UI thread to be concerned with, and it literally does not have a SynchronizationContext class.

  • @briannielsbergh
    @briannielsbergh 2 года назад +1

    Isn't the cache filled in the first method, meaning that the second benchmark is using cache only?

  • @11Karpovich11
    @11Karpovich11 3 года назад +1

    best explanation! Thank you!

  • @strandloper
    @strandloper 3 года назад +1

    Thanks, that's a nice clear explanation.

  • @timb00
    @timb00 3 года назад +1

    Thanks man, finally got it!

  • @distinguishedmoments2277
    @distinguishedmoments2277 3 года назад +1

    I love your videos mate! Thanks a lot

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

    Hello Brian, Thanks so much for your work. I have the question about ValueTask limitations:
    You said we should not run the valuetaskMethod concurrently, but when i check the Paralle.ForeachAsyn method it accept a delegate that return ValueTask. so the delegate is called multiple times ? can you help me to clearify that. Thanks so much.

  • @Tamer_Ali
    @Tamer_Ali 3 года назад +1

    Thanks a lot Brian 👍

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

    Could you make a video explaining how attributes work and how to create custom ones?

  • @karsten600
    @karsten600 3 года назад +1

    Good video, I have a question though. I can't picture any instance where I'd await a task more than once, can you give an example?

    • @BrianLagunas
      @BrianLagunas  3 года назад +1

      Loading files from disk is a common one.

    • @karsten600
      @karsten600 3 года назад

      @@BrianLagunas Thanks :)

  • @CleberCarvalhoTI
    @CleberCarvalhoTI 3 года назад +1

    Ottima spiegazione.

  • @christianf7740
    @christianf7740 2 года назад +1

    This is great stuff.

  • @MarvinKleinMusic
    @MarvinKleinMusic 3 года назад

    Hi Brian,
    could you create a video on best practises to manage data access within a blazor/c# app, without Entity Framework.
    I am think of this for a long time now but I don't find any solution where I say this is the perfect one. E.G. why should I use a service to access the data instead of a static method of my actual object?

    • @BrianLagunas
      @BrianLagunas  3 года назад

      That's a really broad topic. I'll have to think about that one. I will say that you should never use static anything for data access.

    • @MarvinKleinMusic
      @MarvinKleinMusic 3 года назад

      @@BrianLagunas yes. I searched for this topic a while and I can only find examples regarding EF online. But I cannot use this in our project. I'm limited to writing the SQ myself. I am looking for best practises for Create/Update/Delete/Get + other method to fetch data from the database which require data from my model to run.

  • @shreyasacharya
    @shreyasacharya 3 года назад +1

    Awesome!

  • @marvinalone
    @marvinalone 2 года назад

    simple, clear explaination and good benchmark/demo, thanks you ~ but I think this video can be shorter, 5mins top.

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

    eXcellently explained.

  • @joakimcarlsson7826
    @joakimcarlsson7826 3 года назад +1

    Would love an explanation around yield return

  • @Jarenaut
    @Jarenaut 3 года назад

    Nice explanation! I've a question for you... I started programming many years ago and since university studies or higher level professional training most of my programming teachers reinforce me the idea of using descriptive name and explicitly typed variables. As you can see on that time var was "forbidden". C#3.0 introduced var and C#8.0 has made nullable vars too... In dictionaries or local variables could help in readability and so..., instead you must initialize var variables and cannot assign an anonymous method to var or declare for example... Give me some light please... What's the point here¿? Thanks Brian!

    • @BrianLagunas
      @BrianLagunas  3 года назад +1

      My advice would be, use var if you like it. I like it and use it for most of my local variables. It helps keep my code clean and concise. I like to reduce redundant type information. It's shorter, easier to read, and it encourages descriptive names for your variables. Var is extremely helpful when dealing with LINQ queries too. But, it's just a personal preference. Use what you are comfortable with. The important part is that you be consistent in your code base.

  • @FB-eb6tx
    @FB-eb6tx 6 месяцев назад +1

    Excelent video! The best explanation!

  • @tomnolane
    @tomnolane 3 года назад +1

    thanks

  • @randompoints
    @randompoints 3 года назад

    Can we consider ValueTask for singleton service method?

    • @BrianLagunas
      @BrianLagunas  3 года назад +1

      If the method can run either async or sync and you have benchmarked your method to determine that you would benefit from using it, then sure.

  • @Kumar-yn4bs
    @Kumar-yn4bs 2 года назад

    Make a sample code of application using Ivaluetaskasync any example please

  • @philippelhaus
    @philippelhaus 3 года назад

    Sometimes i need to make sure a method is only executed once at a time by only one thread and kills everyone else that wants to get in. so i add a global bool as some sort of lock at the beginning and set it back once i exit the method. I know there’s also mutex’s for these issues, but i wonder whats really the best solution

    • @BrianLagunas
      @BrianLagunas  3 года назад

      If I understand you correctly, I'm pretty sure this is what the "lock" statement is for.

  • @andreapellegrini2794
    @andreapellegrini2794 3 года назад +1

    Thanks you man

  • @Rob_III
    @Rob_III 3 года назад

    Wait... are the benchmark results consistent even if you run them a few times? Without having tested this myself, so just going from what I see in the video (@6:30): Both GetReposAsyncTask and GetReposAsyncValueTask use the same cache object. So isn't it possible the first benchmark (which happened to be the first method) populates the cache and the second method never even goes out to Github and ONLY serves results from cache (since the other method already populated the cache)? That would also explain **a** (maybe not the entire ~20Mb) difference in allocated memory.

    • @BrianLagunas
      @BrianLagunas  3 года назад +1

      No. I tested this myself to be sure. The Benchmark harness executes each test individually as a new instance of the benchmark class.

    • @BrianLagunas
      @BrianLagunas  3 года назад +1

      To be extra sure, I even changed the order in which the test invoke (ValueTask first) and the results were the same.

    • @Rob_III
      @Rob_III 3 года назад +1

      @@BrianLagunas Ah, ok. I just had a hunch the _cachedRepos was maybe shared between the tests which is definitely at the very least a "gotcha" / something to look out for / be mindful of. Nice to hear and have confirmed in this case it's not. Thanks for the video!

    • @Rob_III
      @Rob_III 3 года назад +1

      @@BrianLagunas Great! Again, nice to have this confirmed / double-checked ;-)

    • @ronaldmackee3401
      @ronaldmackee3401 3 года назад

      @@BrianLagunas if the access to the cache were async too (as in distributed cache), then will ValueTask still make a difference? You can argue that the result involves async calls regardless.

  • @imixal
    @imixal 3 года назад

    Can you show, when using ValueTask can be worst than Task?

    • @BrianLagunas
      @BrianLagunas  3 года назад +1

      I actually covered it in this video. If you have a method that whose "hot path" is not pulling from cache, then you are better off just using Task

  • @TonoNamnum
    @TonoNamnum 2 года назад

    When having large numbers like 100000 it's easier to read it if you write it as 100_000. The compiler will make that 10000 and it's easier to read in my opinion

    • @BrianLagunas
      @BrianLagunas  2 года назад

      I guess I’ve been doing this so long, I don’t ever use that syntax. My eyes are just used to reading those numbers 😁. I’ll keep that In mind for demos going forward. Others my not read those numbers as easily. Thanks for the feedback.