Why does .NET IL always create new string objects even when the higher level code references existing ones? -
Why does .NET IL always create new string objects even when the higher level code references existing ones? -
background: have xml document containing thousands of pseudocode functions. i've written utility parse document , generate c# code it. here's simplified snippet of code gets generated:
public class someclass { public string func1() { homecoming "some value"; } public string func2() { homecoming "some other value"; } public string func3() { homecoming "some value"; } public string func4() { homecoming "some other value"; } // ... }
the of import takeaway each string value may returned multiple methods. assumed making minor alter methods instead homecoming references static fellow member strings, both cutting downwards on assembly size , cut down memory footprint of program. example:
public class someclass { private const string _some_value = "some value"; private const string _some_other_value = "some other value"; // ... public string func1() { homecoming _some_value; } public string func2() { homecoming _some_other_value; } public string func3() { homecoming _some_value; } public string func4() { homecoming _some_other_value; } // ... }
but surprise, inspection using .net ildasm.exe utility shows in both cases il functions identical. here 1 of them. either way, hard-coded value gets used ldstr:
.method public hidebysig instance string func1() cil managed { // code size 6 (0x6) .maxstack 8 il_0000: ldstr "some value" il_0005: ret } // end of method someclass::func1
in fact, "optimized" version worse because includes static string members in assembly. when repeat experiment using other object type besides string, see difference expect. note assemblies generated optimization enabled.
question: why .net apparently create new string object regardless of whether code references existing one?
il_0000: ldstr "some value" il_0005: ret
the disassembler beingness helpful show going on. can tell il address, note ldstr instruction takes 5 bytes. way few store string. utilize view + show token values see looks like. you'll see same strings uses same token value. called 'interning'.
the token value still doesn't show string stored after programme jitted. string literals go 'loader heap', heap distinct garbage collected heap. heap static items stored. or set way: string literals highly optimized , cheap. cannot improve yourself.
.net
Comments
Post a Comment