skip to content
Relatively General .NET

Domain Modeling - Anemic Models

by Ardalis

posted on: May 11, 2022

When building a domain model, proper object-oriented design and encapsulation should be applied as much as possible. Some teams choose to…Keep Reading →

Who can give a refund?

by Oren Eini

posted on: May 10, 2022

Consider an eCommerce system where customers can buy stuff. Part of handling commerce is handling faults. Those range from “I bought the wrong thing” to “my kid just bought a Ferrari”. Any such system will need some mechanism to handle fixing those faults. The simplest option we have is the notion of refunds. “You bought by mistake, we can undo that”. In many systems, the question is then “how do we manage the process of refunds”? You can do something like this: So a customer requests a refund, it is processed by the Help Desk and is sent for approval by Finance, who is then consulting Fraud and then get sign off by the vice –CFO. There are about 12 refunds a quarter, however. Just the task of writing down the rules for processing refunds costs more than that. Instead, a refund policy can state that anyone can request a refund within a certain time frame. At which point, the act of processing a refund becomes: Is there a potential for abuse? Probably, but it is going to be caught naturally as we see the number of refunds spike over historical levels. We don’t need to do anything. In fact, the whole idea relies on two important assumptions: There is a human in the loop They are qualified to make decisions and relied upon to try to do the right thing Trying to create a process to handle this is a bad idea if the number of refunds is negligible. It costs too much, and making refunds easy is actually a goal (since that increases trust in the company as a whole).

Enabling IntelliSense for GitHub Actions workflows in VS Code

by Gérald Barré

posted on: May 09, 2022

If you edit a GitHub Actions workflow in GitHub, you can use auto-completion to quickly know what is possible and detect errors.I don't often use the GitHub editor. Instead, I prefer to use VS Code to edit my files. By default, VS Code doesn't support IntelliSense for GitHub Actions workflows. But,

Challenge

by Oren Eini

posted on: May 06, 2022

In my previous post, I asked why this change would result in a better performing system, since the total amount of work that is done is the same: The answer is quite simple. The amount of work that our code is doing is the same, sure, but that isn’t all the code that runs. In the first version, we would allocate the string, and then we’ll start a bunch of async operations. Those operations are likely to take some time and involve I/O (otherwise, they wouldn’t be async). It is very likely that in the meantime, we’ll get a GC run. At that point, the string pointed to be the ids variable will be promoted (since it survived a GC). That means that it would be collected much later. Using the new code, the scope of the ids string is far shorter. That means that the GC is more likely to catch it very early and significantly reduce the cost of releasing the memory.

Challenge

by Oren Eini

posted on: May 05, 2022

Take a look at the following code: This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters Show hidden characters public async Task<ComputeResult> Execute(List<Item> items) { var sw = Stopwatch.StartNew(); var ids = string.Join(", ", items.Select(x=>x.Id)); foreach(var item in items) { await Write(item); } await FlushToClient(); var result = await ReadResult(); log.Info($"Executed computation for '{ids}' in {sp.Elapsed}"); return result; } view raw bad.cs hosted with ❤ by GitHub If we move line 4 to line 11, we can improve the performance of this code significantly. Here is what this looks like:The question is, why?The exact same amount of work is being done in both cases, after all. How can this cause a big difference?

Criticizing Hare language approach for generic data structures

by Oren Eini

posted on: May 04, 2022

I run into this blog post about the Hare language and its approach to generic data structures. From the blog post, we have this quote: …it’s likely that the application of a higher-level data structure will provide a meaningful impact to your program. Instead of providing such data structures in the standard library (or even, through generics, in third-party libraries), Hare leaves this work to you. And this one, at the end: Hare doesn’t provide us with a generic hash map, but we were able to build one ourselves in just a few lines of code. A hash map is one of the simpler data structures we could have shown here, but even for more complex ones, you’ll generally find that it’s not too difficult to implement them yourself in Hare. I… don’t really know where to begin. The relevant code is here, by the way, and you can see how this works. A hash table is not a simple data structure, let’s start with that. It is the subject of much research and a ton of effort was spent on optimizing them. They are not the sort of things that you roll out yourself. To give some context, here are some talks from CppCon that talks about this: Abseil's Open Source Hashtables: 2 Years In - Matt Kulukundis - CppCon 2019 C++Now 2018: You Can Do Better than std::unordered_map: New Improvements to Hash Table Performance CppCon 2017: Matt Kulukundis “Designing a Fast, Efficient, Cache-friendly Hash Table, Step by Step” CppCon 2017 Designing a Fast, Efficient, Cache friendly Hash Table, Step by Step So in a quick search, we can see that there is a lot to discuss here. For that matter, here are some benchmark results, which compare: tsl::hopscotch_map tsl::robin_map tsl::sparse_map std::unordered_map google::dense_hash_map QHash Why are there so many of those? Well, because that matters. Each of those implementations is optimizing for something specific in different ways. There isn’t just a hash table algorithm, the details matter. A lot. The fact that Hare believes that a Hashtable or a map does not have to have a solution is pure insanity in my opinion. Let’s look at the example that is provided in the post, shall we? You can see the raw code here. Let’s take a look to understand what is going on here. There is a static array with 64 buckets that are used as the module cache. In each one of those buckets, you have an array of entries that match that bucket. The hash key here is the FNV32 of the AST node in question. Let’s see how many things just pop to mind immediately in here as issues. Let’s start with the fact that this is a statically sized hash table, which may be appropriate for this scenario, but won’t fit many others. If we need to handle growing the underlying array, the level of complexity will shoot up significantly. The code is also not handling deletes (another complicated topic), and the hash collision mode is chaining (via growing the array). In other words, for many other scenarios, you’ll need to roll your own hash table (and see above about the complexities involved). But let’s take it a bit further. The code is using FNV to compute the hash key. It is also making an assumption here, that the keys will never collide. Let’s see how well that holds up, shall we? This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters Show hidden characters import pyhash hashes = {} for word in open("/usr/share/dict/words", "rt").readlines(): hasher = pyhash.fnv1_32() h = hasher(word) if h in hashes: print(hashes[h] + " " + word + " " + str(h)) hashes[h] = word view raw fnv_collision.py hosted with ❤ by GitHub In other words, it took me a few minutes and under 130 ms to find a hash collision for this scenario. The code above does not handle it. For example, here are a couple of collisions: “intoxicated” and “tonsillectomy's” “Helvetius2” and “Rochester0” Those are going to be counted as the same value by the Hare code above. Fixing this requires non trivial amount of code changes. For that matter, let’s talk for a second about the manner in which I found it. If I were trying to write the same code in Hare, what would I have to do? Well, the answer to that is to write a lot of code, of course. Because I would have to re-implement a hash table from scratch. And the design of the Hare language doesn’t even allow me to provide that as a library. I have to fall down to code generation at best. These sorts of things matter. In C, you don’t have a hash table, and the most natural data structure is some form of a linked list. So that gets used a lot. You can bring in a hash table, of course, but adapting it for use is non trivial, so they are used a lot less often. Try writing the same in Hare, and then compare the cost in time to run (and time to execute). In modern languages, the inclusion of a hash table in the base language is a basic requirement. Languages like C++, Rust or Zig have that in the base class library and have the facilities to allow you to write your own generic data structure. That means that good data structures exist. That it make sense to spend the time writing them because they’ll be broadly applicable. Languages like C# or Java took this further and make sure that all objects have GetHashCode() and Equals() methods, specifically to support the hash table scenario. It is that important. Even Go, before it had generics, had a dedicated syntax carved out in the language to allow you to use maps natively. And now that Go has generics, that is actually far faster. In many systems, a hash table is one of the core data structures. It is used everywhere, and it lack make the ecosystem a lot more painful. Take a look at how Hare handles query string parameters: I mean, I guess it would be nice to have a way to do streaming on query strings? But the most natural way to do that is to use a hash table directly. The same applies for things like headers in web requests, how would you even model that in Hare? I couldn’t disagree more with the premise of the original post. A hashtable is not something that you should punt, the consequences for your users are dire.

Commands, Events, Versions, and Owners

by Ardalis

posted on: May 04, 2022

Commands and events are two common types of messages used in distributed application architectures, including microservice designs…Keep Reading →

Generating sortable Guids using NewId

by Andrew Lock

posted on: May 03, 2022

In this post I discuss the pros-and-cons of using integer vs Guids for IDs, and introduce NewId as an alternative to Guid with some nice properties…