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

Improve Utils.IsEqual for byte compare #2440

Merged
merged 6 commits into from
Dec 22, 2023
Merged

Improve Utils.IsEqual for byte compare #2440

merged 6 commits into from
Dec 22, 2023

Conversation

mregen
Copy link
Contributor

@mregen mregen commented Dec 21, 2023

Proposed changes

  • Improve the Utils.IsEqual helper for some special cases which compare byte[].
  • In the actual implementation the compilers can not optimize for special cases, e.g. comparing a typical certificate as byte[] with 1k size on .NET8 took 167µs, the customized version needs only 69ns and is even faster than a P/Invoke to memc mp (80ns).
  • see benchmark result for improvements
  • Due to the little difference between a custom byte[] and the T[] compare, the byte[] was moved back to the tests
  • .NET Framework falls back on memcmp

see UtilsIsEqualGenericByteArrayCompare is a copy of the implementation up to V1.4.372.106 in the benchmark file.

Method Runtime PayLoadSize Mean Error StdDev Median Ratio RatioSD Gen0 Code Size Allocated Alloc Ratio
UtilsIsEqualGenericByteArrayCompare .NET 6.0 4096 865,245.1 ns 42,327.84 ns 124,804.58 ns 859,613.3 ns 0.778 0.11 23.4375 1,232 B 196678 B 0.997
UtilsIsEqualByteArrayCompare .NET 6.0 4096 203.0 ns 6.66 ns 19.20 ns 199.1 ns 0.000 0.00 - 1,213 B - 0.000
UtilsIsEqualTemplateByteArrayCompare .NET 6.0 4096 185.6 ns 3.69 ns 3.63 ns 185.5 ns 0.000 0.00 - 1,213 B - 0.000
UtilsIsEqualObjectCompare .NET 6.0 4096 335.3 ns 17.84 ns 52.60 ns 327.1 ns 0.000 0.00 - 1,303 B - 0.000
UtilsIsEqualIEnumerableCompare .NET 6.0 4096 201.4 ns 4.85 ns 13.83 ns 198.8 ns 0.000 0.00 - 1,213 B - 0.000
SequenceEqualsByteArrayCompare .NET 6.0 4096 194.3 ns 4.54 ns 13.10 ns 192.2 ns 0.000 0.00 - 1,188 B - 0.000
ForLoopBinaryCompare .NET 6.0 4096 6,537.8 ns 130.73 ns 214.79 ns 6,556.9 ns 0.006 0.00 - 104 B - 0.000
MemCmpByteArrayCompare .NET 6.0 4096 310.2 ns 6.24 ns 16.87 ns 304.5 ns 0.000 0.00 - 81 B - 0.000
UtilsIsEqualGenericByteArrayCompare .NET 8.0 4096 603,837.6 ns 12,008.10 ns 21,957.48 ns 598,516.8 ns 0.553 0.03 23.4375 1,763 B 196678 B 0.997
UtilsIsEqualByteArrayCompare .NET 8.0 4096 187.7 ns 4.77 ns 13.92 ns 185.3 ns 0.000 0.00 - 998 B - 0.000
UtilsIsEqualTemplateByteArrayCompare .NET 8.0 4096 186.0 ns 3.77 ns 8.36 ns 184.7 ns 0.000 0.00 - 998 B - 0.000
UtilsIsEqualObjectCompare .NET 8.0 4096 224.5 ns 4.37 ns 4.86 ns 225.5 ns 0.000 0.00 - 1,693 B - 0.000
UtilsIsEqualIEnumerableCompare .NET 8.0 4096 175.9 ns 3.57 ns 3.50 ns 175.3 ns 0.000 0.00 - 998 B - 0.000
SequenceEqualsByteArrayCompare .NET 8.0 4096 187.2 ns 3.68 ns 4.52 ns 188.2 ns 0.000 0.00 - 968 B - 0.000
ForLoopBinaryCompare .NET 8.0 4096 6,204.6 ns 120.62 ns 201.53 ns 6,162.3 ns 0.006 0.00 - 104 B - 0.000
MemCmpByteArrayCompare .NET 8.0 4096 290.1 ns 5.87 ns 8.79 ns 288.7 ns 0.000 0.00 - 79 B - 0.000
UtilsIsEqualGenericByteArrayCompare .NET Framework 4.8 4096 1,080,895.1 ns 21,527.64 ns 53,210.98 ns 1,068,179.3 ns 1.000 0.00 31.2500 1,294 B 197266 B 1.000
UtilsIsEqualByteArrayCompare .NET Framework 4.8 4096 96,856.0 ns 1,875.57 ns 3,997.00 ns 95,656.3 ns 0.089 0.01 - 84 B 65 B 0.000
UtilsIsEqualTemplateByteArrayCompare .NET Framework 4.8 4096 295.4 ns 5.83 ns 5.45 ns 293.4 ns 0.000 0.00 - 105 B - 0.000
UtilsIsEqualObjectCompare .NET Framework 4.8 4096 534.9 ns 7.94 ns 7.04 ns 535.8 ns 0.001 0.00 - 1,374 B - 0.000
UtilsIsEqualIEnumerableCompare .NET Framework 4.8 4096 97,511.8 ns 1,894.10 ns 2,948.88 ns 97,664.6 ns 0.089 0.01 - 84 B 65 B 0.000
SequenceEqualsByteArrayCompare .NET Framework 4.8 4096 94,807.6 ns 1,889.00 ns 1,939.86 ns 94,856.6 ns 0.089 0.00 - 587 B 65 B 0.000
ForLoopBinaryCompare .NET Framework 4.8 4096 6,052.1 ns 106.12 ns 82.85 ns 6,025.8 ns 0.006 0.00 - 104 B - 0.000
MemCmpByteArrayCompare .NET Framework 4.8 4096 298.7 ns 6.03 ns 13.23 ns 294.1 ns 0.000 0.00 - 80 B - 0.000

Related Issues

Types of changes

What types of changes does your code introduce?
Put an x in the boxes that apply. You can also fill these out after creating the PR.

  • Bugfix (non-breaking change which fixes an issue)
  • Enhancement (non-breaking change which adds functionality)
  • Test enhancement (non-breaking change to increase test coverage)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected, requires version increase of Nuget packages)
  • Documentation Update (if none of the other choices apply)

Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code.

  • I have read the CONTRIBUTING doc.
  • I have signed the CLA.
  • I ran tests locally with my changes, all passed.
  • I fixed all failing tests in the CI pipelines.
  • I fixed all introduced issues with CodeQL and LGTM.
  • I have added tests that prove my fix is effective or that my feature works and increased code coverage.
  • I have added necessary documentation (if appropriate).
  • Any dependent changes have been merged and published in downstream modules.

Further comments

If this is a relatively large or complex change, kick off the discussion by explaining why you chose the solution you did and what alternatives you considered, etc...

Copy link

codecov bot commented Dec 21, 2023

Codecov Report

Attention: 5 lines in your changes are missing coverage. Please review.

Comparison is base (8b3126b) 53.60% compared to head (922a787) 53.71%.
Report is 5 commits behind head on master.

Files Patch % Lines
Stack/Opc.Ua.Core/Types/Utils/Utils.cs 77.27% 2 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2440      +/-   ##
==========================================
+ Coverage   53.60%   53.71%   +0.11%     
==========================================
  Files         333      333              
  Lines       64319    64338      +19     
  Branches    13175    13183       +8     
==========================================
+ Hits        34477    34560      +83     
+ Misses      26098    26034      -64     
  Partials     3744     3744              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@mregen mregen marked this pull request as ready for review December 22, 2023 07:53
@mregen mregen merged commit af7bb8c into master Dec 22, 2023
68 of 72 checks passed
@mregen mregen deleted the memcmp branch December 22, 2023 10:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Utils.IsEqual for byte[] compare appears to be very slow
4 participants