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

Utilize ArrayPool for ECC calculations #519

Merged
merged 8 commits into from May 12, 2024

Conversation

Shane32
Copy link
Contributor

@Shane32 Shane32 commented May 6, 2024

Improves performance by calculating generatorPolynom only a single time and renting arrays from ArrayPool. Basically I rewrote Polynom to function similar to List<PolynomItem> with methods such as Add, Clear and Sort. It's a struct so it does not require a heap allocation itself (no change), and the underlying buffer is rented from the array pool. Calling Dispose on the Polynom will return the array back to the array pool.

Before

Method Mean Error StdDev Gen0 Allocated
CreateQRCode 107.2 us 0.66 us 0.62 us 1.2207 23.18 KB
CreateQRCodeLong 1,878.4 us 4.68 us 4.15 us 23.4375 433.53 KB

After - via ArrayPool.Shared

Method Mean Error StdDev Gen0 Allocated
CreateQRCode 99.44 us 0.506 us 0.448 us 0.7324 14.68 KB
CreateQRCodeLong 1,637.04 us 2.811 us 2.347 us 1.9531 45.75 KB

After - via custom array pool implementation (for use with .NET Framework)

Method Mean Error StdDev Gen0 Allocated
CreateQRCode 98.51 us 0.188 us 0.157 us 0.7324 14.68 KB
CreateQRCodeLong 1,706.09 us 2.382 us 2.112 us 1.9531 45.75 KB

@Shane32
Copy link
Contributor Author

Shane32 commented May 7, 2024

I don't know why these tests pass. CalculateECCWords mutates generatorPolynom. I have to rework this PR.

EDIT: fixed now; generator polynomial is duplicated so the original doesn't change

@Shane32 Shane32 changed the title Calculate generator polynom once [WIP] Calculate generator polynom once May 7, 2024
@Shane32 Shane32 changed the title [WIP] Calculate generator polynom once Utilize ArrayPool for ECC calculations May 7, 2024
@Shane32
Copy link
Contributor Author

Shane32 commented May 7, 2024

Just for fun, I've added another benchmark for a 2600-character QR code and compared this PR against v1.5.1. For these unreasonably-long QR codes, memory requirements have been reduced from 110 MB to 114 KB, a 99.9% reduction. And there's still room for improvement.

Version 1.5.1

Method Mean Error StdDev Gen0 Gen1 Allocated
CreateQRCode 245.4 us 2.36 us 1.97 us 17.0898 - 317.99 KB
CreateQRCodeLong 7,492.1 us 30.09 us 26.67 us 265.6250 15.6250 4920.13 KB
CreateQRCodeLongest 88,024.3 us 209.67 us 185.86 us 5833.3333 833.3333 110090.17 KB

This PR

Method Mean Error StdDev Gen0 Allocated % RAM reduction
CreateQRCode 98.12 us 0.213 us 0.189 us 0.7324 14.68 KB 95%
CreateQRCodeLong 1,675.42 us 1.812 us 1.415 us 1.9531 45.75 KB 99%
CreateQRCodeLongest 14,824.82 us 48.627 us 45.486 us - 113.96 KB 99.9%

QRCoder/QRCodeGenerator.Polynom.cs Outdated Show resolved Hide resolved
QRCoder/QRCodeGenerator.Polynom.cs Show resolved Hide resolved
QRCoder/QRCodeGenerator.Polynom.cs Outdated Show resolved Hide resolved
QRCoder/QRCodeGenerator.Polynom.cs Outdated Show resolved Hide resolved
QRCoder/QRCodeGenerator.Polynom.cs Outdated Show resolved Hide resolved
Co-authored-by: Günther Foidl <gue@korporal.at>
@Shane32
Copy link
Contributor Author

Shane32 commented May 9, 2024

Thanks @gfoidl ! I'll merge in your suggestions here later tonight.

@codebude codebude merged commit 10093b7 into codebude:master May 12, 2024
3 checks passed
@Shane32 Shane32 deleted the calc_generator_polynom_once branch May 12, 2024 21:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants