Skip to content

Improve std::vector allocations and insertions performance #37879

@mateusznowakTT

Description

@mateusznowakTT

Default std::vector allocation strategy is not optimal in case of insertion of multiple elements.
By default, initial capacity is 0, and with each insertion, it is extended to fit new item (g++ strategy is double the capacity), and existing items are migrated (copied or moved) to new underlying buffer. If memory is reserved ahead of insertions this can be hugely improved.

On top of that, there is a performance benefit from constructing vector elements in-place instead of copying or moving elements. Even inline initialization (std::vector v{v1, v2, v3};) suffers from this.

Microbenchmark presenting performance improvements possible https://quick-bench.com/q/4H4CNijgXcyUyAkaPMomSVDprPA

In scope of the task: analyze std::vector usage across codebase and change usage patterns to ones that should have less runtime overhead allowing to improve overall host-side performance.

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions