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

Speed up AST rewriting #7716

Merged
merged 15 commits into from
Mar 22, 2021
Merged

Speed up AST rewriting #7716

merged 15 commits into from
Mar 22, 2021

Conversation

systay
Copy link
Collaborator

@systay systay commented Mar 19, 2021

Another performance improvement!

This time, we have attacked the AST rewriter. It's used, similarly to the AST visitor, to traverse the AST tree. The rewriter has the cool capability to be able to replace any node in the tree, and is used before planning to normalise and clean up the query before the actual planning starts.

The optimisations are:

  1. Instead of panic/recover, use explicit error checking
  2. Instead of copying the cursor to the stack, we use a single cursor, and overwrite it's fields before every use
  3. Only initialise the cursor when we know we are going to use it
  4. Smaller interface dispatch methods - before, we would have a single type switch for all SQLNode implementations. Now we have a specific dispatch type switch for every interface, not just for the top one. rewriteExpr has much fewer types to switch between than rewriteSQLNode.

Together, these changes amount to a nice ~20% performance improvement when benchmarking the rewriter.

Here follows some benchmark outputs. It's five random expressions per expression size.

name                                     old time/op    new time/op    delta
RewriteLargeExpression/Size-1-Expr-1-16     223ns ± 1%     176ns ± 0%   -20.99%  (p=0.016 n=5+4)
RewriteLargeExpression/Size-1-Expr-2-16     222ns ± 1%     176ns ± 2%   -20.63%  (p=0.008 n=5+5)
RewriteLargeExpression/Size-1-Expr-3-16     222ns ± 1%     181ns ± 5%   -18.23%  (p=0.008 n=5+5)
RewriteLargeExpression/Size-1-Expr-4-16     409ns ± 1%     335ns ± 1%   -18.09%  (p=0.008 n=5+5)
RewriteLargeExpression/Size-1-Expr-5-16     213ns ± 6%     167ns ± 1%   -21.76%  (p=0.008 n=5+5)
RewriteLargeExpression/Size-1-Expr-6-16     203ns ± 2%     164ns ± 1%   -19.25%  (p=0.008 n=5+5)
RewriteLargeExpression/Size-2-Expr-1-16     454ns ± 2%     341ns ± 2%   -24.91%  (p=0.008 n=5+5)
RewriteLargeExpression/Size-2-Expr-2-16     701ns ± 4%     494ns ± 1%   -29.57%  (p=0.008 n=5+5)
RewriteLargeExpression/Size-2-Expr-3-16    1.73µs ± 1%    1.78µs ± 1%    +3.16%  (p=0.008 n=5+5)
RewriteLargeExpression/Size-2-Expr-4-16     399ns ± 2%     311ns ± 1%   -22.11%  (p=0.008 n=5+5)
RewriteLargeExpression/Size-2-Expr-5-16     477ns ± 3%     356ns ± 1%   -25.35%  (p=0.008 n=5+5)
RewriteLargeExpression/Size-2-Expr-6-16     228ns ± 2%     183ns ± 3%   -19.60%  (p=0.008 n=5+5)
RewriteLargeExpression/Size-3-Expr-1-16    1.47µs ± 1%    1.27µs ± 1%   -13.36%  (p=0.008 n=5+5)
RewriteLargeExpression/Size-3-Expr-2-16    6.96µs ± 1%    5.07µs ± 1%   -27.23%  (p=0.008 n=5+5)
RewriteLargeExpression/Size-3-Expr-3-16     666ns ± 2%     459ns ± 1%   -31.07%  (p=0.008 n=5+5)
RewriteLargeExpression/Size-3-Expr-4-16    2.27µs ± 1%    1.51µs ± 1%   -33.37%  (p=0.008 n=5+5)
RewriteLargeExpression/Size-3-Expr-5-16     225ns ± 1%     182ns ± 0%   -19.18%  (p=0.000 n=5+4)
RewriteLargeExpression/Size-3-Expr-6-16     423ns ± 5%     298ns ± 4%   -29.44%  (p=0.008 n=5+5)
RewriteLargeExpression/Size-4-Expr-1-16    10.4µs ± 4%     8.6µs ±13%   -17.88%  (p=0.008 n=5+5)
RewriteLargeExpression/Size-4-Expr-2-16    5.32µs ± 5%    4.42µs ±20%      ~     (p=0.056 n=5+5)
RewriteLargeExpression/Size-4-Expr-3-16    8.45µs ± 9%    7.20µs ±15%      ~     (p=0.056 n=5+5)
RewriteLargeExpression/Size-4-Expr-4-16     227ns ± 0%     183ns ± 1%   -19.30%  (p=0.016 n=4+5)
RewriteLargeExpression/Size-4-Expr-5-16    1.51µs ± 7%    1.01µs ± 2%   -33.36%  (p=0.008 n=5+5)
RewriteLargeExpression/Size-4-Expr-6-16    21.9µs ± 1%    16.4µs ± 1%   -24.91%  (p=0.016 n=4+5)
RewriteLargeExpression/Size-5-Expr-1-16    14.4µs ± 8%    10.6µs ± 1%   -26.04%  (p=0.008 n=5+5)
RewriteLargeExpression/Size-5-Expr-2-16    14.6µs ± 4%    11.6µs ± 6%   -20.35%  (p=0.008 n=5+5)
RewriteLargeExpression/Size-5-Expr-3-16     232ns ± 8%     187ns ± 7%   -19.15%  (p=0.008 n=5+5)
RewriteLargeExpression/Size-5-Expr-4-16    3.72µs ± 8%    2.61µs ± 7%   -29.87%  (p=0.008 n=5+5)
RewriteLargeExpression/Size-5-Expr-5-16    27.0µs ± 7%    21.4µs ± 7%   -20.55%  (p=0.008 n=5+5)
RewriteLargeExpression/Size-5-Expr-6-16    13.0µs ± 6%    10.4µs ± 3%   -20.30%  (p=0.008 n=5+5)
RewriteLargeExpression/Size-6-Expr-1-16    46.2µs ± 7%    34.4µs ± 1%   -25.41%  (p=0.016 n=5+4)
RewriteLargeExpression/Size-6-Expr-2-16     225ns ± 0%     186ns ± 5%   -17.25%  (p=0.016 n=4+5)
RewriteLargeExpression/Size-6-Expr-3-16    22.5µs ± 6%    17.6µs ±10%   -21.83%  (p=0.008 n=5+5)
RewriteLargeExpression/Size-6-Expr-4-16     139µs ± 5%     120µs ± 7%   -13.93%  (p=0.008 n=5+5)
RewriteLargeExpression/Size-6-Expr-5-16    21.9µs ± 7%    17.6µs ± 2%   -19.75%  (p=0.008 n=5+5)
RewriteLargeExpression/Size-6-Expr-6-16     159ns ± 2%     151ns ± 4%    -5.15%  (p=0.016 n=5+5)

