close
Skip to content

Reserve apartment contexts before push_back to reduce reallocations - #666

Open
zengelbrecht61-lgtm wants to merge 1 commit into
microsoft:masterfrom
zengelbrecht61-lgtm:perf/reserve-contexts
Open

Reserve apartment contexts before push_back to reduce reallocations#666
zengelbrecht61-lgtm wants to merge 1 commit into
microsoft:masterfrom
zengelbrecht61-lgtm:perf/reserve-contexts

Conversation

@zengelbrecht61-lgtm

Copy link
Copy Markdown

Summary

Pre-size the apartment context collection before pushing entries into the vector so it avoids repeated reallocation while the apartment lock is held.

Why

The apartment migration code collects context pointers and then appends them back into a vector each time it enumerates the apartment set. The vector grew one element at a time, which caused repeated reallocation and copy churn under the lock.

What changed

  • computed the total number of contexts to capture once
  • reserved that capacity before push_back
  • left the rest of the logic unchanged

Validation

  • builds and existing WIL tests continue to pass with the change

…eserve() call based on the apartment storage size before collecting apartment contexts to avoid repeated reallocations while holding the lock.\n\nCo-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@dunhor

Copy link
Copy Markdown
Member

Chris Guzak (@ChrisGuzak) IIRC you wrote most of this code, so you'd be able to better comment on this than me.

From the code, not all contexts are pushed into the vector, so this is likely going to over-allocate. That's probably fine - vector is likely going to over-allocate when it doubles its size the final time - however your description is missing the very important why? What problem are you trying to solve?

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