Skip to content

sqlbase: avoid allocations due to ErrNameString, speed up IMPORT by 10%#35317

Merged
craig[bot] merged 1 commit intocockroachdb:masterfrom
nvb:nvanbenschoten/avoidAllocName
Mar 2, 2019
Merged

sqlbase: avoid allocations due to ErrNameString, speed up IMPORT by 10%#35317
craig[bot] merged 1 commit intocockroachdb:masterfrom
nvb:nvanbenschoten/avoidAllocName

Conversation

@nvb
Copy link
Copy Markdown
Contributor

@nvb nvb commented Mar 1, 2019

Informs #34809.

Before this change, ErrNameString only accepted a pointer to a string.
This forced the owner of the string to escape. In the following two
cases, this resulted in an entire ColumnDescriptor proto to be
allocated on each call:

  • sqlbase.MarshalColumnValue
  • sqlbase.CheckDatumTypeFitsColumnType

This showed up on IMPORT benchmarks, where the allocation in MarshalColumnValue
was responsible for about a third of heap allocations.

name                 old time/op    new time/op    delta
ImportFixtureTPCC-4     6.24s ± 2%     5.68s ±11%   -9.02%  (p=0.000 n=10+10)

name                 old speed      new speed      delta
ImportFixtureTPCC-4  14.2MB/s ± 2%  15.6MB/s ±10%  +10.08%  (p=0.000 n=10+10)

name                 old alloc/op   new alloc/op   delta
ImportFixtureTPCC-4    4.10GB ± 0%    2.77GB ± 0%  -32.34%  (p=0.000 n=10+10)

name                 old allocs/op  new allocs/op  delta
ImportFixtureTPCC-4     39.5M ± 0%     33.2M ± 0%  -16.11%  (p=0.000 n=10+9)

It appears that this will also have an effect on row.Inserter and
row.Updater.

Release note: None

@nvb nvb requested review from a team, danhhz and knz March 1, 2019 23:22
@nvb nvb requested a review from a team as a code owner March 1, 2019 23:22
@nvb nvb requested review from a team March 1, 2019 23:22
@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

Copy link
Copy Markdown
Contributor

@knz knz left a comment

Choose a reason for hiding this comment

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

Well spotted.

@nvb
Copy link
Copy Markdown
Contributor Author

nvb commented Mar 1, 2019

Spotted by @danhhz 😃

Copy link
Copy Markdown
Contributor

@danhhz danhhz left a comment

Choose a reason for hiding this comment

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

:lgtm: thanks for picking off the fix!

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


pkg/sql/sem/tree/name_part.go, line 63 at r1 (raw file):

// identifier suitable for printing in error messages.
func ErrNameString(s string) string {
	return ErrString(((*Name)(&s)))

I think you could replace the body of this with something like this, no?

var buf bytes.Buffer
lex.EncodeRestrictedSQLIdent(&buf, s, FmtBareIdentifiers.EncodeFlags())
return buf.String()

That would avoid the allocation in the error path, but more importantly, it would mean you could get rid of ErrNameStringP and make this all easier to use. It's pretty subtle that you have to know which of these two to call based on whether the string is already escaping or not.

@knz
Copy link
Copy Markdown
Contributor

knz commented Mar 1, 2019

That still creates you an allocation (that of buf) though.

@danhhz
Copy link
Copy Markdown
Contributor

danhhz commented Mar 1, 2019

Ah. I was thinking it would allocate that buf either way, but I hadn't dug down far enough to see fmtCtxPool. Nevermind what I said.

Informs cockroachdb#34809.

Before this change, `ErrNameString` only accepted a pointer to a string.
This forced the owner of the string to escape. In the following two
cases, this resulted in an entire `ColumnDescriptor` proto to be
allocated on each call:
- `sqlbase.MarshalColumnValue`
- `sqlbase.CheckDatumTypeFitsColumnType`

This showed up on IMPORT benchmarks, where the allocation in `MarshalColumnValue`
was responsible for about a third of heap allocations.

```
name                 old time/op    new time/op    delta
ImportFixtureTPCC-4     6.24s ± 2%     5.68s ±11%   -9.02%  (p=0.000 n=10+10)

name                 old speed      new speed      delta
ImportFixtureTPCC-4  14.2MB/s ± 2%  15.6MB/s ±10%  +10.08%  (p=0.000 n=10+10)

name                 old alloc/op   new alloc/op   delta
ImportFixtureTPCC-4    4.10GB ± 0%    2.77GB ± 0%  -32.34%  (p=0.000 n=10+10)

name                 old allocs/op  new allocs/op  delta
ImportFixtureTPCC-4     39.5M ± 0%     33.2M ± 0%  -16.11%  (p=0.000 n=10+9)
```

It appears that this will also have an effect on `row.Inserter` and
`row.Updater`.

Release note: None
@nvb nvb force-pushed the nvanbenschoten/avoidAllocName branch from c1e4605 to b3f5776 Compare March 2, 2019 01:16
@nvb
Copy link
Copy Markdown
Contributor Author

nvb commented Mar 2, 2019

bors r+

craig bot pushed a commit that referenced this pull request Mar 2, 2019
35317: sqlbase: avoid allocations due to ErrNameString, speed up IMPORT by 10% r=nvanbenschoten a=nvanbenschoten

Informs #34809.

Before this change, `ErrNameString` only accepted a pointer to a string.
This forced the owner of the string to escape. In the following two
cases, this resulted in an entire `ColumnDescriptor` proto to be
allocated on each call:
- `sqlbase.MarshalColumnValue`
- `sqlbase.CheckDatumTypeFitsColumnType`

This showed up on IMPORT benchmarks, where the allocation in `MarshalColumnValue`
was responsible for about a third of heap allocations.

```
name                 old time/op    new time/op    delta
ImportFixtureTPCC-4     6.24s ± 2%     5.68s ±11%   -9.02%  (p=0.000 n=10+10)

name                 old speed      new speed      delta
ImportFixtureTPCC-4  14.2MB/s ± 2%  15.6MB/s ±10%  +10.08%  (p=0.000 n=10+10)

name                 old alloc/op   new alloc/op   delta
ImportFixtureTPCC-4    4.10GB ± 0%    2.77GB ± 0%  -32.34%  (p=0.000 n=10+10)

name                 old allocs/op  new allocs/op  delta
ImportFixtureTPCC-4     39.5M ± 0%     33.2M ± 0%  -16.11%  (p=0.000 n=10+9)
```

It appears that this will also have an effect on `row.Inserter` and
`row.Updater`.

Release note: None

Co-authored-by: Nathan VanBenschoten <nvanbenschoten@gmail.com>
@craig
Copy link
Copy Markdown
Contributor

craig bot commented Mar 2, 2019

Build succeeded

@craig craig bot merged commit b3f5776 into cockroachdb:master Mar 2, 2019
@danhhz danhhz mentioned this pull request Mar 4, 2019
6 tasks
@nvb nvb deleted the nvanbenschoten/avoidAllocName branch March 6, 2019 03:53
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