Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is CIL ldstr replaced with constant reference to the string.Intern during JIT/AOT compilation?

Tags:

string

c#

cil

il

Say I have a code that needs to be optimized but easy to debug at the same time. I would thus assign a string to every value I use. Does the string inflict a major performance hit or is it turned into a constant reference obtained from string.Intern during JIT/AOT compilation, thus turning into a single simple instruction ?

Example:

In IL it would be ldstr "gazilion lines".

Is it during JIT/AOT compilation turned into something like ldsflda string.InternCache.ID_0000647516166 and const string ID_0000647516166 = "gazilion lines"; is added to string.InternCache ?

Yes, I could theoretically look into https://github.com/mono/mono but I have no idea how to find it.

Yes, I am mixing CIL, C# and whatever CIL is compiled into, but you get the idea.

Is ldstr guaranteed to be O(1)?

Yes it's "implementation detail" of the CLR but it would make sense if it was optimized this way.

like image 838
aeroson Avatar asked Nov 08 '22 23:11

aeroson


1 Answers

Long answer: yes. Becomes a single MOV reg,imm instruction at runtime, be sure to use a debugger that can show you the disassembly so you don't have to fret about how the metadata token and the jitter works. – Hans Passant

like image 141
aeroson Avatar answered Nov 14 '22 22:11

aeroson