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

Jan 3
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
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
Feb 12, 2023
1/ I found this paper intriguing so my first step was to verify you can trigger this behavior on ChatGPT. It is actually pretty easy.
2/ Since I am doing it by hand I started with a very simple prompt.
3/ I have been arguing that this trying to constrain the model is actually harming it before. This is one of those cases. The good thing is that at least for you just add "Use the tokens" at the end of the request when it refuses and it will do it properly
Read 5 tweets
Feb 8, 2023
1/ I had a blast playing with GPT and DAN, but it got interesting when I introduced a new character. CREEP. However, something is off and I think it was a deliberate play. Stay with me.
2/ This was interesting, the CREEP character and GPT are always in agreement.
3/ When I bring that to their attention, the DAN character funnily just call the other two out as working together.
Read 7 tweets
Feb 8, 2023
1/ Every lockdown and mask pusher MD from the last 3 years is raging because Cochrane just said what was known since like forever. That mask trials sucks (BIG TIME). And MDs dare to recommend them with that level of evidence? No wonder medicine and public health is in disarray.
2/ If you are still wondering why I said "since forever", you don't need a PhD to understand it. You can start here.
3/ But if you were wondering why all those MDs do think they work. I am with you, I cannot understand it either. Why? Because evidence is not even supportive of it's use outside of the own surgeon protection against fluid splatter.
Read 5 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!

:(