readonly struct and readonly struct instance members (optimization?)
Hey you beautiful people. Recently I experimented with readonly struct's and readonly struct instance members (as one does)
However the compiler for this doesn't quite act like I expected it to.
Consider something like this for instance:
I would expect the il for the "_=test[0];" line to be something roughly like this:
ldloc.0
ldc.i4.0
call TestStruct::get_item(SomeEnum key)
pop
But from my testing, it seems the compiler does the less efficient route (the route of which I thought readonly struct was designed to prevent) of:
ldloca.s 0
ldc.i4.0
call TestStruct::get_item(SomeEnum key)
pop
aka it passes a pointer of the "test" local instead of by value to the get_item call (which for my understanding passing by value would be the expected behaviour here)
I'm using a custom compiler atm (I'm gonna try this with VS too) that are using the official roslyn codeanalysis nuget package
However the compiler for this doesn't quite act like I expected it to.
Consider something like this for instance:
I would expect the il for the "_=test[0];" line to be something roughly like this:
ldloc.0
ldc.i4.0
call TestStruct::get_item(SomeEnum key)
pop
But from my testing, it seems the compiler does the less efficient route (the route of which I thought readonly struct was designed to prevent) of:
ldloca.s 0
ldc.i4.0
call TestStruct::get_item(SomeEnum key)
pop
aka it passes a pointer of the "test" local instead of by value to the get_item call (which for my understanding passing by value would be the expected behaviour here)
I'm using a custom compiler atm (I'm gonna try this with VS too) that are using the official roslyn codeanalysis nuget package