Task vs ValueTask: When Should I use ValueTask?

  Рет қаралды 21,251

Brian Lagunas

Brian Lagunas

Күн бұрын

Пікірлер: 115
@andreikashin
@andreikashin 3 жыл бұрын
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 Жыл бұрын
Best explanation on the internet in this topic
@BrianLagunas
@BrianLagunas Жыл бұрын
Thank you so much.
@jacfarjama467
@jacfarjama467 3 жыл бұрын
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.
@BrianLagunas
@BrianLagunas 3 жыл бұрын
Glad you liked it!
@ChrisBoot
@ChrisBoot 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 Жыл бұрын
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 жыл бұрын
The best explanation I've seen for valuetask
@BrianLagunas
@BrianLagunas 3 жыл бұрын
I’m glad it was helpful
@CRBarchager
@CRBarchager 8 ай бұрын
Awesome explaination. I failed to understand other explainations of this type but yours was clear and easy to understand.
@EminoMeneko
@EminoMeneko 3 жыл бұрын
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 жыл бұрын
You explained the best, out of a few that I have seen on KZbin.
@BrianLagunas
@BrianLagunas 3 жыл бұрын
Thank you for the kind words
@eduard.schaefer
@eduard.schaefer 3 жыл бұрын
That was a really good explanation, Brian!
@BrianLagunas
@BrianLagunas 3 жыл бұрын
Thank you.
@fabrizziocht
@fabrizziocht 2 жыл бұрын
Finally a definitive explanation, thank you very much for your help!
@BrianLagunas
@BrianLagunas 2 жыл бұрын
You’re very welcome. Thank you for watching
@aleckendall1253
@aleckendall1253 2 жыл бұрын
Excellent explanation!
@BrianLagunas
@BrianLagunas 2 жыл бұрын
Thank you very much
@LoganDunning
@LoganDunning Жыл бұрын
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.
@debtpeon
@debtpeon Жыл бұрын
Excellent summary.
@BrianLagunas
@BrianLagunas Жыл бұрын
Thank you for watching
@nathanzimmerman6941
@nathanzimmerman6941 Жыл бұрын
Good presentation.
@BrianLagunas
@BrianLagunas Жыл бұрын
I appreciate you
@udaysuddhala3512
@udaysuddhala3512 3 жыл бұрын
This is why I consider u as my guru🙏🙏 explanation is crystal clear 👌
@BrianLagunas
@BrianLagunas 3 жыл бұрын
Thank you so much for the kinds words sir
@teraformerr
@teraformerr 7 ай бұрын
Another brilliant video, hope you will come back to teach us more! Thank you
@KamelJabber1
@KamelJabber1 2 ай бұрын
This was a great explanation! Thank you!
@mykolakriukov1252
@mykolakriukov1252 2 жыл бұрын
Very clear explanation, thank you!
@BrianLagunas
@BrianLagunas 2 жыл бұрын
Thanks for watching
@ВолодимирДідух-н3р
@ВолодимирДідух-н3р Жыл бұрын
What a teacher! Thank you a lot for your excellent explanation!
@BrianLagunas
@BrianLagunas Жыл бұрын
Thank you so much for the kind words
@njezichigozie709
@njezichigozie709 2 жыл бұрын
Thanks a lot on this Brian.
@BrianLagunas
@BrianLagunas 2 жыл бұрын
You’re welcome
@Pocvocem
@Pocvocem 3 жыл бұрын
Thanks for explanation, this is the clearest.
@BrianLagunas
@BrianLagunas 3 жыл бұрын
Thank you for watching
@user-mr-m12312
@user-mr-m12312 3 жыл бұрын
Great explanation Brian, thank you! ...But wait a second, where is "roll dat intro"?
@BrianLagunas
@BrianLagunas 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 😁
@vkg.codefactory
@vkg.codefactory 3 жыл бұрын
Great explaination
@BrianLagunas
@BrianLagunas 3 жыл бұрын
Thanks for watching
@meetrajasekar
@meetrajasekar Жыл бұрын
Good explanation. Thank you!!
@BrianLagunas
@BrianLagunas Жыл бұрын
Thank you for watching
@timb00
@timb00 3 жыл бұрын
Thanks man, finally got it!
@BrianLagunas
@BrianLagunas 3 жыл бұрын
Great to hear
@briannielsbergh
@briannielsbergh 3 жыл бұрын
Isn't the cache filled in the first method, meaning that the second benchmark is using cache only?
@CodingKurzgeschichten
@CodingKurzgeschichten 3 жыл бұрын
Very nice video and great demonstration.
@BrianLagunas
@BrianLagunas 3 жыл бұрын
Thanks for watching.
@FB-eb6tx
@FB-eb6tx 7 ай бұрын
Excelent video! The best explanation!
@BrianLagunas
@BrianLagunas 7 ай бұрын
Thanks for watching
@joakimcarlsson7826
@joakimcarlsson7826 3 жыл бұрын
Would love an explanation around yield return
@BrianLagunas
@BrianLagunas 3 жыл бұрын
Thanks for the feedback
@CleberCarvalhoTI
@CleberCarvalhoTI 3 жыл бұрын
Ottima spiegazione.
@BrianLagunas
@BrianLagunas 3 жыл бұрын
Grazie
@11Karpovich11
@11Karpovich11 3 жыл бұрын
best explanation! Thank you!
@BrianLagunas
@BrianLagunas 3 жыл бұрын
Thanks for watching
@distinguishedmoments2277
@distinguishedmoments2277 3 жыл бұрын
I love your videos mate! Thanks a lot
@BrianLagunas
@BrianLagunas 3 жыл бұрын
Thanks for watching
@adnanmaqboolkhan7522
@adnanmaqboolkhan7522 Жыл бұрын
Thanks for the great explanation. What happens if we use valuetask when the return type is always a task?
@christianf7740
@christianf7740 2 жыл бұрын
This is great stuff.
@BrianLagunas
@BrianLagunas 2 жыл бұрын
I appreciate the kind words
@karsten600
@karsten600 3 жыл бұрын
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 жыл бұрын
Loading files from disk is a common one.
@karsten600
@karsten600 3 жыл бұрын
@@BrianLagunas Thanks :)
@milos5247
@milos5247 Жыл бұрын
Could you make a video explaining how attributes work and how to create custom ones?
@Tamer_Ali
@Tamer_Ali 3 жыл бұрын
Thanks a lot Brian 👍
@BrianLagunas
@BrianLagunas 3 жыл бұрын
Very welcome
@ievheniiierokhin1677
@ievheniiierokhin1677 3 жыл бұрын
Thank you! I don't know that it could bring that performance, especially memory usage
@BrianLagunas
@BrianLagunas 3 жыл бұрын
Thanks for watching
@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 жыл бұрын
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.
@strandloper
@strandloper 3 жыл бұрын
Thanks, that's a nice clear explanation.
@BrianLagunas
@BrianLagunas 3 жыл бұрын
Glad it was helpful!
@davidwhite2011
@davidwhite2011 3 жыл бұрын
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.
@BrianLagunas
@BrianLagunas 3 жыл бұрын
Isn't that what I said? 😀
@shreyasacharya
@shreyasacharya 3 жыл бұрын
Awesome!
@BrianLagunas
@BrianLagunas 3 жыл бұрын
Glad you think so!
@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 жыл бұрын
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 жыл бұрын
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 жыл бұрын
@@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 жыл бұрын
@@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.
@randompoints
@randompoints 3 жыл бұрын
Can we consider ValueTask for singleton service method?
@BrianLagunas
@BrianLagunas 3 жыл бұрын
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.
@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.
@peternguyen9382
@peternguyen9382 2 жыл бұрын
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.
@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.
@coolfyb
@coolfyb 2 жыл бұрын
eXcellently explained.
@BrianLagunas
@BrianLagunas 2 жыл бұрын
Thank you so much
@Kumar-yn4bs
@Kumar-yn4bs 2 жыл бұрын
Make a sample code of application using Ivaluetaskasync any example please
@tomnolane
@tomnolane 3 жыл бұрын
thanks
@BrianLagunas
@BrianLagunas 3 жыл бұрын
Thanks for watching
@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 жыл бұрын
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.
@andreapellegrini2794
@andreapellegrini2794 3 жыл бұрын
Thanks you man
@BrianLagunas
@BrianLagunas 3 жыл бұрын
Thank you for watching
@imixal
@imixal 3 жыл бұрын
Can you show, when using ValueTask can be worst than Task?
@BrianLagunas
@BrianLagunas 3 жыл бұрын
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
@mahesh_rcb
@mahesh_rcb 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
@marvinalone
@marvinalone 2 жыл бұрын
simple, clear explaination and good benchmark/demo, thanks you ~ but I think this video can be shorter, 5mins top.
@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.
Adding a Prism Login Screen
8:55
Brian Lagunas
Рет қаралды 10 М.
C# Yield Return: What is it and how does it work?
15:09
Brian Lagunas
Рет қаралды 58 М.
Sigma Kid Mistake #funny #sigma
00:17
CRAZY GREAPA
Рет қаралды 30 МЛН
Try this prank with your friends 😂 @karina-kola
00:18
Andrey Grechka
Рет қаралды 9 МЛН
“Don’t stop the chances.”
00:44
ISSEI / いっせい
Рет қаралды 62 МЛН
The Best Band 😅 #toshleh #viralshort
00:11
Toshleh
Рет қаралды 22 МЛН
Which do I use, ConfigureAwait True or False?
6:53
Brian Lagunas
Рет қаралды 39 М.
C# Async Yield Return: IAsyncEnumerable will change your life!!!
10:13
Settling the Biggest Await Async Debate in .NET
14:47
Nick Chapsas
Рет қаралды 148 М.
Where are types allocated in .NET and why people get it so wrong
14:35
Use Arc Instead of Vec
15:21
Logan Smith
Рет қаралды 157 М.
How slow is MediatR really?
15:51
Nick Chapsas
Рет қаралды 61 М.
Are events in C# even relevant anymore?
16:19
Nick Chapsas
Рет қаралды 172 М.
What is Span in C# and why you should be using it
15:15
Nick Chapsas
Рет қаралды 262 М.
Sigma Kid Mistake #funny #sigma
00:17
CRAZY GREAPA
Рет қаралды 30 МЛН