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

Tiny Weights #14402

Merged
merged 14 commits into from
Nov 7, 2023
Merged

Tiny Weights #14402

merged 14 commits into from
Nov 7, 2023

Conversation

vmg
Copy link
Collaborator

@vmg vmg commented Oct 31, 2023

Description

This is a new optimization pattern for the execution engine. The idea is to speed up comparison operations by embedding "tiny weights" inside sqltypes.Value during execution. A tiny weight is a 4-byte compressed form of the full weight string for the value (see: evalengine.TinyWeighter for a detailed description and implementation). Since we actually have 4 spare bytes inside sqltypes.Value, we can inject the weight there without increasing the allocation cost for our in-memory rows, and any further comparison operators will automatically make use of them. This makes e.g. sorting wildly more efficient, because most comparisons during the sort can be performed by comparing two uint32 integers, instead of doing a full collation-aware comparison.

Of course, two tiny weight strings can collide (as they're essentially a lossy form of the weight string), but this is perfectly safe because we always fall back to a full comparison of the two values whenever their tiny weight strings are identical.

The arewefastyet benchmark results are not wildly impressive because OLTP is actually a pathological case for this example. The strings that OLTP uses in sort queries in the benchmark are all numerical strings, so their alphabet is very reduced (10 possible characters), making the 4 byte string collide quite often. The improvement on these OLTP Distinct-sorted queries is just 15%.

goos: linux
goarch: amd64
pkg: vitess.io/vitess/go/vt/vtgate/endtoend
cpu: 13th Gen Intel(R) Core(TM) i9-13900K
                       │ baseline.txt │          sha-2e689b2faf.txt          │
                       │    sec/op    │    sec/op     vs base                │
OLTP/DistinctRanges-16    565.4µ ± 6%   479.3µ ± 31%  -15.22% (p=0.000 n=10)

If we were to craft a different benchmark with string columns that contain arbitrary UTF8 data, the improvement gets all the way to ~40% because all the comparisons during sorting are performed with the tiny weight strings.

The global improvement in arewefast is pretty good, particularly for latency:

image

https://benchmark.vitess.io/compare?ltag=369b6a1e55aecd98c3cf6d4366cfbcee0477c474&rtag=946eb31e74187866a4a7414ca5df1954435681da

Again I wouldn't pay much attention to OLTP here because it's not representative of real world data (:cry:), but the speed up for real queries that include SORT BY or DISTINCT will be significant.

cc @dbussink @systay

Related Issue(s)

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

Copy link
Contributor

vitess-bot bot commented Oct 31, 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 31, 2023
@vmg vmg added Type: Performance Benchmark me Add label to PR to run benchmarks Component: Evalengine changes to the evaluation engine 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 31, 2023
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.

@github-actions github-actions bot added this to the v19.0.0 milestone Oct 31, 2023
@vmg vmg force-pushed the vmg/tiny-weights branch from 91cdf55 to 171966f Compare November 2, 2023 10:47
@vmg vmg force-pushed the vmg/tiny-weights branch from 2280e1f to 946eb31 Compare November 6, 2023 08:12
@vmg vmg marked this pull request as ready for review November 6, 2023 16:43
cmp, err := evalengine.NullsafeCompare(currentKey[gb.KeyCol], nextRow[gb.KeyCol], gb.Type.Coll)
v1 := currentKey[gb.KeyCol]
v2 := nextRow[gb.KeyCol]
if v1.TinyWeightCmp(v2) != 0 {
Copy link
Collaborator

Choose a reason for hiding this comment

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

we don't want to do this in the evalengine.NullsafeCompare instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I opted to wire up the comparison on the relevant callsites because there's a lot of places that don't use tiny weights right now. I think it'll be sensible to move it into NullsafeCompare once we wire up tiny weight generation in more paths.

@vmg vmg merged commit a15ef42 into vitessio:main Nov 7, 2023
115 checks passed
@vmg vmg deleted the vmg/tiny-weights branch November 7, 2023 10:41
@vmg
Copy link
Collaborator Author

vmg commented Nov 7, 2023

This was actually huge in arewefast.

image
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Benchmark me Add label to PR to run benchmarks Component: Evalengine changes to the evaluation engine Type: Performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants