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

copy: avoid recopying buffer #106088

Merged
merged 2 commits into from
Jul 7, 2023
Merged

copy: avoid recopying buffer #106088

merged 2 commits into from
Jul 7, 2023

Conversation

rafiss
Copy link
Collaborator

@rafiss rafiss commented Jul 4, 2023

Use a []byte instead of a bytes.Buffer, so that we only move the cursor into the buffer if we know we want to advance.

This provides a 25% reduction in allocations in BenchmarkCopyCSVEndToEnd.

goos: darwin
goarch: arm64
                   │ /var/folders/p3/c61_z_vd3r7dr1_hnztm3ryr0000gq/T/tmp.Dm00yAlG6O/bench.a51e8a7806c │ /var/folders/p3/c61_z_vd3r7dr1_hnztm3ryr0000gq/T/tmp.Dm00yAlG6O/bench.0c9d3d5efb0 │
                   │                                      sec/op                                       │                           sec/op                            vs base               │
CopyCSVEndToEnd-10                                                                          3.991 ± 3%                                                   3.883 ± 2%  -2.71% (p=0.019 n=10)

                   │ /var/folders/p3/c61_z_vd3r7dr1_hnztm3ryr0000gq/T/tmp.Dm00yAlG6O/bench.a51e8a7806c │ /var/folders/p3/c61_z_vd3r7dr1_hnztm3ryr0000gq/T/tmp.Dm00yAlG6O/bench.0c9d3d5efb0 │
                   │                                       B/op                                        │                              B/op                                vs base          │
CopyCSVEndToEnd-10                                                                        8.322Gi ± 2%                                                      8.304Gi ± 1%  ~ (p=0.971 n=10)

                   │ /var/folders/p3/c61_z_vd3r7dr1_hnztm3ryr0000gq/T/tmp.Dm00yAlG6O/bench.a51e8a7806c │ /var/folders/p3/c61_z_vd3r7dr1_hnztm3ryr0000gq/T/tmp.Dm00yAlG6O/bench.0c9d3d5efb0 │
                   │                                     allocs/op                                     │                         allocs/op                          vs base                │
CopyCSVEndToEnd-10                                                                         18.30M ± 0%                                                 13.79M ± 0%  -24.62% (p=0.000 n=10)

Epic: None
Release note: None

@rafiss rafiss requested a review from stevendanna July 4, 2023 06:50
@blathers-crl
Copy link

blathers-crl bot commented Jul 4, 2023

It looks like your PR touches production code but doesn't add or edit any test code. Did you consider adding tests to your PR?

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@cockroach-teamcity
Copy link
Member

This change is Reviewable

rafiss added 2 commits July 7, 2023 01:32
Use a []byte instead of a bytes.Buffer, so that we only move the cursor
into the buffer if we know we want to advance.

This provides a 25% reduction in allocations in BenchmarkCopyCSVEndToEnd.

```
goos: darwin
goarch: arm64
                   │ /var/folders/p3/c61_z_vd3r7dr1_hnztm3ryr0000gq/T/tmp.Dm00yAlG6O/bench.a51e8a7806c │ /var/folders/p3/c61_z_vd3r7dr1_hnztm3ryr0000gq/T/tmp.Dm00yAlG6O/bench.0c9d3d5efb0 │
                   │                                      sec/op                                       │                           sec/op                            vs base               │
CopyCSVEndToEnd-10                                                                          3.991 ± 3%                                                   3.883 ± 2%  -2.71% (p=0.019 n=10)

                   │ /var/folders/p3/c61_z_vd3r7dr1_hnztm3ryr0000gq/T/tmp.Dm00yAlG6O/bench.a51e8a7806c │ /var/folders/p3/c61_z_vd3r7dr1_hnztm3ryr0000gq/T/tmp.Dm00yAlG6O/bench.0c9d3d5efb0 │
                   │                                       B/op                                        │                              B/op                                vs base          │
CopyCSVEndToEnd-10                                                                        8.322Gi ± 2%                                                      8.304Gi ± 1%  ~ (p=0.971 n=10)

                   │ /var/folders/p3/c61_z_vd3r7dr1_hnztm3ryr0000gq/T/tmp.Dm00yAlG6O/bench.a51e8a7806c │ /var/folders/p3/c61_z_vd3r7dr1_hnztm3ryr0000gq/T/tmp.Dm00yAlG6O/bench.0c9d3d5efb0 │
                   │                                     allocs/op                                     │                         allocs/op                          vs base                │
CopyCSVEndToEnd-10                                                                         18.30M ± 0%                                                 13.79M ± 0%  -24.62% (p=0.000 n=10)
```

Release note: None
@rafiss rafiss marked this pull request as ready for review July 7, 2023 05:33
@rafiss rafiss requested review from otan and cucaroach July 7, 2023 05:34
@rafiss
Copy link
Collaborator Author

rafiss commented Jul 7, 2023

tftr!

bors r+

@craig
Copy link
Contributor

craig bot commented Jul 7, 2023

Build succeeded:

@craig craig bot merged commit 81b17ca into cockroachdb:master Jul 7, 2023
@rafiss rafiss deleted the avoid-recopy branch July 7, 2023 14:16
@stevendanna
Copy link
Collaborator

Thanks! Really nice improvement.

craig bot pushed a commit that referenced this pull request Jul 18, 2023
106056: cli,sql: improve userfile upload performance r=adityamaru a=stevendanna

This PR is a serious of changes to improve the performance of userfile uploads.
See the individual commits for details.

The performance of uploading a 64 MB file improves substantially with these changes:

```
Before: BenchmarkUserfileUpload-16   1  13537820708 ns/op   4.96 MB/s  37239761608 B/op 1311062 allocs/op
 After: BenchmarkUserfileUpload-16   1   2135076152 ns/op  31.43 MB/s   2460613480 B/op  551130 allocs/op
```

- [x] Avoid buffering entire file during upload on the CLI-side
- [x] Avoid quadratic behaviour when searching for line endings (`@rafiss` fixed in #106088, thanks!)
- [x] Tests for escapingReader
- [x] Benchmark

Co-authored-by: Steven Danna <[email protected]>
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