Skip to content

Improve performance of RuntimeHelpers.GetMethodTable - #247

Closed
GrabYourPitchforks wants to merge 1 commit into
dotnet:masterfrom
GrabYourPitchforks:getmethodtable
Closed

GrabYourPitchforks wants to merge 1 commit into
dotnet:masterfrom
GrabYourPitchforks:getmethodtable

Conversation

@GrabYourPitchforks

Copy link
Copy Markdown
Member

Some small perf improvements to RuntimeHelpers.GetMethodTable. Previously the codegen looked like:

488d5108        lea     rdx,[rcx+8]
488b52f8        mov     rdx,qword ptr [rdx-8]

Now the codegen looks like:

488b11          mov     rdx,qword ptr [rcx]   ; no intermediate 'lea' instruction, saving 5 bytes of codegen

Below is the microbenchmark for pulling a Span<byte> from a Memory<byte> backed by a byte[].

Method Toolchain Mean Error StdDev Ratio
GetSpan_MemOfBytesFromArray master 1.980 ms 0.0169 ms 0.0150 ms 1.00
GetSpan_MemOfBytesFromArray memproto 1.898 ms 0.0303 ms 0.0284 ms 0.96

It's not much, but it could help make a small dent if these APIs are a hot path in a loop.

@GrabYourPitchforks

Copy link
Copy Markdown
Member Author

/cc @AndyAyersMS @CarolEidt Do you two have any thoughts on this? Seems like a straightforward enough change, but wondered if you knew of any gotchas here.

@jkotas

jkotas commented Nov 25, 2019

Copy link
Copy Markdown
Member

As always, it would be nice to teach the JIT to just optimize the existing code instead of adding workarounds like this.

@jkotas

jkotas commented Nov 25, 2019 •

Copy link
Copy Markdown
Member

This workaround is adding 3 helper types. I guess it would be ok to just have IL intrinsic expansion for Runtime.GetMethodTable() to save these 3 helper types and to make it easier for the JIT.

@AndyAyersMS

Copy link
Copy Markdown
Member

This seems like something that should be simple to fix in the jit, but last I time I investigated, it didn't look all that simple.

Making this an intrinsic seems sensible. I don't know when we'll get around to addressing the issues in the jit.

@GrabYourPitchforks

Copy link
Copy Markdown
Member Author

One thing I don't have a good read on is what would be the relative impact of a JIT-wide improvement vs a targeted improvement like what is proposed here. There have been other issues where we've opted to make targeted fixes because the pattern in question didn't appear frequently enough to justify the JIT work.

The pattern in question here is "get a ref to an instance field, then immediately perform ref arithmetic." I don't know how commonly that occurs outside these RuntimeHelpers helper methods.

@AndyAyersMS

Copy link
Copy Markdown
Member

I don't think it is all that common. Most of the code the JIT sees won't use Unsafe and explicit address manipulation. Regular field accesses and such are handled reasonably well, even for things like arrays of structs.

However, code that ends up using Unsafe and address manipulation is often trying to squeeze out the last bit of perf, so it's a bit of a disconnect to see that the JIT doesn't cope with this as well as it could.

Address mode formation is not as robust in the JIT as it is in native compilers and there are numerous open issues (eg dotnet/coreclr#5526, dotnet/coreclr#10737, dotnet/coreclr#11458, ...) with suggestions on how to improve. But none of these are small work items.

@GrabYourPitchforks

Copy link
Copy Markdown
Member Author

In the latest iteration I implemented an IL intrinsic rather than declaring new types. Let me know if this is closer to what you're looking for.

I confirmed the codegen is the same as the original iteration: no intermediate lea instruction, just a simple mov rdx, qword ptr [rcx].

@GrabYourPitchforks

Copy link
Copy Markdown
Member Author

Something went horribly wrong with my repo and I can no longer make code changes to this PR. 😬
Abandoning this and opened #275 instead. Sorry for the inconvenience.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 11, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants