Federico Andres Lois Profile picture
Jul 14, 2021 18 tweets 7 min read Read on X
1/ Actually a pretty good question. Why would you make a virtual table with C#? Usually, you wouldn't. Unless ...

You want flexibility for interpretation AND high performance without having to create code on the fly (Roslyn or any other shenanigan).
2/ Let's say that you have the following query. What we are doing here is transforming this text into a DAG (directed acyclic graph).
3/ If you would transform that into code, it would look like the following. Keep in match the bigger the graph the more those things start to add up.

Consider another AND (BinaryMatch) statement over another of those types. It gets unwieldly FAST.
4/ The big issue is that the types start crawling back into your code. For this code, no problem... 'var' does the inference and solve the issues (the compiler have to deal with n levels of nesting, but you just see 'var'). Don't even try to reason about the type on a stack trace
5/ If you need to write code like the following, there are not many tradeoffs from here. You either hide the return type with an interface or you use Roslyn to generate dynamic code. UNLESS....
6/ You guessed it. You build a virtual table :). Why? Because neither of the other solutions gets you flexibility AND efficiency. Roslyn solution is efficient as hell (if you can amortize the upfront cost that is). On the other hand, the interface is nasty performance wise.
7/ But what does the virtual table buys you? It buys you something that is taboo on the .Net land. It buys you type erasure mechanics. Now you can write code like this.
8/ Notice that I am erasing the fact that TInner and TOuter are actually structs, BUT, the code that gets executed is the one specialized for it.
9/ This is the fun part. I will probably still keep the Term and Match difference for semantic reasons, but I can get away with a single Match to write this code if I want to.
10/ Generated is the parser version generating the code dynamically through Roslyn. That means that the JIT does know everything and will create the best code out of it.

BUT... BUT... Where's the trick??? How can you effectively erase the type and still get the same performance?
11/ This can be done thanks a group of optimization and features from the C# language working in tandem. Struct based metaprogramming (links for my talks about the subject at #DotNextConf in the last tweet) and C# new Function Pointers. Which allow us to write this.
12/ It is said that almost every problem in Computer Science can be solve with just another level of indirection. This wont be the exception. The trick requires 2 things, first your function table (these are the jump points).
13/ Second is the trigger mechanism for the JIT to get you the proper jump points. You will see some boxing and unboxing there, but as shown there the JIT does a pretty good job to get rid of them :D
14/ Third your actual type erased type.
15/ That alone will get you up to 20% of the execution cost of the Roslyn generated code. BUT, if you want to squeeze even the last bit out of it, you can do this :)
16/ And if you want to go the extra mile. You can actually apply the same trick and create a DictionaryFunctionCache that will get you the specialized version you want to execute for the cost of a dictionary lookup. That tradeoff is obviously not that clear cut there.
17/ Links to the talks at @DotNextConf
(2017) Patterns for High Performance C#:
(2018) Scratch Metal:
(2019) Metaprogramming for the masses:
18/ Go find your nails. Enjoy.

• • •

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

Keep Current with Federico Andres Lois

Federico Andres Lois 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 @federicolois

Feb 10
1/ Alright, nerds buckle up. My read on todays news is @elonmusk playing 8D chess as usual. You have to hand it to him, he is smart as fuck. This isn’t just joking around trying to buy OpenAI. This is the AI industry’s version of Game of Thrones, and everyone’s got knives out.

Let’s break it down. 🧵👇
2/ First, OpenAI’s structure is a financial booby trap.

- There’s a nonprofit (OpenAI Inc.) that controls the for-profit OpenAI LP.
- That means you can’t just buy OpenAI outright—first, you gotta deal with the nonprofit board.
- It’s like saying, "I wanna buy Twitter," but the deal has to be approved by a secret society of monks.
3/ Musk’s move? Drop a $97.4 billion bid.

Now, did he actually wanna buy it? Probably not. This is a game theory 101 class onto adversarial games.

- If OpenAI rejects it, they must explain why, revealing their real motives.
- If they accept, Musk gets control and can shut down the Microsoft-aligned vision.
- If they try a legal loophole? He sues their pants off.

Either way, he wins.
Read 10 tweets
Apr 24, 2024
1/ After almost 1.5 years of studying cancer research for personal reasons, I arrived at a realization that prompted me to write this tweet. I will lay out the hypothesis in this thread.
2/ Disclaimer: I am not a formally trained health researcher. More like a very curious and tenacious guy with a 15+ year background in research, development, & reproducibility in computer science (computer science).
3/ I am putting the hypothesis out there because it may make sense to others doing field work. Feel free to dissect this hypothesis, find holes in it, and play devil's advocate. We will all come out smarter from it.
Read 33 tweets
Jan 3, 2024
1/ There is a very perverse dynamic on how Chavism (aka "the communist socialism") works. Let's use Argentina as the example. Over the first 20 years they initiate a process that we could call "Earnings Substitution" that will seal your fate over time.
2/ Your earnings/salary is going down and at the same time "subsidies" start to go up in order to fool people into think that nothing has changed. This works because the dirty job is done by inflation which is a much slower process.
3/ By the time people starts to realize that something is wrong, because some critical goods are not available (medicine, food, you name it) or inflation enters a death spiral; most people already depend on subsidies for spending.
Read 6 tweets
Aug 2, 2023
1/ Recently some interesting papers have been doing the rounds in the health community. To me the most interesting ones have been the GlyNAC paper and the more recent Taurine deficiency as a driver of aging papers.
2/ Disclaimer: While I have been researching this for a year and even executed an experimental protocol tailored for myself based on the GlyNAC paper, I am NOT a health professional, and I am just taking my health into my own hands. This is not advice of any kind.
3/ Disclaimers aside, why do I think these 2 papers are interesting? First because the claim (if true) is a game changer. And second because they may be related but I haven’t seen this relationship spotlighted by anyone.
Read 14 tweets
Apr 1, 2023
This just confirmed the weaponization of block lists. If enough people/bots block and mute you, they are essentially cancelling you. I find lots of people with I have never interacted with that has me blocked. Assuming there are third party block lists and block networks.
Normally that is an issue in general. Anyone that has done reinforcement learning had figure out (usually in the worst way) that you have to be incredible cautious with penalties. They are very prone to be gamed.
Read 4 tweets
Mar 20, 2023
1/ I asked #GPT4 to review our paper with @LDjaparidze. This is what happened and what I learned in the process. medrxiv.org/content/10.110…
2/ Since the general problem that practitioners find (in the worst way) is always training set tainting (guilty-as-charged). Habits die hard, the first thing I did is asking to do a review of the paper without any extra knowledge about what the paper says
3/ From the response alone I learned 2 things. First, our paper title was deadly accurate. I also learned that it has no information whatsoever on it, as the entire response can be generated from understanding the title itself.
Read 10 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!

:(