Impacted Areas in Vitess

Components that this PR will affect:

  • Query Serving

harshit-gangal and others added 12 commits March 19, 2021 16:29
Signed-off-by: Harshit Gangal <harshit@planetscale.com>
Signed-off-by: Harshit Gangal <harshit@planetscale.com>
Signed-off-by: Andres Taylor <andres@planetscale.com>
Signed-off-by: Harshit Gangal <harshit@planetscale.com>
Signed-off-by: Harshit Gangal <harshit@planetscale.com>
Signed-off-by: Andres Taylor <andres@planetscale.com>
Signed-off-by: Andres Taylor <andres@planetscale.com>
Signed-off-by: Andres Taylor <andres@planetscale.com>
Signed-off-by: Andres Taylor <andres@planetscale.com>
Signed-off-by: Andres Taylor <andres@planetscale.com>
Signed-off-by: Andres Taylor <andres@planetscale.com>
Signed-off-by: Harshit Gangal <harshit@planetscale.com>
Signed-off-by: Andres Taylor <andres@planetscale.com>
Signed-off-by: Andres Taylor <andres@planetscale.com>
@systay systay marked this pull request as ready for review March 20, 2021 11:47
@systay systay requested a review from vmg March 20, 2021 12:39
Signed-off-by: Andres Taylor <andres@planetscale.com>
@systay systay merged commit bf9f4ff into vitessio:master Mar 22, 2021
@systay systay deleted the rewrite-gen-2 branch March 22, 2021 08:42
@askdba askdba added this to the v10.0 milestone Mar 23, 2021
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.

None yet

3 participants