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

Optimize Operator Input Handling #16689

Merged
merged 14 commits into from
Sep 3, 2024
Merged

Conversation

systay
Copy link
Collaborator

@systay systay commented Aug 31, 2024

Summary

This PR introduces several optimizations to improve query planning and execution performance:

  1. Refactored the Operator interface to use unaryOperator and binaryOperator types in most cases, replacing the generic Inputs() and SetInputs() methods.
  2. Implemented reuse of the evalengine environment to reduce allocations.
  3. Delayed expression simplification to later stages of query planning.

These changes aim to reduce memory allocations, improve performance, and optimize the query planning process.

Key Changes

  1. Introduced unaryOperator and binaryOperator types and refactored most operators to use these.
  2. Implemented a mechanism to reuse the evalengine environment across multiple operations.
  3. Moved expression simplification to later stages in the query planning pipeline.
  4. Updated related code to work with the new operator types and optimizations.

Performance Impact

The changes resulted in significant performance improvements across various planner benchmarks:

  • Overall Performance: 10.76% improvement in execution time (geometric mean)
  • Memory Allocation: 2.74% reduction in bytes allocated per operation (geometric mean)
  • Allocation Count: 2.13% reduction in allocations per operation (geometric mean)

Raw performance figures:

goos: darwin
goarch: arm64
pkg: vitess.io/vitess/go/vt/vtgate/planbuilder
cpu: Apple M1 Ultra
                                            │   before    │                after                │
                                            │   sec/op    │   sec/op     vs base                │
OLTP-20                                       223.4µ ± 0%   224.8µ ± 1%   +0.62% (p=0.001 n=10)
TPCC-20                                       2.279m ± 0%   1.661m ± 0%  -27.12% (p=0.000 n=10)
TPCH-20                                       11.33m ± 1%   10.25m ± 0%   -9.52% (p=0.000 n=10)
Planner/from_cases.json-gen4-20               7.528m ± 0%   7.382m ± 1%   -1.95% (p=0.000 n=10)
Planner/filter_cases.json-gen4-20             183.6m ± 0%   149.3m ± 0%  -18.71% (p=0.000 n=10)
Planner/large_cases.json-gen4-20              490.1µ ± 1%   470.4µ ± 0%   -4.02% (p=0.000 n=10)
Planner/aggr_cases.json-gen4-20               13.03m ± 1%   13.29m ± 0%   +2.03% (p=0.000 n=10)
Planner/select_cases.json-gen4-20             10.51m ± 1%   10.31m ± 2%   -1.88% (p=0.002 n=10)
Planner/union_cases.json-gen4-20              3.815m ± 1%   3.787m ± 0%   -0.73% (p=0.005 n=10)
SemAnalysis-20                                63.09m ± 0%   28.85m ± 1%  -54.27% (p=0.000 n=10)
SelectVsDML/DML_(random_sample,_N=32)-20      991.8µ ± 2%   978.9µ ± 2%        ~ (p=0.075 n=10)
SelectVsDML/Select_(random_sample,_N=32)-20   1.947m ± 0%   1.711m ± 0%  -12.12% (p=0.000 n=10)
BaselineVsMirrored/Baseline-20                128.6µ ± 1%   128.3µ ± 0%        ~ (p=0.143 n=10)
BaselineVsMirrored/Mirrored-20                331.4µ ± 1%   337.0µ ± 0%   +1.69% (p=0.000 n=10)
geomean                                       3.175m        2.834m       -10.76%

                                            │    before     │                 after                 │
                                            │     B/op      │     B/op       vs base                │
OLTP-20                                        145.1Ki ± 0%    145.8Ki ± 0%   +0.52% (p=0.000 n=10)
TPCC-20                                       1082.3Ki ± 0%   1017.3Ki ± 0%   -6.00% (p=0.000 n=10)
TPCH-20                                        5.328Mi ± 0%    5.230Mi ± 0%   -1.84% (p=0.000 n=10)
Planner/from_cases.json-gen4-20                4.506Mi ± 0%    4.532Mi ± 0%   +0.58% (p=0.000 n=10)
Planner/filter_cases.json-gen4-20              22.39Mi ± 0%    17.95Mi ± 0%  -19.82% (p=0.000 n=10)
Planner/large_cases.json-gen4-20               269.0Ki ± 0%    271.8Ki ± 0%   +1.03% (p=0.000 n=10)
Planner/aggr_cases.json-gen4-20                7.185Mi ± 0%    7.223Mi ± 0%   +0.53% (p=0.000 n=10)
Planner/select_cases.json-gen4-20              6.066Mi ± 0%    6.070Mi ± 0%   +0.07% (p=0.000 n=10)
Planner/union_cases.json-gen4-20               2.217Mi ± 0%    2.226Mi ± 0%   +0.41% (p=0.000 n=10)
SelectVsDML/DML_(random_sample,_N=32)-20       665.9Ki ± 0%    642.8Ki ± 0%   -3.46% (p=0.000 n=10)
SelectVsDML/Select_(random_sample,_N=32)-20    1.100Mi ± 0%    1.029Mi ± 0%   -6.46% (p=0.000 n=10)
BaselineVsMirrored/Baseline-20                 87.88Ki ± 0%    88.05Ki ± 0%   +0.19% (p=0.000 n=10)
BaselineVsMirrored/Mirrored-20                 202.8Ki ± 0%    204.5Ki ± 0%   +0.84% (p=0.000 n=10)
geomean                                        1.280Mi         1.245Mi        -2.74%

                                            │   before    │                 after                 │
                                            │  allocs/op  │  allocs/op   vs base                  │
