Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Use BitOperations in couple more places - #101701

#101701
Merged
MichalStrehovsky merged 26 commits into
dotnet:maindotnet/runtime:mainfrom
PaulusParssinen:use-bitoperations-morePaulusParssinen/runtime:use-bitoperations-moreCopy head branch name to clipboard
Jun 3, 2024
Merged

Use BitOperations in couple more places#101701
MichalStrehovsky merged 26 commits into
dotnet:maindotnet/runtime:mainfrom
PaulusParssinen:use-bitoperations-morePaulusParssinen/runtime:use-bitoperations-moreCopy head branch name to clipboard

Conversation

@PaulusParssinen

@PaulusParssinen PaulusParssinen commented Apr 29, 2024

Copy link
Copy Markdown
Contributor

Should be files that are only included by NetCoreAppToolCurrent projects.

  • UPDATE1: Made ILVerification library consumed by ILVerify tool target NetCoreAppToolCurrent (same as target as ILVerify)
  • UPDATE2: Never mind, I didn't realize it was shipped as NuGet package.. Reverting..
answered question I want to use this to ask, whether any contributions to ``src/coreclr/tools/Common/Internal/NativeFormat/`` is welcomed as the files around here are largely untouched from their initial import and could utilize some newer APIs by my quick skim through. Should these be left alone?

@ghost ghost added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Apr 29, 2024
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Apr 29, 2024
@am11 am11 added area-Meta and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Apr 29, 2024
Comment thread src/coreclr/tools/Common/Internal/NativeFormat/NativeFormatWriter.cs Outdated
Co-authored-by: Adeel Mujahid <3840695+am11@users.noreply.github.com>

// Round number of buckets up to the power of two
_nBuckets = (uint)(1 << HighestBit(bucketsEstimate));
_nBuckets = (uint)HighestBit(bucketsEstimate);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be better to use RoundUpToPowerOf2. The old implementation would round up 2^n to next bigger value.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed this comment yesterday too, will use that instead.. thanks!

@PaulusParssinen PaulusParssinen Apr 30, 2024

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I think the comment here is inaccurate in that the existing code did not round up to the power of two but only set the highest bit.

e.g.

int bucketEstimate =                                                        0b0000_1010;
int highestBitSet = 1 << HighestBit(bucketEstimate);            // returns: 0b0000_1000
int nextPow2 = BitOperations.RoundUpToPowerOf2(bucketEstimate); // returns: 0b0001_0000

whether it should, is another question.. 🤔

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, at a glance it rather looks like the logic for HighestBit is incorrect. It claims to be computing the following, where one would normally interpret "rounded up" as meaning the actual algorithm is 1 + ceil(log2(x)) for non-zero x

Returns 1 + log2(x) rounded up, 0 iff x == 0

What's actually happening, however, is that there is no rounding up and it's simply doing 1 + log2(x) for non-zero x:

Console.WriteLine($"{HighestBit(0)} {BitOperations.Log2(0)}");  // 0 0 -- correct
Console.WriteLine($"{HighestBit(1)} {BitOperations.Log2(1)}");  // 1 0 -- correct
Console.WriteLine($"{HighestBit(2)} {BitOperations.Log2(2)}");  // 2 1 -- correct
Console.WriteLine($"{HighestBit(3)} {BitOperations.Log2(3)}");  // 2 1 -- should be 3, as log2(3) is 1.58496...
Console.WriteLine($"{HighestBit(4)} {BitOperations.Log2(4)}");  // 3 2 -- correct
Console.WriteLine($"{HighestBit(5)} {BitOperations.Log2(5)}");  // 3 2 -- should be 4, as log2(5) is 2.321928...
...

@MichalStrehovsky

Copy link
Copy Markdown
Member

I want to use this to ask, whether any contributions to src/coreclr/tools/Common/Internal/NativeFormat/ is welcomed as the files around here are largely untouched from their initial import and could utilize some newer APIs by my quick skim through. Should these be left alone?

They were not touched because there were no problems with them and they are not perf hotspots. They can be changed. Note that the files with Gen suffix are generated with the https://github.com/dotnet/runtime/blob/main/src/coreclr/tools/Common/Internal/Metadata/NativeFormat/UpdateNativeFormatSources.cmd script so updating them involves updating the generator and re-running it to generate the sources.

@PaulusParssinen

PaulusParssinen commented Apr 30, 2024

Copy link
Copy Markdown
Contributor Author

They were not touched because there were no problems with them and they are not perf hotspots. They can be changed. Note that the files with Gen suffix are generated with the https://github.com/dotnet/runtime/blob/main/src/coreclr/tools/Common/Internal/Metadata/NativeFormat/UpdateNativeFormatSources.cmd script so updating them involves updating the generator and re-running it to generate the sources.

Okay, thanks for the advice! Unrelated to this PR but should those genered files be perhaps updated to follow the .g.cs pattern? Or is the contribution bar in these parts: if it works, don't touch? 😄

@MichalStrehovsky

Copy link
Copy Markdown
Member

Okay, thanks for the advice! Unrelated to this PR but should those genered files be perhaps updated to follow the .g.cs pattern? Or is the contribution bar in these parts: if it works, don't touch? 😄

We're not very consistent with doing this (this is not the only "offline generator") but it might be an improvement to rename them to .g.cs.

@PaulusParssinen

Copy link
Copy Markdown
Contributor Author

I'm unable to build the runtime locally for unknown reasons (#101720) so I'll close this for time being..

@PaulusParssinen
PaulusParssinen marked this pull request as ready for review May 10, 2024 18:04
The only consumer of this library is ILVerify, which itself is NetCoreAppToolCurrent.
Comment thread src/coreclr/nativeaot/System.Private.CoreLib/src/System/RuntimeFieldHandle.cs Outdated
Comment thread src/coreclr/tools/Common/Internal/Runtime/EETypeBuilderHelpers.cs Outdated
Comment thread src/coreclr/tools/Common/TypeSystem/Common/TypeHashingAlgorithms.cs Outdated

@MichalStrehovsky MichalStrehovsky left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@MichalStrehovsky
MichalStrehovsky merged commit 59e8bbc into dotnet:main Jun 3, 2024
@PaulusParssinen
PaulusParssinen deleted the use-bitoperations-more branch June 5, 2024 14:13
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-Meta community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.