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!


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

13 Jul
1/ Challenge time has ended, so here comes the solution. Why did I challenge you to estimate the prevalence of the left-out group, you may ask yourself?. Because, finding on your own teaches things.
2/ In this massive thread I won't give you the fish either, but I will give you all the tools to figure out the massive analysis errors that are made in most of the studies of this type. You may remember the missing deaths conundrum.
3/ Some believe these are signs of conspiracies; IMHO the problem is low skills in analyzing real-world messy data, so everybody just copy what others have done. In this case, most studies mimic the published protocols for the actual trials. And make the same mistakes.
Read 40 tweets
10 Jul
1/ For the upcoming numbers riddle I will give you some time for you to study and try to figure out the riddle before I give the answer. You know the last riddle.
2/ The materials for this riddle are:
[1] Effectiveness of an Inactivated SARS-Cov-2 Vaccine In Chile nejm.org/doi/full/10.10…
[2] Supplementary nejm.org/doi/suppl/10.1…
[3] populationpyramid.net/chile/2019/
[4] Subtraction: en.wikipedia.org/wiki/Subtracti…
3/ I will give you a hint.
Read 4 tweets
2 Jul
1/ It was eventually retracted, as we pointed out with @LDjaparidze the logic was flawed BUT retraction should only be used for misconduct or requested by the authors themselves. mdpi.com/2076-393X/9/7/…
2/ Peer review is broken since earlier than SARS-Cov-2 and review board should be accountable because they allowed it to pass review for publication. In a sense this retraction is not authors fault, but the whole editorial board. ALL OF THEM.
3/ The reason why retraction is not the tools is that sometimes even bad papers provide good data. When a retraction is stamped into it, the data collected even if poorly interpreted gets destroyed. Bad paper, data pointing to new science. One example:
Read 5 tweets
30 Jun
1/ Probably you have seen this. This got me scared for a bit, luckily I can say that my calculations say this is NOT the case. We have enough with the actual state of the data to add more sources of worry. Reason is complex, but I will try to explain. nejm.org/doi/full/10.10… Image
2/ The first thing I've noticed was that you are doing a rolling enrolling (which means that not everybody gets enrolled at the same time in the pregnancy) which is essentially what the last picture said. Suffice to say the number there is right, the interpretation is NOT.
3/ What do I mean? Well, between 50% to 75% of miscarriage's are reported to happen in the first 8 weeks. There is a caveat (as always) that many are not even clinically recognized. But we know that between 8% to 15% are. So that's better than nothing.
Read 9 tweets
20 Jun
1/n The power of the internet is incredible. Long story short. Back in 2018, I built a set of trading indicators, some of them truly novel stuff which I haven't published neither in source or a paper. tradingview.com/u/redknight666…
2/n Needless to say that I have been using them successfully since then, but something interesting happened yesterday. A user of Tradingview platform sent me this message. Some of my indicators happen to have some likes, and I always wondered how people used them.
3/n Obviously I said YES, bring it on. The indicator in question is a very strange indicator from a family I named as "Trend Denormalized". Most oscillators have an equivalent TA version. And in fact, I have and used successfully too the TA-RSI and other unpublished ones.
Read 5 tweets
13 Jun
1/n I have been sitting on this information for weeks on end, waiting someone to highlight it. But, if I tell you I will be subjected to 'being special' treatment. So, I wont give you the fish, I will teach you how to catch it.
2/n Resources and materials:
- [1] Total Coronavirus Deaths in Israel: worldometers.info/coronavirus/co…
- [2] National surveillance data: thelancet.com/journals/lance…
- [3] Supplementary: thelancet.com/cms/10.1016/S0…
3/n For better understanding you should be reading the whole [2] to understand what it means. I promise you it is worth the effort. I wont go anywhere (actually I will because I already wrote this).
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

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

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!