Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Improve performance of BigInteger.Multiply - #1436

Merged
KrzysztofCwalina merged 1 commit into
dotnet:masterfrom
axelheer:biginteger-performance
Apr 28, 2015
Merged

KrzysztofCwalina merged 1 commit into
dotnet:masterfrom
axelheer:biginteger-performance

Conversation

@axelheer

Copy link
Copy Markdown
Contributor

To introduce performance tweaks for BigInteger's operations a new static class BigCalc implements those algorithms based on raw uint[] objects, partially switching even to pointer arithmetic to spare some index calculations. First of all a multiplication implementation is included, furthermore an optimized version for squaring.

A basic performance comparison based on this code unveils the following results:

Multiply

# of bits # of vals before ms after ms
16 1,000,000 41 35
64 1,000,000 127 89
256 1,000,000 284 182
1,024 100,000 236 147
4,096 10,000 323 142
16,384 1,000 495 132
65,536 100 785 121

Square

# of bits # of vals before ms after ms
16 1,000,000 41 42
64 1,000,000 127 78
256 1,000,000 284 147
1,024 100,000 236 102
4,096 10,000 323 98
16,384 1,000 495 92
65,536 100 785 84

Note: the performance gain is not as big as expected based on my previous work (as mentioned within the issue), but I'd still call it significant. I actually had some difficulties executing a custom build of System.Runtime.Numerics though, since just dropping in the dll as described here does not work 'cause of some "type forwards". I finally managed to execute it using kre-coreclr-win-x64.1.0.0-beta3 and some hacks... 😳
#1307

@dnfclas

dnfclas commented Apr 19, 2015

Copy link
Copy Markdown

Hi @axelheer, I'm your friendly neighborhood .NET Foundation Pull Request Bot (You can call me DNFBOT). Thanks for your contribution!

In order for us to evaluate and accept your PR, we ask that you sign a contribution license agreement. It's all electronic and will take just minutes. I promise there's no faxing. https://cla2.dotnetfoundation.org.

TTYL, DNFBOT;

@dnfclas

dnfclas commented Apr 19, 2015

Copy link
Copy Markdown

@axelheer, Thanks for signing the contribution license agreement so quickly! Actual humans will now validate the agreement and then evaluate the PR.

Thanks, DNFBOT;

@ellismg

ellismg commented Apr 20, 2015

Copy link
Copy Markdown
Contributor

@dotnet-bot test this please

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.

I know it's an internal type, but I think it would be better for code browsing if it was called BigIntegerCalculator, BigIntegerHelpers, BigIntegerMath, or something like that.

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.

Ok, I'll rename it.

@KrzysztofCwalina

Copy link
Copy Markdown
Member

BTW, the perf results are impressive. Thanks for doing this PR! I think it's a great example how the community members with domain expertise, like yourself, can make .NET better.

I added some comments to the PR. The main one is that I wish the sources had more comments and some links to the algorithm description.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do we need to be concerned about overflow here?

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.

Nope, I'll write some comments for explanation.

@sharwell

Copy link
Copy Markdown

@hpshelton The cases you are pointing out regarding curly braces already conform to the style guidelines for this project.

Sources:

@hpshelton

Copy link
Copy Markdown
Contributor

Thanks, @sharwell. Deleted all but one instance of the comment.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Per the style guidelines, I believe these are supposed to be _multiplyThreshold and _squareThreshold?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

It's a constant, so PascalCasing is correct (last item in the list). Perhaps this point should be closer to the other points related to naming.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Darnit. Sorry. I'll add that to the list of things to fix on the wiki when I can figure out how to submit a PR for the wiki...

@axelheer

Copy link
Copy Markdown
Contributor Author

Thanks! I'll update the code accordingly.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Spelling: "already"

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.

Should I push an extra commit for this, or is it better to wait for further comments? I'm pretty new to this process...

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@axelheer I recommend installing the Visual Studio Spell Checker extension and making a pass through all the comments you added.

Most people wait for a period of time so reviewers add all their comments, but 12 hours is more than enough time to assume they've made the comments they want to for now. You can update it whenever you feel like it (or whenever you get away from the other things you work on). 😄

@KrzysztofCwalina

Copy link
Copy Markdown
Member

This looks good to me. Please squash the commits and I will merge this PR.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants