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

vindexes: Efficient unicode hashing #14395

Merged
merged 11 commits into from
Oct 31, 2023
Merged

Conversation

vmg
Copy link
Collaborator

@vmg vmg commented Oct 30, 2023

Description

The current implementation of Unicode vindexes is not efficient because it depends on the x/text/collate package, which allocates a linear amount of memory based on the length of the input string being collated. This can be done much more efficiently because the value we require for a vindex is always a hash, so it can be calculated using a constant amount of memory if instead of generating a weight string and hashing it, we write directly into a hasher.

Paired on this with @dbussink.

Benchmarks:

goos: linux
goarch: amd64
pkg: vitess.io/vitess/go/vt/vtgate/vindexes
cpu: 13th Gen Intel(R) Core(TM) i9-13900K
                                │   old.txt    │               new.txt               │
                                │    sec/op    │   sec/op     vs base                │
UnicodeTest/repeat=1/MD5-16       10.003µ ± 2%   7.619µ ± 0%  -23.83% (p=0.000 n=10)
UnicodeTest/repeat=1/xxHash-16     8.714µ ± 3%   6.547µ ± 1%  -24.86% (p=0.000 n=10)
UnicodeTest/repeat=4/MD5-16        39.70µ ± 2%   29.96µ ± 1%  -24.55% (p=0.000 n=10)
UnicodeTest/repeat=4/xxHash-16     34.93µ ± 2%   26.13µ ± 1%  -25.21% (p=0.000 n=10)
UnicodeTest/repeat=16/MD5-16       159.7µ ± 3%   119.8µ ± 1%  -24.97% (p=0.000 n=10)
UnicodeTest/repeat=16/xxHash-16    135.8µ ± 4%   102.7µ ± 1%  -24.32% (p=0.000 n=10)
geomean                            37.13µ        27.99µ       -24.62%

                                │     old.txt     │              new.txt               │
                                │      B/op       │    B/op     vs base                │
UnicodeTest/repeat=1/MD5-16        12906.00 ±  7%   80.00 ± 0%  -99.38% (p=0.000 n=10)
UnicodeTest/repeat=1/xxHash-16     12814.50 ±  5%   72.00 ± 0%  -99.44% (p=0.000 n=10)
UnicodeTest/repeat=4/MD5-16        49138.50 ±  5%   80.00 ± 0%  -99.84% (p=0.000 n=10)
UnicodeTest/repeat=4/xxHash-16     50670.00 ±  4%   72.00 ± 0%  -99.86% (p=0.000 n=10)
UnicodeTest/repeat=16/MD5-16      194932.50 ± 11%   80.00 ± 0%  -99.96% (p=0.000 n=10)
UnicodeTest/repeat=16/xxHash-16   195962.50 ± 11%   72.00 ± 0%  -99.96% (p=0.000 n=10)
geomean                             48.88Ki         75.89       -99.85%

                                │   old.txt    │              new.txt               │
                                │  allocs/op   │ allocs/op   vs base                │
UnicodeTest/repeat=1/MD5-16        34.000 ± 0%   2.000 ± 0%  -94.12% (p=0.000 n=10)
UnicodeTest/repeat=1/xxHash-16     34.000 ± 0%   2.000 ± 0%  -94.12% (p=0.000 n=10)
UnicodeTest/repeat=4/MD5-16       133.000 ± 0%   2.000 ± 0%  -98.50% (p=0.000 n=10)
UnicodeTest/repeat=4/xxHash-16    133.000 ± 0%   2.000 ± 0%  -98.50% (p=0.000 n=10)
UnicodeTest/repeat=16/MD5-16      529.000 ± 0%   2.000 ± 0%  -99.62% (p=0.000 n=10)
UnicodeTest/repeat=16/xxHash-16   529.000 ± 0%   2.000 ± 0%  -99.62% (p=0.000 n=10)
geomean                             133.7        2.000       -98.50%

cc @mattlord @aquarapid

Related Issue(s)

Fixes #14398

Checklist

  • "Backport to:" labels have been added if this change should be back-ported
  • Tests were added or are not required
  • Did the new or modified tests pass consistently locally and on the CI
  • Documentation was added or is not required

Deployment Notes

@vitess-bot
Copy link
Contributor

vitess-bot bot commented Oct 30, 2023

Review Checklist

Hello reviewers! 👋 Please follow this checklist when reviewing this Pull Request.

General

  • Ensure that the Pull Request has a descriptive title.
  • Ensure there is a link to an issue (except for internal cleanup and flaky test fixes), new features should have an RFC that documents use cases and test cases.

Tests

  • Bug fixes should have at least one unit or end-to-end test, enhancement and new features should have a sufficient number of tests.

Documentation

  • Apply the release notes (needs details) label if users need to know about this change.
  • New features should be documented.
  • There should be some code comments as to why things are implemented the way they are.
  • There should be a comment at the top of each new or modified test to explain what the test does.

New flags

  • Is this flag really necessary?
  • Flag names must be clear and intuitive, use dashes (-), and have a clear help text.

If a workflow is added or modified:

  • Each item in Jobs should be named in order to mark it as required.
  • If the workflow needs to be marked as required, the maintainer team must be notified.

Backward compatibility

  • Protobuf changes should be wire-compatible.
  • Changes to _vt tables and RPCs need to be backward compatible.
  • RPC changes should be compatible with vitess-operator
  • If a flag is removed, then it should also be removed from vitess-operator and arewefastyet, if used there.
  • vtctl command output order should be stable and awk-able.

@vitess-bot vitess-bot bot added NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsIssue A linked issue is missing for this Pull Request NeedsWebsiteDocsUpdate What it says labels Oct 30, 2023
@github-actions github-actions bot added this to the v19.0.0 milestone Oct 30, 2023
@dbussink dbussink added Type: Internal Cleanup Component: Query Serving and removed NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsWebsiteDocsUpdate What it says NeedsIssue A linked issue is missing for this Pull Request labels Oct 30, 2023
@dbussink
Copy link
Contributor

This can be done much more efficiently because the value we require for a vindex is always a hash, so it can be calculated using a constant amount of memory if instead of generating a weight string and hashing it, we write directly into a hasher.

The vendoring also addresses another issue. When x/text is updated, the collation tables can be fixed for a newer Unicode version but that could (very subtlety) break a vindex since it changes what input hashes to.

Here we vendor, so we essentially set it in stone and keep it always consistent for the future.

vmg and others added 9 commits October 31, 2023 09:02
Co-authored-by: Dirkjan Bussink <d.bussink@gmail.com>
Signed-off-by: Vicent Marti <vmg@strn.cat>
Co-authored-by: Dirkjan Bussink <d.bussink@gmail.com>
Signed-off-by: Vicent Marti <vmg@strn.cat>
Co-authored-by: Dirkjan Bussink <d.bussink@gmail.com>
Signed-off-by: Vicent Marti <vmg@strn.cat>
Co-authored-by: Dirkjan Bussink <d.bussink@gmail.com>
Signed-off-by: Vicent Marti <vmg@strn.cat>
Co-authored-by: Dirkjan Bussink <d.bussink@gmail.com>
Signed-off-by: Vicent Marti <vmg@strn.cat>
Co-authored-by: Dirkjan Bussink <d.bussink@gmail.com>
Signed-off-by: Vicent Marti <vmg@strn.cat>
Co-authored-by: Dirkjan Bussink <d.bussink@gmail.com>
Signed-off-by: Vicent Marti <vmg@strn.cat>
Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>
Signed-off-by: Vicent Marti <vmg@strn.cat>
Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>
Signed-off-by: Vicent Marti <vmg@strn.cat>
@vmg vmg force-pushed the vmg/vindex-collate branch from f541c0d to aac3a0c Compare October 31, 2023 08:02
Copy link
Contributor

@dbussink dbussink left a comment

Choose a reason for hiding this comment

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

So much more memory efficient!

Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>
Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>
@harshit-gangal harshit-gangal added the Benchmark me Add label to PR to run benchmarks label Oct 31, 2023
@vitess-bot
Copy link
Contributor

vitess-bot bot commented Oct 31, 2023

Hello! 👋

This Pull Request is now handled by arewefastyet. The current HEAD and future commits will be benchmarked.

You can find the performance comparison on the arewefastyet website.

Copy link
Member

@harshit-gangal harshit-gangal left a comment

Choose a reason for hiding this comment

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

A general question. Does the output for the current hash functions still remain the same?

@vmg
Copy link
Collaborator Author

vmg commented Oct 31, 2023

Yes, this is 100% backwards compatible and there are golden tests that verify it. Otherwise we'd break all existing Vitess deployments by changing the shard position of existing values!

@vmg vmg merged commit 7388255 into vitessio:main Oct 31, 2023
116 checks passed
@vmg vmg deleted the vmg/vindex-collate branch October 31, 2023 10:43
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.

Feature Request: Vendor the collation logic from x/text in Vitess
3 participants