OLTP-20                                       3.612k ± 0%   3.612k ± 0%        ~ (p=1.000 n=10) ¹
TPCC-20                                       25.70k ± 0%   24.43k ± 0%   -4.95% (p=0.000 n=10)
TPCH-20                                       134.0k ± 0%   131.6k ± 0%   -1.80% (p=0.000 n=10)
Planner/from_cases.json-gen4-20               106.7k ± 0%   106.8k ± 0%   +0.11% (p=0.000 n=10)
Planner/filter_cases.json-gen4-20             562.7k ± 0%   481.1k ± 0%  -14.51% (p=0.000 n=10)
Planner/large_cases.json-gen4-20              10.52k ± 0%   10.46k ± 0%   -0.61% (p=0.000 n=10)
Planner/aggr_cases.json-gen4-20               164.8k ± 0%   164.8k ± 0%   +0.04% (p=0.000 n=10)
Planner/select_cases.json-gen4-20             140.4k ± 0%   140.0k ± 0%   -0.27% (p=0.000 n=10)
Planner/union_cases.json-gen4-20              52.07k ± 0%   52.06k ± 0%   -0.03% (p=0.000 n=10)
SelectVsDML/DML_(random_sample,_N=32)-20      13.14k ± 0%   13.26k ± 0%   +0.89% (p=0.000 n=10)
SelectVsDML/Select_(random_sample,_N=32)-20   25.30k ± 0%   23.93k ± 0%   -5.42% (p=0.000 n=10)
BaselineVsMirrored/Baseline-20                2.276k ± 0%   2.276k ± 0%        ~ (p=1.000 n=10) ¹
BaselineVsMirrored/Mirrored-20                5.700k ± 0%   5.700k ± 0%        ~ (p=1.000 n=10) ¹
geomean                                       32.25k        31.56k        -2.13%
¹ all samples are equal

Related issue:

#16789

Checklist

  • "Backport to:" labels have been added if this change should be back-ported to release branches
  • If this change is to be back-ported to previous releases, a justification is included in the PR description
  • Tests were added or are not required
  • Did the new or modified tests pass consistently locally and on CI?
  • Documentation was added or is not required

Copy link
Contributor

vitess-bot bot commented Aug 31, 2024

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 NeedsBackportReason If backport labels have been applied to a PR, a justification is required 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 Aug 31, 2024
@github-actions github-actions bot added this to the v21.0.0 milestone Aug 31, 2024
Copy link

codecov bot commented Aug 31, 2024

Codecov Report

Attention: Patch coverage is 78.53261% with 79 lines in your changes missing coverage. Please review.

Project coverage is 68.91%. Comparing base (44e96c2) to head (14dd62a).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
go/vt/vtgate/planbuilder/operators/rewriters.go 47.12% 46 Missing ⚠️
go/vt/vtgate/planbuilder/operators/mirror.go 46.15% 7 Missing ⚠️
go/vt/vtgate/planbuilder/operators/ordering.go 40.00% 6 Missing ⚠️
...t/vtgate/planbuilder/operators/insert_selection.go 44.44% 5 Missing ⚠️
go/vt/vtgate/planbuilder/operators/limit.go 44.44% 5 Missing ⚠️
go/vt/vtgate/planbuilder/operators/distinct.go 55.55% 4 Missing ⚠️
go/vt/vtgate/planbuilder/operators/expressions.go 90.62% 3 Missing ⚠️
go/vt/vtgate/planbuilder/operators/join.go 88.88% 1 Missing ⚠️
go/vt/vtgate/planbuilder/operators/plan_query.go 66.66% 1 Missing ⚠️
...vtgate/planbuilder/plancontext/planning_context.go 97.43% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #16689      +/-   ##
==========================================
- Coverage   68.94%   68.91%   -0.04%     
==========================================
  Files        1565     1565              
  Lines      201653   201605      -48     
==========================================
- Hits       139033   138934      -99     
- Misses      62620    62671      +51     

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

@systay systay force-pushed the quick-planner-2 branch 2 times, most recently from 9948c66 to 7c0ad91 Compare September 2, 2024 11:20
@systay systay added Component: Query Serving Type: Performance 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 NeedsBackportReason If backport labels have been applied to a PR, a justification is required labels Sep 2, 2024
@systay systay marked this pull request as ready for review September 2, 2024 11:21
@systay systay changed the title reuse the evalengine environment Optimize Operator Input Handling Sep 2, 2024
Signed-off-by: Andres Taylor <[email protected]>
Signed-off-by: Andres Taylor <[email protected]>
Comment on lines +232 to +237
newRHS, changed := bottomUp(root.RHS, rootID, resolveID, rewriter, shouldVisit, false)
if DebugOperatorTree && changed.Changed() {
fmt.Println(ToTree(newRHS))
}
anythingChanged = anythingChanged.Merge(changed)
root.RHS = newRHS
Copy link
Member

Choose a reason for hiding this comment

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

nit picking but how about extracting this, so it can be reused for the LHS and RHS

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 tried, but haven't found a nice way to do it without creating extra intermediate structs, and that's exactly what we are trying to avoid here.

Do you have an example of what you were thinking?

Copy link
Member

@frouioui frouioui left a comment

Choose a reason for hiding this comment

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

Been through the PR once, and it looks good to me.

Signed-off-by: Andres Taylor <[email protected]>
@systay systay merged commit d276007 into vitessio:main Sep 3, 2024
129 checks passed
@systay systay deleted the quick-planner-2 branch September 3, 2024 14:40
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.

3 participants