David Fowler Profile picture
Oct 3, 2021 7 tweets 2 min read Read on X
.NET has 4 built-in dictionary/map types:
- Hashtable
- Dictionary
- ConcurrentDictionary
- ImmutableDictionary

There’s no guidance on when to use what, mostly individual documentation on each implementation.

#dotnet
ConcurrentDictionary - Good read speed even in the face of concurrency, but it’s a heavyweight object to create and slower to update.

Dictionary with lock - Poor read speed lightweight to create and “medium” update speed.
Dictionary as immutable object - best read speed and lightweight to create but heavy update. Copy and modify on mutation e.g. new Dictionary(old).Add(key, value)
Hastable - Good read speed (no lock required), sameish weight as dictionary but more expensive to mutate and no generics!

ImmutableDictionary - Poorish read speed, no locking required but more allocations require to update than a dictionary.
This sort of guidance usually only comes up when implementation tradeoffs are being made but I’d love to spend more time documenting details like this…
Turns out there's some nice docs on this, see
This post is from an internal conversation I had with @stephentoub. The architect for the .NET libraries. We’ve been having discussions about how to write better recommendations for types like this.

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with David Fowler

David Fowler Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @davidfowl

Oct 10
Shiny!

#dotnet #aspire Image
Yes, it supports .NET 8 (LTS) and 9 Image
CLI too Image
Read 4 tweets
Oct 1
Aspire 9 upcoming features 🧵:

First, you'll be able to use Aspire 9 with .NET 8 and .NET 9! It will no longer require a call to dotnet workload install, and instead uses an MSBuild SDK. This should simplify CI/CD integration and getting started immensely! All you need is NuGet!

#dotnet #aspireImage
Next is the first-class addition of "WaitFor". You can now wait for dependencies to start, be healthy or to be complete before running your resource.

This is much like "depends_on" in docker compose, but with the ability to write health checks in C#. Image
@JamesNK has been posting about the most requested feature being the ability to start and stop services. If you have the debugger attached it will re-attach on restart 🤯!


Read 11 tweets
Apr 28
A customer mentioned trying to use default interface implementations to do "mixin" style programming in C# and it totally nerd sniped me. 🤔

#dotnet #csharp Image
Here's the sample: Image
The intent is to try to avoid reflection and use C# language features to drive composition where possible.

PS: I don't know if this is a good idea or not, but it's fun 😅.
Read 4 tweets
Jul 11, 2023
Here's some code that is on the hot path on your application and you want to optimize it. This is what a typical C# developer would write (actually copilot wrote this). It's pretty clear, but suboptimal. How could you go about improving it? #dotnet #csharp
There are lots of allocations here:
1. The string[] splitting up query string parts by &
2. Each key value pair string[] splitting each part by =
3. The List<string> of new results
4. The final string
One more assumption you can make: The instanceId will only occur once or 0 times in the input querystring.
Read 22 tweets
Dec 31, 2022
Discrete events masquerading as a workflow should be expressed as such. Consider the following event-based model: #dotnet
The game has 3 events:
- GameStarted
- GameEnded
- OnQuestion

The order of execution should be obvious from the naming...
The application doesn't control the event loop, the event loop will trigger the events at the appropriate time. Storing state across events means understanding the order in which they fire, the thread safety of such events and more (do they fire concurrently? can you block?)
Read 6 tweets
Dec 31, 2022
Currently designing how this trivia game will work on multiple servers. I have 3 architectures in mind (Twitter can help me pick one, but I have a preferred one). Both clients are part of the same game. Games are ephemeral and last a maximum of 2 minutes.
Architecture 1 - Using Redis as the game state storage and SignalR backplane.
Architecture 2 - Use Orleans grains as the SignalR backplane and state storage for a game.
Read 9 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us!

:(