Skip to content

sql: clean up uses of Statement#56217

Merged
craig[bot] merged 1 commit intocockroachdb:masterfrom
RaduBerinde:stmt-refactor-2
Nov 3, 2020
Merged

sql: clean up uses of Statement#56217
craig[bot] merged 1 commit intocockroachdb:masterfrom
RaduBerinde:stmt-refactor-2

Conversation

@RaduBerinde
Copy link
Copy Markdown
Member

This change cleans up the use of sql.Statement and reduces some
allocations. Specifically:

  • we create a Statement lower in the stack (in execStmtInOpenState),
    and pass only what we need in the higher layers;

  • we change various functions to take a tree.Statement rather than
    an entire Statement when possible;

  • we move down the withStatement context allocation, so that it is
    avoided in the implicit transaction state transition;

  • we store a copy rather than a pointer to the Statement in the
    planner;

  • we avoid directly using stmt fields from func() declarations
    that escape;

  • we populate Statement.AnonymizedStr upfront. The anonymized
    string is always needed (to update statement stats).

name                               old time/op    new time/op    delta
EndToEnd/kv-read/EndToEnd             153µs ± 1%     154µs ± 2%    ~     (p=0.486 n=4+4)
EndToEnd/kv-read-no-prep/EndToEnd     216µs ± 1%     217µs ± 1%    ~     (p=0.886 n=4+4)
EndToEnd/kv-read-const/EndToEnd       111µs ± 1%     113µs ± 1%  +1.01%  (p=0.029 n=4+4)

name                               old alloc/op   new alloc/op   delta
EndToEnd/kv-read/EndToEnd            25.8kB ± 1%    25.5kB ± 1%    ~     (p=0.114 n=4+4)
EndToEnd/kv-read-no-prep/EndToEnd    32.2kB ± 1%    31.9kB ± 1%    ~     (p=0.686 n=4+4)
EndToEnd/kv-read-const/EndToEnd      21.0kB ± 1%    20.7kB ± 2%    ~     (p=0.200 n=4+4)

name                               old allocs/op  new allocs/op  delta
EndToEnd/kv-read/EndToEnd               252 ± 1%       250 ± 0%  -0.99%  (p=0.029 n=4+4)
EndToEnd/kv-read-no-prep/EndToEnd       332 ± 0%       330 ± 1%    ~     (p=0.229 n=4+4)
EndToEnd/kv-read-const/EndToEnd         214 ± 0%       212 ± 0%  -0.93%  (p=0.029 n=4+4)

Release note: None

@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

Copy link
Copy Markdown
Member

@yuzefovich yuzefovich left a comment

Choose a reason for hiding this comment

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

:lgtm: but it'd probably be beneficial for someone else to also have a look.

Reviewed 18 of 18 files at r1.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @andreimatei, @nvanbenschoten, and @RaduBerinde)


pkg/sql/conn_executor_exec.go, line 203 at r1 (raw file):

		// StatementType() different from "Rows".
		if portal.exhausted {
			return nil, nil, nil

nit: not sure if it's important, but maybe we should just have return without nils so that default ev, payload, err values are used?

Copy link
Copy Markdown
Contributor

@andreimatei andreimatei left a comment

Choose a reason for hiding this comment

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

:lgtm:

💯

Reviewable status: :shipit: complete! 2 of 0 LGTMs obtained (waiting on @nvanbenschoten and @yuzefovich)


pkg/sql/conn_executor_exec.go, line 203 at r1 (raw file):

Previously, yuzefovich wrote…

nit: not sure if it's important, but maybe we should just have return without nils so that default ev, payload, err values are used?

no... no naked returns pls :)

@RaduBerinde
Copy link
Copy Markdown
Member Author

bors r+

@craig
Copy link
Copy Markdown
Contributor

craig bot commented Nov 3, 2020

Build failed:

This change cleans up the use of `sql.Statement` and reduces some
allocations. Specifically:

 - we create a  `Statement` lower in the stack (in execStmtInOpenState),
   and pass only what we need in the higher layers;

 - we change various functions to take a `tree.Statement` rather than
   an entire Statement when possible;

 - we move down the withStatement context allocation, so that it is
   avoided in the implicit transaction state transition;

 - we store a copy rather than a pointer to the Statement in the
   planner;

 - we avoid directly using `stmt` fields from `func()` declarations
   that escape;

 - we populate `Statement.AnonymizedStr` upfront. The anonymized
   string is always needed (to update statement stats).

```
name                               old time/op    new time/op    delta
EndToEnd/kv-read/EndToEnd             153µs ± 1%     154µs ± 2%    ~     (p=0.486 n=4+4)
EndToEnd/kv-read-no-prep/EndToEnd     216µs ± 1%     217µs ± 1%    ~     (p=0.886 n=4+4)
EndToEnd/kv-read-const/EndToEnd       111µs ± 1%     113µs ± 1%  +1.01%  (p=0.029 n=4+4)

name                               old alloc/op   new alloc/op   delta
EndToEnd/kv-read/EndToEnd            25.8kB ± 1%    25.5kB ± 1%    ~     (p=0.114 n=4+4)
EndToEnd/kv-read-no-prep/EndToEnd    32.2kB ± 1%    31.9kB ± 1%    ~     (p=0.686 n=4+4)
EndToEnd/kv-read-const/EndToEnd      21.0kB ± 1%    20.7kB ± 2%    ~     (p=0.200 n=4+4)

name                               old allocs/op  new allocs/op  delta
EndToEnd/kv-read/EndToEnd               252 ± 1%       250 ± 0%  -0.99%  (p=0.029 n=4+4)
EndToEnd/kv-read-no-prep/EndToEnd       332 ± 0%       330 ± 1%    ~     (p=0.229 n=4+4)
EndToEnd/kv-read-const/EndToEnd         214 ± 0%       212 ± 0%  -0.93%  (p=0.029 n=4+4)
```

Release note: None
@RaduBerinde
Copy link
Copy Markdown
Member Author

bors r+

@craig
Copy link
Copy Markdown
Contributor

craig bot commented Nov 3, 2020

Build succeeded:

@craig craig bot merged commit 0653b92 into cockroachdb:master Nov 3, 2020
@RaduBerinde RaduBerinde deleted the stmt-refactor-2 branch November 16, 2020 21:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants