Week 2 of analyzing how goroutines pause and resume and how the networking stack and other subsystems interact with the primitives exposed by the runtime.
One of the things I appreciate about C# is how many low level features are directly exposed to users. #golang#dotnet
In golang, it's not possible for you to implement pausing and resuming of goroutines other than by using the primitives go offers, channels, network IO, timers (sleep). This is fine for most applications but comes with additional overhead.
I also learned that go's GC doesn't move pointers and therefore there's no way to pin memory when working with C interop. There's no equivalent to passing around a GCHandle in go, just don't pass things with managed pointers to C.
There's a proposal for pinning for a possible future where a moving/compacting GC is implemented github.com/golang/go/issu….
I really appreciated the level of control C# affords you here. Though it does complicate things, it's possible to re-implement almost anything the runtime can do itself, especially with the language and runtime becoming more capable.
• • •
Missing some Tweet in this thread? You can try to
force a refresh
Tonight, we'll discuss some of the little known low level optimizations made to async/await in .NET Core 3.x! By now, most of you know what asynchronous programming is, if not look at docs.microsoft.com/en-us/dotnet/c…#dotnet
As a primer, remember I can write asynchronous logic in a sequential manner. The keywords async/await originated in C# 5.0 8 years ago, though there were inspirations in other languages that lead to this final syntax. I get sad when people think javascript invented async/await ;)
To see what the compiler generates, we decompile the async state machine back into C# to compare sharplab.io/#v2:D4AQTAjAsA…. The state machine keeps track of local state and all of the required context to pause and resume at each await point.
Somethings that we tried to change when building ASP.NET Core:
- Removing all first chance exceptions (where possible).
- Remove scanning all assemblies on Startup to find things (controllers/hubs/plugins/etc). We try to do a bulk of the work during build...
So that startup time isn't impacted by the scan and load all types in all assemblies.
- Code over configuration. Configuration is read and configured by imperative code.
- DI everywhere, anywhere we call your code, you can inject dependencies
- No static APIs, the entire framework is testable and uses constructor injection. Places that use the service locator pattern only do so because they themselves are factories. The root of the universe is the is DI container.
One of the beauties of shared abstractions is once the ecosystem starts to take advantage of them, adding value becomes that much more impactful. This is one of the goals of Microsoft.Extensions.* that we built while developing ASP.NET Core #dotnetcore#aspnetcore
It serves as a building block for what we call "application models" and attempts to handle the boring boilerplate and cross cutting concerns like dependency injection, logging and configuration.
The strategy is multi pronged:
- Introduce libraries with minimal dependencies that other libraries can take a dependency on without too much burden (ILogger, IConfiguration, IOptions, IHostingEnvironment) etc.
More Task and async gotchas! This code will dead lock.
Just a refresher, when you use a TaskCompletionSource, calling Set* or TrySet* will execute the continuation on that thread. Think of it like an event handler, you're triggering the event inline.
There's a new option that was introduced in .NET 4.6 and all of .NET Core that allows you to specify that the continuation should be dispatched to the thread pool.
Next, IHostedService. A way to run long running background operations in both the generic host and in your web hosted applications. docs.microsoft.com/en-us/aspnet/c…. 2.1 added support for a BackgroundService base class that makes it trivial to write a long running async loop.