Writing C# without allocating ANY memory

2022 ж. 30 Нау.
141 516 Рет қаралды

Check out my courses: dometrain.com
Become a Patreon and get source code access: / nickchapsas
Hello everybody I'm Nick and in this video I will show you how you can use features such Span, ReadOnlySpan and stackalloc to write allocation free C# that is very fast. This video will just focus on a single example but I will explain every decision as I go and hopefully you will find areas where you can use the technique you see here to optimize your own code.
Don't forget to comment, like and subscribe :)
Social Media:
Follow me on GitHub: bit.ly/ChapsasGitHub
Follow me on Twitter: bit.ly/ChapsasTwitter
Connect on LinkedIn: bit.ly/ChapsasLinkedIn
Keep coding merch: keepcoding.shop
#csharp #dotnet #performance

Пікірлер
  • Definitely want to see more content like this, thanks Nick for constantly expanding my mind. I just want to say I'm probably about 10 years older than you but I have no shame in saying that you are a great teacher and I learn a lot from you, I always promote your videos at work.

    @RoughSubset@RoughSubset2 жыл бұрын
    • I also promote his video's at work. 😁

      @brentsteyn6671@brentsteyn66712 жыл бұрын
    • Yep, I third this, lol

      @shawnelswick5715@shawnelswick57152 жыл бұрын
    • I equally do share his videos with my colleagues and other programmer friends.

      @akeemaweda1716@akeemaweda17162 жыл бұрын
    • Me Too, also promoting his courses

      @RuiNogueiraHB@RuiNogueiraHB2 жыл бұрын
  • Great example! Well done Nick! BTW: when your process runs in 32 Bit mode, the default thread stack size is 1MB. In 64 Bit mode it's 4MB.

    @IAmFeO2x@IAmFeO2x2 жыл бұрын
    • Source?

      @myname2462@myname24626 ай бұрын
  • I would be interested in seeing json serialization/deserialization techniques that are more memory efficient. Especially when receiving back large json payloads that need to be deserialized to an object.

    @joephillips6634@joephillips6634 Жыл бұрын
    • exactly what I was thinking about!

      @ofiry@ofiry Жыл бұрын
    • I reckon you’d use System.Text.Json as it is built with similar optimisations as this video.

      @SudhanshuMishraTheOne@SudhanshuMishraTheOne9 ай бұрын
  • No matter how much I think I know, I always learn something from your videos! Thanks for continuing to put out great content!!

    @brianm1864@brianm18642 жыл бұрын
  • One thing the Primeagen always mentions about garbage collected languages is that the performance gains of having fewer allocations won't show up in spot tests like this, because the garbage-collector pass didn't get timed in the stopwatch. So the optimized methods clocked in way faster but also shorten the timing on the garbage collector. Not sure if that also applies to C# like it does to JS

    @Nate77HK@Nate77HK11 ай бұрын
  • the new span and memory types are making the old C programmer in me happy

    @7th_CAV_Trooper@7th_CAV_Trooper2 жыл бұрын
  • C# is starting to feel more and more like Rust. Thanks for taking the time to walk us through this.

    @macgyverswissarmykni@macgyverswissarmykni2 жыл бұрын
    • Add first-class sum types and late interface implementation/orphan rules to the lang, and ideally offer niceties like the One True Constructor and a true no-null mode, and C# becomes very attractive indeed

      @berylliosis5250@berylliosis52502 жыл бұрын
    • @@berylliosis5250 i give another name to this, feature hell. The language is getting more and more complex, if i want rust, i use rust instead, is far more performant from scratch.

      @rafaelrosa3841@rafaelrosa38412 жыл бұрын
    • @@rafaelrosa3841 That's a fair point, but the whole "small and simple language" ship has _very_ much already sailed for C#. As for using Rust instead - sure, but C# has different design constraints that are genuinely better at some things, so providing the "common" things that are really nice makes it nicer to use when necessary. (E.g. for use with Godot, which has a threading model that broadly disagrees with safe Rust)

      @berylliosis5250@berylliosis52502 жыл бұрын
    • @@rafaelrosa3841 Rust has the borrow checker and with that another memory management model. C# is and will always be a Garbage Collected Language. This makes for very different ergonomics, ever if they share features. One's hell is the other persons heaven. The recent additions to the language have all been aimed at reducing boilerplate and reducing error potential. And for all of them, I think one thing applies: If you read the code using these features you'd be hard pressed to assume it does something else than what it does.. And that's imho the most important thing.

      @RedHotBagel@RedHotBagel2 жыл бұрын
    • @@RedHotBagel I don't agree with this, more features to do the same things differently only will kill the homogeneous code base to a more extensive and hard to read language, for me give a different name to a Apple killing one p (aple) Wich mean now i writing less to describe the same thing will not help, I already know what apple means it's just now have two ways of describe it, and more ways to describe the same thing will increase the learning curve of the language.

      @rafaelrosa3841@rafaelrosa3841 Жыл бұрын
  • Some tips on this video: 1) there is a Guid.TryWriteBytes() method that acts without allocation 2) you can use string.Create() to fast allocate the strings based on Span, instead of calling string constructor

    @volan4ik.@volan4ik. Жыл бұрын
  • I'd definitely like to see more videos like this. Great example in this video. Keep up the great content.

    @malsmith69@malsmith692 жыл бұрын
  • Prior to 2019, allocation-free code was *essential* in Unity (the game engine), which ran on a 12-year-old version of Mono with single-threaded garbage collection. Having too much garbage would result in stuttering. To work around this issue, in 2015 Unity introduced "IL2CPP", a compiler which takes the compiled C# IL code and compiled it into C++, and then compiled *that* into a native binary. No, this is not a joke; it's a real thing that still exists. However in 2019 they finally upgraded to a version of Mono with multi-threaded GC, so IL2CPP is less useful. It's still a good idea to minimize allocations, though.

    @BlueRaja@BlueRaja2 жыл бұрын
    • I don't think that was or is the primary reason for IL2CPP....

      @RedHotBagel@RedHotBagel2 жыл бұрын
    • Yeah definitely not the only reason for IL2CPP. iOS sticks out, as you can currently ONLY build Unity games for iOS using IL2CPP. Android supports the Mono runtime or IL2CPP.

      @IgnoreSolutions@IgnoreSolutions Жыл бұрын
    • still an older version of mono, but at least not ancient

      @arjix8738@arjix8738 Жыл бұрын
    • Now you have burst wich converts IL/.NET bytecode to highly optimized native code using LLVM, but to get the most of it, you have to write in a subset of c# called high performance c#. It is c# but with no reference types.

      @wenacabros9676@wenacabros9676 Жыл бұрын
    • Now with .NET 7 we have Native AOT! Even without it, RyuJit can output some very optimized assembly if you know what you're doing.

      @ryanshea5221@ryanshea52216 ай бұрын
  • This is amazing! Learnt a lot of things that I’ve heard about but didn’t really know, keep up the good work!

    @sylascoker1483@sylascoker14832 жыл бұрын
  • Nick, I really value your content. Every time I listen to you talk about some topic, I realize I don't know enough, but I finish the video knowing more than I did before. Cannot thank you enough

    @josephizang6187@josephizang61872 жыл бұрын
  • I never use structs but thanks to this video I will use them a lot more. I am starting to see the real power of structs when you just use them to compute stuff and don't store them in memory. thanks Nick great video!

    @TonoNamnum@TonoNamnum2 жыл бұрын
  • Brilliant as usual. Thank you. Your videos push people to make their minds more efficient

    @VasilyPavlik@VasilyPavlik2 жыл бұрын
  • Great video. I learned something very valuable today! Now I've got to look up stack vs heap. Hopefully Nick Chapsas is in the results. :)

    @jancarius101@jancarius1012 жыл бұрын
  • Thanks for great video! I love Span and Memory.

    @siavash2176@siavash21762 жыл бұрын
    • Memory, array, and string builder pools

      @siavash2176@siavash21762 жыл бұрын
  • This is great video! please do more of those! I never thought about the size of the guid. I am willing to pay for a course with this level of details!

    @AltaiiSinan@AltaiiSinan9 ай бұрын
  • Another thing worth mentioning is if you want your URL-safe base64 to be RFC 4648 compliant, you need to swap the replacements. Pluses should be replaced with hyphens, not underscores. Slashes should be replaced with underscores.

    @WhoCaresCertainlyNotMe@WhoCaresCertainlyNotMe Жыл бұрын
  • Yes. More like this. Very good optimisation walkthrough.

    @jchandra74@jchandra742 жыл бұрын
  • I really like using Span and Memory when appropriate. I remember using Memory for creating a zip archive from file streams. With a maximum compression I was able to make my application 2.5 times faster because of using Memory (2.5 seconds instead of 6-7)

    @antonmartyniuk@antonmartyniuk2 жыл бұрын
  • The most insightful KZhead channel!! Please more content like this, also Unsafe code :) Thanks very very much Nick!!!

    @DaviGn@DaviGn2 жыл бұрын
  • I wanna see more of those kind of videos for sure, thanks for sharing.

    @ricardoandrade8529@ricardoandrade8529 Жыл бұрын
  • Wow, I can already think of where to use it at work. Great stuff!

    @inzyster@inzyster2 жыл бұрын
  • Thank you! For both this video and the one on the Span.

    @NagyTami@NagyTami2 жыл бұрын
  • Thank you for sharing your knowledge!

    @ProgramandoDePie@ProgramandoDePie2 жыл бұрын
  • About the constant characters; I'd even consider it worse to create character constants mapping them to their names. You already know that '=' is the equals sign, and it's the glyph itself you care about when replacing them in base 64. It would be good practice if that '=' character represented some special sentinel that acts like a magic value, a special value encoded in the same data type.

    @AlFasGD@AlFasGD2 жыл бұрын
    • I was just going to say the same. It would make sense to define PaddingReplacement, PlusReplacement and SlashReplacement because that's where you're deviating from / overriding the standard and you may want to make it configurable. But this way you just make the reader doubt if the constants actually contain what their names claim to contain.

      @benjaminschug5572@benjaminschug55722 жыл бұрын
    • I assume we all know and agree that const’s provide some benefits including being included in the assembly at build time and also being able to view occurrences referencing the const, allowing easy renaming/ better refactoring as opposed to hard coded literals.

      @NedMorse@NedMorse2 жыл бұрын
    • @@NedMorse I agree on the occurrence and refactoring argument, but it doesn't matter if you have a const field, const variable, or simply use the literal. The resulting assembly code is the same. IMO as these constants are private and they are only used once, it is a choice of style, and I tend prefer AlFas's and Benjamin's suggestion.

      @IAmFeO2x@IAmFeO2x2 жыл бұрын
    • I'm going to concur with AlFas and Benjamin Schug as well. Characters are self-identifying constants already. If you're going to give them another name, give them the name of what they're representing. Replacing `'=`` with `Equals` is just obfuscation. Replacing it with `Base64PaddingCharacter` tells the reader why you're appending an equals without actually needing to tell them what the character even is.

      @hhaavvvvii@hhaavvvvii Жыл бұрын
  • extremely high level content, thanks!

    @paulofernandoee@paulofernandoee Жыл бұрын
  • Props to you, you really make it look easy!

    @underdev@underdev7 ай бұрын
  • Wonderful .. truly high value stuff ... keep it up. 👍

    @Explorest@Explorest Жыл бұрын
  • This is absolutely great! This'd be an amazing way to optimize azure function apps

    @wva6809@wva68092 жыл бұрын
  • Cool content, Thanks for your great videos

    @reza.kargar@reza.kargar2 жыл бұрын
  • I would be interested in seeing if what difference replacing new string() with string.Create() would make in both memory and performance.

    @zummygummi@zummygummi2 жыл бұрын
    • Of course it depends on hardware, OS and .NET version, but my tests (i7-9850H, win 10, .net 6) shown that string.Create is slower. No difference in memory allocation.

      @hasmich@hasmich2 жыл бұрын
  • Well done! Thank you!

    @samwheat3357@samwheat33574 ай бұрын
  • This is very useful! Thank you for sharing..

    @marna_li@marna_li2 жыл бұрын
  • I don't use C# anymore but I'm impressed how far it's gotten since I used it many years ago.

    @insertoyouroemail@insertoyouroemail2 жыл бұрын
  • First of all, I just discovered your channel and I think it is really good and informative. Kudos. Now, as an old C developer (now a manager :P ) the "allocate memory" concept rang so many bells in my mind. I have a general comment on this. If you want speed, you need to go low. If you do all this you may as well go writting unsafe in my opinion. Still it is great for people to see these kind of tutorials in order to actually see how things work!

    @Kikeron@Kikeron2 жыл бұрын
  • Great video nick !!!!

    @jbravobr@jbravobr2 жыл бұрын
  • Thank you for teaching us this

    @m3xpl4y@m3xpl4y10 ай бұрын
  • Love how you emphasized "when you need to"...

    @SpaceShot@SpaceShot2 жыл бұрын
  • This is great! Thanks a lot.

    @KonstantinKuda@KonstantinKuda2 жыл бұрын
  • Great video! But what if I want to remove some characters from a Span? And what if I want to replace a sequence of chars with one specific char?

    @sanderdam7655@sanderdam76552 жыл бұрын
  • Thank for the video, Nick. It’s a pity that Span doesn’t work with async methods. (Can’t be members of heap objects which async state machine is)

    @antonsmolkov751@antonsmolkov7512 жыл бұрын
    • The state machine is a struct in release mode, it’s just not a ref struct

      @nickchapsas@nickchapsas2 жыл бұрын
  • Excelente content!!

    @alexpablo90@alexpablo902 жыл бұрын
  • very informative!

    @tehsimo@tehsimo2 жыл бұрын
  • Awesome content!!

    @expertreviews1112@expertreviews11122 жыл бұрын
  • Amazing content. More like this please

    @Cristian-ek7xy@Cristian-ek7xy11 ай бұрын
  • Hi Nick. Amazing video as always. Do you have any safe vs unsafe code video for C#? That would really help us folks out. Thanks.

    @vivekkaushik9508@vivekkaushik9508 Жыл бұрын
  • I write different type of C# apps, worked since VS 2001 with C#, I had to use strange memory allocations twice and that was for a multichannel (80+) audio streaming application.

    @asicdathens@asicdathens2 жыл бұрын
  • I reallly like this kind of content, pitty i didnt run into this channel earlier

    @Lmao-ke9lq@Lmao-ke9lq2 жыл бұрын
  • Is extracting the strings to constants necessary? The compiler performs automatic string interning on literals and should only allocate once for the life of the program.

    @drewnichols7723@drewnichols77232 жыл бұрын
    • It's a style thing. An ordinary idea in programming is to avoid "magic strings" in your methods, and have all constant values at the top. As you said it doesn't do anything at all to impact speed or memory consumption.

      @AndersJohansen92@AndersJohansen922 жыл бұрын
    • ​@@AndersJohansen92 As a style thing, this video shows exactly how NOT to do it correctly. A couple of points: - Replacing '/' with `Slash` is akin to replacing `42` with `FourthyTwo`. It doesn't provide any value and definitely doesn't respect the idea behind the no-magic-literals rule which is to make sure that all of your literals are *described* in your code. A better name for `Slash` would be `Base64_Value63` which describes what the code expects the variable to contain. - The cast to byte should also reuse this variable and not redeclare it from '/' again to highlight the relationship between the char and byte version. In this example, one could decide to replace the '/' with another char but forget to do it on the other way around. This would prevent decoding from working correctly. - The fact that the compiler already handles this for you is not mentioned in the video. This means that most people will watch this video and think that those const extractions actually helps with the performance of this code, which is a very common myth. - '+' comes before '/' in base 64, so they should be declared in that order as well. In fact, a proper variable name would also highlight this.

      @EtienneMaheu@EtienneMaheu2 жыл бұрын
    • @@EtienneMaheu Awesome points man. This youtuber is a total noob, he should not have a youtube channel. You would do a much better job.

      @ZelenoJabko@ZelenoJabko2 жыл бұрын
    • @@ZelenoJabko 🤡

      @TedThomasTT@TedThomasTT2 жыл бұрын
    • This is because of rule #1: You write code for developers, not for computers. While defining Equals = '=' is kinda pointless, it comes from a good habit where you leave not constant unchecked, so you fan find wherever you use them. Try finding where use used = as a constant with "Find in Files"

      @ErdoganKurtur@ErdoganKurtur2 жыл бұрын
  • Really videos like this opens yours eyes on you do wrong 😅. Thank you

    @djsito1000@djsito1000 Жыл бұрын
  • You’re literally god of c#, thanks for existing

    @mattaku9430@mattaku9430 Жыл бұрын
  • Great video. I was actually discussing url safe Id's with my professor today say..do you have tutorials for ASP.NET and Memory management in C# I really want to dive into this

    @eyassh6747@eyassh67472 жыл бұрын
  • well done!

    @ivaniliev93@ivaniliev932 жыл бұрын
  • wow! thats cool! thank you!

    @3263927@3263927 Жыл бұрын
  • Love it.

    @urbanelemental3308@urbanelemental33082 жыл бұрын
  • That's next level stuff

    @JeanFrancoisDesrosiers@JeanFrancoisDesrosiers2 жыл бұрын
  • Tiktokers: Say something without saying it. Nick: Access something without allocating it.

    @kaksisve4012@kaksisve40122 жыл бұрын
  • Hey Nick, How do we preserve benchmarked code and keep code duplication low? Do we create these methods in test project?

    @sajagjain7169@sajagjain71692 жыл бұрын
  • That youtube link example 🤦‍♂, it got me

    @Grenefeld@Grenefeld2 жыл бұрын
    • Wow, you took the time to type that out and got punished 🤣

      @BillyBraga@BillyBraga2 жыл бұрын
  • Up until a few weeks ago, it'd been a long time since I'd touched C#, and I've only recently been doing Windows Forms in Mono on Linux. I'd spent a long time working on C/C++ and microcontroller assembly, so seeing these highly performant improvements to C# is very exciting! I'm loving the safe stackallocs to Span! If only it were implemented in Mono. 😢

    @c4ashley@c4ashley2 жыл бұрын
    • You don't need Mono. .NET is fully cross platform.

      @nickchapsas@nickchapsas2 жыл бұрын
    • @@nickchapsas Yeah but Windows Forms isn't. And yeah I know Avalonia is a thing, but I just wanted to quickly whip up this personal project in something I was familiar with. (In retrospect, maybe I should've spent the time to learn Avalonia and the newer .NET and C# features, but alas.)

      @c4ashley@c4ashley2 жыл бұрын
    • @@c4ashley You can use MAUI instead of WinForms.

      @nickchapsas@nickchapsas2 жыл бұрын
    • @@nickchapsas I wish. It's still unsupported on Linux, and the linux-maui community fork is a total bust in my case. Maybe one day soon. Or maybe if I spend long enough trying to solve installation problems. For now, Mono is fine. A bit buggy, a bit dated, but at least it actually works. Anyway, this is all tangental; I was just saying I like the features you're pointing out in this video, and I'm excited at the prospect of using them for future (non-Mono) projects! 😀

      @c4ashley@c4ashley2 жыл бұрын
  • seeing chained replaces... i think i know where this is going lol. great content.

    @kaiserbergin@kaiserbergin2 жыл бұрын
  • Hey Nick, I feel like I've become pretty proficient in safe memory optimizations (which usually is more than enough). Especially since span got added. However, I never really trained the unsafe muscle and I was wondering if you can point me towards some good videos / blogs to start looking into that?

    @marvinbrouwer459@marvinbrouwer4592 жыл бұрын
    • Same for me :)

      @renauddanniau676@renauddanniau6762 жыл бұрын
    • Le dot

      @EddieDemon@EddieDemon2 жыл бұрын
  • Tip for writing allocation free code: At least in Rider you can set a different highlight color for struct types. I have structs green and class orange. Additionally the `Heap Allocations Viewer` plugin is really great.

    @JulianMoschuring@JulianMoschuring2 жыл бұрын
    • Just because something is a value type it doesn't mean it will be a stack allocation so the colour highlighting advice is misleading. HAV is good though

      @nickchapsas@nickchapsas2 жыл бұрын
    • @@nickchapsas no, but knowing if something is a value or a ref type is essential to be able to reason about allocation and performance characteristics and having different highlights makes this a lot easier.

      @JulianMoschuring@JulianMoschuring2 жыл бұрын
    • I do this too. Most types are the standard teal color, but structs are a neon greenish color. (dark theme).

      @Sahuagin@Sahuagin9 ай бұрын
  • Nice!

    @agustinbasilio5447@agustinbasilio54472 жыл бұрын
  • Amazing

    @OlaBacker@OlaBacker2 жыл бұрын
  • Could you make a video about how dispose works (IDisposable) and how to implement it correctly?

    @jonas_hoel@jonas_hoel2 жыл бұрын
    • Keep in mind there's also IDisposableAsync. Dispose is used to release resources. Disposable objects should be put in a using statement when possible. The whole point of IDisposable is to use it in a using statement so it will get called when the using statement ends, even if an exception gets thrown.

      @briankarcher8338@briankarcher83382 жыл бұрын
  • Will introducing a variable for indexers' calls in switch statements improve the performance?

    @ShakirovRuslan@ShakirovRuslan2 жыл бұрын
  • Do we can update base64span array without creating finalChars? Iterate and update special chars.

    @DmitryV_HF@DmitryV_HF2 жыл бұрын
  • I like your low_level optimization videos in particular, but why are there no built-in URI-safe GUID methods which use safe characters as their native lexicon?

    @CharlesBurnsPrime@CharlesBurnsPrime2 жыл бұрын
  • Also it would be even faster if you convert to/from base64. It's just a lookup.. You were walking through the bytes/chars for replacement. You may use that time you to convert with a lookup table..

    @denizcancgsar2810@denizcancgsar28104 ай бұрын
  • is it possible to use Benchmark in existing application to measure parts of code that are not so easy to extract into separate console test app? use case would be to collect measurements of code that you suspect is the root of all evil before refactoring that would allow to benchmark those parts in more isolated way. of course you can use dotTrace or similar profiling tools but maybe Benchmark or other package could be of use as well here? and as always, this video was great, keep them coming!

    @SchmataB@SchmataB2 жыл бұрын
  • I was waiting for a pointer I'm any time, but we still safe.

    @neociber24@neociber242 жыл бұрын
  • Would casting char to byte within the function allocate memory as well? Or you could have worked directly with const char? Also when was that switch introduced? First time seeing it

    @Gastell0@Gastell02 жыл бұрын
    • Char and byte are value types so there would be no allocation. Switch expressions have been out for a few years now

      @nickchapsas@nickchapsas2 жыл бұрын
  • Looking forward to seeing unsafe version of this optimization

    @antonmartyniuk@antonmartyniuk2 жыл бұрын
  • When using the default case in a switch you do a second array lookup. Would it be faster to use a binding match instead of an underscore? Like char i => i instead of _ => array[pos]

    @cn-ml@cn-ml2 жыл бұрын
    • There is no array lookup at all with the switch. It's being lowered to if checks

      @nickchapsas@nickchapsas2 жыл бұрын
    • @@nickchapsas I had the same reaction as @Michael Chen and I must admit I don't understand your answer...

      @Krimog@Krimog2 жыл бұрын
    • @@Krimog Sorry I misunderstood the question. No that can't happen because list patterns don't work like that. You can't do i => i.

      @nickchapsas@nickchapsas2 жыл бұрын
  • MORE PLEASE

    @CricketThomas@CricketThomas2 жыл бұрын
  • Great video, but I got a bit lost when you said Span was allocated on the stack (so I thought it generally was) and still used stackalloc (in order to achieve something I thought was already the case?). What am I misunderstanding?

    @enji._@enji._2 жыл бұрын
    • Broadly speaking: Span only contains a 'pointer' to memory and bounds. That memory can still be on the stack or on the heap. Using a stackalloc you allocate memory on the stack, and then create new Span object (also on the stack) that points to that memory

      @tajkris@tajkris2 жыл бұрын
  • Is such conversion of guid to bytes correct? What about BE/LE systems? Won't they mess bytes?

    @QwDragon@QwDragon Жыл бұрын
  • Hey Nick, for some of your courses, if you have questions, what is the best way to reach out on those?

    @shawnelswick5715@shawnelswick57152 жыл бұрын
    • Linked in, Twitter DMs or email works

      @nickchapsas@nickchapsas2 жыл бұрын
  • very useful, but this way for reference type, have any way for value type ?

    @simpleboy8556@simpleboy8556 Жыл бұрын
  • Your intro sounded like a aprils fool - glad I knew about Spans and stuck around ;)

    @padreigh@padreigh2 жыл бұрын
  • Having a full grown api already i am curious if this would improve the performance so drastically. I would think you have the string converted after each call of an endpoint, so basically every Guid you use now would be string something like B2yOjLyEZk0. Then you'd convert it back to guid and use the guid for querying database. Would be interesting to see how you would implement that into an existing api. I am also curious if the DI would slow things down once you implemented the function to convert guids. edit: i just saw you're using static.. so forget the DI.. :D

    @LCSL90@LCSL902 жыл бұрын
    • You will really see a benefit if you are using the conversion in many areas. For example if you were allocating 200 bytes (like in the video) per conversion, then if you’re dealing with 1000 requests per second (which I do more of that) then you’re instantly on 20kb or wasted memory per second which adds up quickly and triggers collection by memory pressure. I think you should profile your apps memory and if that part is hot then optimise it otherwise, ignore it and go for the low hanging fruit. You wanna be pragmatic here

      @nickchapsas@nickchapsas2 жыл бұрын
  • Awesome

    @FlavioO@FlavioO2 жыл бұрын
  • Can you do a video on improving Entity Framework performance?

    @thomas3141@thomas31412 жыл бұрын
    • It's interesting idea, how we can optimize for example update entity which has child and they have own child. What about batch operations and so on

      @rasimismatulin1400@rasimismatulin14002 жыл бұрын
    • Dapper :)

      @briankarcher8338@briankarcher83382 жыл бұрын
    • @@briankarcher8338 🤣🤣

      @rasimismatulin1400@rasimismatulin14002 жыл бұрын
  • Why did you create a const variable for backslash and plus and such? is it for maintainability?

    @wuketuke6601@wuketuke66013 ай бұрын
  • What does it mean "without allocating ANY memory"? stackalloc initializes stack allocated buffers. Sure, it does not use heap. "without allocating ANY memory" - it is like your Span/Span exists in virtual address space of virtual address space 😀

    @PolishchukMaxim@PolishchukMaxim2 жыл бұрын
    • You don't allocate. You are using already allocated limited stack memory. No overhead for GC too.

      @buraktamturk286@buraktamturk2862 жыл бұрын
    • There is obviously no programming without allocations of some sort, but in C# when we talk about memory allocations we talk about heap allocations. This video shows how you can use the Span struct to mostly or solely allocate memory on the stack which is fast and cheap.

      @nickchapsas@nickchapsas2 жыл бұрын
    • @@buraktamturk286 when you are creating (placing) variable in stack it means that some stack memory will be allocated specially for this variable. Variable is unable to exist anywhere in "already allocated limited stack memory", this variable will have concrete address in stack memory, i.e. we have allocating of specially space for variable in stack. Keyword is stackalloc, isn't it? 😀

      @PolishchukMaxim@PolishchukMaxim2 жыл бұрын
    • @@nickchapsas It is OK. Forgive me for grabbing that screaming headline. This is a very good video. Many thanks.

      @PolishchukMaxim@PolishchukMaxim2 жыл бұрын
    • @@PolishchukMaxim well, the stack for running thread is already allocated when that thread is created. So you are using pre-allocated memory. It is just stack pointer that gets adjusted by the amount of data requested. (It is just a simple math operation on stack pointer). Name is for convention only.

      @buraktamturk286@buraktamturk2862 жыл бұрын
  • 1:24 Wasn't the URL I was expecting but still got the expected result.

    @gctypo2838@gctypo28385 ай бұрын
  • why extract 'Plus' and 'Equals' to the constants?? can they ever change? Plus become minus or something?

    @megasuperlexa2@megasuperlexa27 ай бұрын
  • About making this faster: The conversion of the arrays in the optimised To and From, could be done in parallel.. I guess that needs testing and benchmarked to verify improvement in speed.

    @noylevi3340@noylevi33402 жыл бұрын
    • Adding multithreading or concurrency here would make it slower due to the workload being small. It's overhead with diminishing returns

      @nickchapsas@nickchapsas2 жыл бұрын
  • Where can I learn stuff like this? I missed a lot of this kind of stuff in my degree.

    @TurtleWargaming@TurtleWargaming2 жыл бұрын
  • Cool!

    @oscareriksson9414@oscareriksson94142 жыл бұрын
  • Instead of copying the bytes in the Guid into a Span, can't you just cast it into a byte* in unsafe mode?

    @JuvStudios@JuvStudios Жыл бұрын
    • We don't wanna resort to unsafe code

      @nickchapsas@nickchapsas Жыл бұрын
  • Is it possible to do some branchless programming to avoid those switch statements, and make it faster that way?

    @TheSaintsVEVO@TheSaintsVEVO2 жыл бұрын
    • It doesn’t matter because these switches will be turned into if statements behind the scenes so that part is as fast as it gets

      @nickchapsas@nickchapsas2 жыл бұрын
    • You could use a lookup table to avoid the branches, I suspect. Anyone's guess whether the branch predictor or the cache prefetch is faster tho.

      @DFPercush@DFPercush Жыл бұрын
  • As you said at the end this could lead to stack overflow

    @amrosamy8232@amrosamy82322 жыл бұрын
    • This would not lead to stack overflow because the memory is deterministic and extremely small. You should worry about stack overflow on lengths that you cannot control. this example will never lead to a problem. Most of the .NET code behind the scenes uses this exact practice to optimise the code

      @nickchapsas@nickchapsas2 жыл бұрын
    • @@nickchapsas thank you for the explanation, and thank you for that wonderful demo. For sure this will be useful with embedded boards and less resources computers where the performance is a main part.

      @amrosamy8232@amrosamy82322 жыл бұрын
  • More!

    @berathebrain@berathebrain2 жыл бұрын
  • This video was very poggies, yes, yes

    @JustArion@JustArion2 жыл бұрын
  • 17:20 "there is an overload, so this iwll alocate no memory again" I'm not sure that this is the case. assuming it would NOT allocate memory again, then the string would point internally to memory on the stack, so it would point to memory which won't exist after the mehtod anymore. OR it would mean that this constructor implictly MOVES the memory from the span into the string. ALSO: the benchmark shows that it will allocate the memory on the heap, as it resulted in 76 Bytes allocated which are: 32 Byte for the empty string + 22* 2 Bytes for each of the characters. **edit** while listening again to you, i think I've understood you wrong the first time. I thought you meant it would only allocate the 32 Byte for the string, but would not allocate the content of the string.

    @Soraphis91@Soraphis912 жыл бұрын
  • If i want to optimize my (real time)C# code i'd rather port it to C++. My boss doesnt think that way tho. Thanks for the knowledge

    @manuelvalencia6705@manuelvalencia67057 ай бұрын
  • Apparently there already is a base64url encoding in RFC 4648 §5 which differs from your solution only in that the _ and - are swapped.

    @andreasvox8068@andreasvox80682 жыл бұрын
    • Ah shit that’s the one I wanted to showcase but I mixed the characters up 😂

      @nickchapsas@nickchapsas2 жыл бұрын
KZhead