Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Take this explicitly to avoid argument shuffling by caller #111084

Closed
wants to merge 1 commit into from

Conversation

xtqqczze
Copy link
Contributor

@xtqqczze xtqqczze commented Jan 4, 2025

No description provided.

@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Jan 4, 2025
@xtqqczze
Copy link
Contributor Author

xtqqczze commented Jan 4, 2025

@MihuBot

@huoyaoyuan
Copy link
Member

I don't think this is the right thing to do. It's just saving one mov in exceptional path, which is not performance critical, and likely won't have any impact on modern CPU because of register naming.

Moreover, emitting static local function as static is quite an implementation detail of C# compiler.

@xtqqczze
Copy link
Contributor Author

xtqqczze commented Jan 4, 2025

@MihuBot arm64

@xtqqczze
Copy link
Contributor Author

xtqqczze commented Jan 4, 2025

It's just saving one mov in exceptional path, which is not performance critical

True, but it does impact code size.

Moreover, emitting static local function as static is quite an implementation detail of C# compiler.

Presumably relying on an implementation detail is acceptable in System.Private.CoreLib.

@MihaZupan
Copy link
Member

Wouldn't this also make things worse after inlining if this can be entirely omitted currently?

@huoyaoyuan
Copy link
Member

It's just saving one mov in exceptional path, which is not performance critical

True, but it does impact code size.

There are only several bytes of difference with no real impact. In practice, we would take the opposite approach that accepts trivial regression to simplify the code.

@xtqqczze xtqqczze marked this pull request as ready for review January 4, 2025 14:36
@xtqqczze
Copy link
Contributor Author

xtqqczze commented Jan 4, 2025

This pattern appears elsewhere:

#pragma warning disable IDE0060 // 'this' taken explicitly to avoid argument shuffling by caller
static int TryEncodeScalarAsHex(object @this, uint scalarValue, Span<byte> destination)
#pragma warning restore IDE0060

@xtqqczze
Copy link
Contributor Author

xtqqczze commented Jan 4, 2025

@MihuBot -arm

@huoyaoyuan
Copy link
Member

This pattern appears elsewhere:

The trick in referenced code is outdated: sharplab

It saves literally one mov only, and the resultant asm size has actually regressed because of GPR encoding of extra register (r10 vs rdx. We should get rid of the trick.

@xtqqczze
Copy link
Contributor Author

xtqqczze commented Jan 4, 2025

We should get rid of the trick.

xtqqczze@91a5fc2: MihuBot/runtime-utils#861

@xtqqczze
Copy link
Contributor Author

xtqqczze commented Jan 4, 2025

MihuBot/runtime-utils#860

@MihaZupan Interesting there is an improvement of 764 bytes on arm64, only 16 bytes on X64

@xtqqczze
Copy link
Contributor Author

xtqqczze commented Jan 4, 2025

We should get rid of the trick.

xtqqczze@91a5fc2: MihuBot/runtime-utils#861

@huoyaoyuan I see regressions in code size and PerfScore with the trick removed

@huoyaoyuan
Copy link
Member

@huoyaoyuan I see regressions in code size and PerfScore with the trick removed

The "regression" is very unlikely to be impactful. PerfScore is an internal JIT term for optimization decisions. We shouldn't tweak code too much to fit the JIT, but teach JIT instead.
I don't expect there will be any impact on performance benchmark numbers - that's the most important thing.

@jkotas
Copy link
Member

jkotas commented Jan 5, 2025

This pattern appears elsewhere:

runtime/src/libraries/System.Text.Encodings.Web/src/System/Text/Encodings/Web/DefaultHtmlEncoder.cs

This is not an exception throwing path. The trick is potentially saving extra instructions on a hot path here. It is still debatable whether this micro-optimization is worth the extra complexity.

@jkotas
Copy link
Member

jkotas commented Jan 5, 2025

Interesting there is an improvement of 764 bytes on arm64

This improvement does not look real. It is an issue in asm-diff tooling.

@jkotas
Copy link
Member

jkotas commented Jan 5, 2025

Thank you for a contribution, but we are not interested in complicating the code like this to save a few instructions on exception throwing paths.

@jkotas jkotas closed this Jan 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community-contribution Indicates that the PR has been added by a community member needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants