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

perf: pre-allocate buffers with with_capacity() #364

Merged
merged 3 commits into from
Oct 8, 2023

Conversation

RoloEdits
Copy link
Contributor

Pre-allocating removes a lot of regrows for the buffers.

Benchmarks

Quick benchmark opening a 150k row, 17 column .xlsx file shows 15% increase in performance.

master:

868,545,530 ns/iter (+/- 7,365,438)

perf/pre_alloc_buf:

750,808,050 ns/iter (+/- 6,366,129)

Profiles

Purple highlight is alloc.

Current master:
image

Change:
image

Changed from vec![] macro to with_capacity() as there was an introduction to a lot of instructions running.

vec![0;N]:
image

with_capacity(N):
image

allocating the buffer prior prevents a lot of reallocations from regrowing
in profiling, the macro option had a lot of overhead vs `with_capacity()`
let mut buf = Vec::new();
let mut val_buf = Vec::new();
let mut buf = Vec::with_capacity(1024);
let mut val_buf = Vec::with_capacity(1024);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

val_buf in particular had the biggest impact when profiling with this change

@RoloEdits
Copy link
Contributor Author

Marginal improvement but still an improvement:

pre 2881d13:
image

post:
image

@tafia
Copy link
Owner

tafia commented Oct 8, 2023

Awesome, thanks!

@tafia tafia merged commit 8a1ace7 into tafia:master Oct 8, 2023
4 checks passed
@RoloEdits RoloEdits deleted the perf/pre_alloc_buf branch October 8, 2023 05:09
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.

2 participants