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

SWEEP: Add unchecked to GetHashCode, #1065 #1068

Merged
merged 2 commits into from
Dec 19, 2024

Conversation

paulirwin
Copy link
Contributor

  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a change, please open an issue to discuss the change or find an existing issue.

Adds unchecked to all GetHashCode implementations where it is applicable

Fixes #1065

Description

GetHashCode should not throw an exception on overflow. This PR adds unchecked blocks to all GetHashCode implementations that do addition or multiplication that could overflow.

During review, it was determined that GetHashCode methods that return constants, simply delegate GetHashCode to another object, or solely do bitwise xor operations do not need unchecked added.

@paulirwin paulirwin added the notes:bug-fix Contains a fix for a bug label Dec 18, 2024
@paulirwin paulirwin marked this pull request as ready for review December 18, 2024 05:29
Copy link
Contributor

@NightOwl888 NightOwl888 left a comment

Choose a reason for hiding this comment

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

Thanks for the PR. It looks good.

There are 3 places where we are now doubling up on the unchecked block with an unchecked cast in the middle. We can now remove the cast. See the inline comments.

src/Lucene.Net/Util/FixedBitSet.cs Outdated Show resolved Hide resolved
src/Lucene.Net/Util/LongBitSet.cs Outdated Show resolved Hide resolved
src/Lucene.Net/Util/OpenBitSet.cs Outdated Show resolved Hide resolved
@NightOwl888
Copy link
Contributor

Wouldn't this be categorized as a performance improvement rather than a bugfix?

@paulirwin
Copy link
Contributor Author

Wouldn't this be categorized as a performance improvement rather than a bugfix?

I'm not sure there would be any performance impact. Originally I had assumed that we needed to do this to avoid overflow exceptions, but I realized just now that our projects do not have the "Check for arithmetic overflow" compiler option checked, so that reasoning is not valid alone. Perhaps an argument could be made that it helps ensure best practices and might help users if they want to recompile the library with that option for some reason, as well as to be consistent with other places in the codebase where we were already using unchecked blocks in GetHashCode... but in terms of performance, it looks like it generates the same opcodes without these blocks (i.e. add vs add.ovf), so there's no perf benefit here either. I'm not opposed to still moving forward with this PR for consistency's sake, but it's not the bugfix I thought it was. Perhaps "notes:ignore" would be a better label then.

@paulirwin
Copy link
Contributor Author

Aside: we seem to be more regularly be getting transient build failures downloading from nuget.org and MyGet again. This possibly is a result of adding the .NET 9 tests with an extra ~70 actions running in parallel. Fortunately the workaround is simple, to just choose the option to re-run failed jobs.

@NightOwl888 NightOwl888 added notes:improvement An enhancement to an existing feature and removed notes:bug-fix Contains a fix for a bug labels Dec 19, 2024
@NightOwl888 NightOwl888 merged commit a739a80 into apache:master Dec 19, 2024
267 checks passed
@NightOwl888
Copy link
Contributor

Aside: we seem to be more regularly be getting transient build failures downloading from nuget.org and MyGet again. This possibly is a result of adding the .NET 9 tests with an extra ~70 actions running in parallel. Fortunately the workaround is simple, to just choose the option to re-run failed jobs.

I suspect this is due to extra holiday traffic, however, it would probably help if we enabled NuGet caching (#856 and #857). I am not convinced we need to generate an extra file and figure out how to maintain it just so we can update packages, though. Our first attempt at doing so (on Azure DevOps) didn't need to, but the key didn't include the dependencies.props file (which missed the entire point) and apparently the only way to fix that is to completely rename the cache key so it can include that file (it should include all .props, .targets, and '.*proj` files just to be on the safe side).

@paulirwin paulirwin deleted the issue/1065 branch December 19, 2024 04:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
notes:improvement An enhancement to an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Review GetHashCode for possible overflows
2 participants