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

optimize memory allocation of changesets with many small strings #5614

Merged
merged 2 commits into from
Jun 24, 2022

Conversation

ironage
Copy link
Contributor

@ironage ironage commented Jun 23, 2022

A customer (HELP-34650) reported slow downloads on iOS 14 and android. Profiling sync on an iOS 14 device revealed tons of time being spent requesting memory. I tried to reproduce this on mac, and ubuntu but could not. @ericjordanmossman profiled an actual iOS 14 device, and we found a bottleneck in InstructionBuilder::add_string_range:

Screen Shot 2022-06-23 at 2 33 16 PM

I noticed that changesets were mostly adding small strings (~10-50 bytes) such that m_string_buffer has to constantly request more memory, but only by a little bit each time once it outgrows the initial 1024 reserved capacity. The fix is to keep reserving 1k buffer space in chunks rather than increasing a little bit each time. I think this is not a problem on machines with more memory, because if the initial allocation uses memory that has additional space after the requested block, then subsequent allocations can just extend the range rather than moving the entire block to somewhere else. This wouldn't show up in benchmarks so I haven't added any. However, profiling the same workload as above with this optimization shows a ~60x speedup:

Screen Shot 2022-06-23 at 2 34 18 PM

@ironage ironage requested review from jbreams and tgoyne June 23, 2022 21:46
@ironage ironage self-assigned this Jun 23, 2022
@cla-bot cla-bot bot added the cla: yes label Jun 23, 2022
Comment on lines 545 to 548
if (m_string_buffer->capacity() - m_string_buffer->size() < string.size() &&
string.size() < small_string_buffer_size) {
m_string_buffer->reserve(m_string_buffer->capacity() + small_string_buffer_size);
}
Copy link
Member

Choose a reason for hiding this comment

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

Do we want to instead use exponential growth with a minimum size? Growing by 1024 bytes each time makes the constant factor a lot better but still leaves it quadratic time.

Suggested change
if (m_string_buffer->capacity() - m_string_buffer->size() < string.size() &&
string.size() < small_string_buffer_size) {
m_string_buffer->reserve(m_string_buffer->capacity() + small_string_buffer_size);
}
if (m_string_buffer->capacity() - m_string_buffer->size() < string.size()) {
m_string_buffer->reserve(std::max(std::max(small_string_buffer_size, m_string_buffer->capacity() * 2), m_string_buffer->size() + string.size()));
}

Copy link
Contributor

Choose a reason for hiding this comment

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

I was going to suggest this exact change, but then I thought "wait don't vectors/strings amortize their growth?" At least on libc++ strings double in size when they exceed their capacity until you reach a capacity that's half the size of the maximum capacity of the string. Then I read the cppreference page for reserve() and realized we're probably shooting ourselves in the foot here. https://en.cppreference.com/w/cpp/string/basic_string/reserve. Until c++20:

  • If new_cap is less than the current capacity(), this is a non-binding shrink request.
  • If new_cap is less than the current size(), this is a non-binding shrink-to-fit request equivalent to shrink_to_fit() (since C++11).

I suspect that what's going on here is that we reserve 1024, and then for every additional string that we append past the first 1024 bytes, we effectively shrink_to_fit the string. If we change this to only call reserve if the capacity is less than 1024, do you still see the slowdown?

Btw, this behavior goes away in C++20.

Copy link
Member

Choose a reason for hiding this comment

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

Huh, I wouldn't have guessed that reserve() would do a shrink_to_fit(). I guess I'm glad c++20 fixes that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Interesting, good analysis. I will change this to let the string implementation do its own capacity management and ask Eric to see if it is more effective.

@ironage
Copy link
Contributor Author

ironage commented Jun 24, 2022

@ericjordanmossman confirmed that these changes dropped the customer download from ~70s to ~8s on iOS 14.

@ironage ironage merged commit 4631455 into master Jun 24, 2022
@ironage ironage deleted the js/small-string-allocs branch June 24, 2022 20:33
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants