Skip to content

Add tunable_vector std::vector wrapper container#746

Draft
Spartan322 wants to merge 1 commit into
add/stl-like-containersfrom
add/tunable-vector
Draft

Add tunable_vector std::vector wrapper container#746
Spartan322 wants to merge 1 commit into
add/stl-like-containersfrom
add/tunable-vector

Conversation

@Spartan322
Copy link
Copy Markdown
Member

@Spartan322 Spartan322 commented May 20, 2026

Add tunable_vector memory alias to core/memory

@Spartan322 Spartan322 added enhancement New feature or request topic:core labels May 20, 2026
@Spartan322 Spartan322 force-pushed the add/stl-like-containers branch from 0c289ed to b2c561e Compare May 20, 2026 16:39
Add tunable_vector memory alias to `core/memory`
@Spartan322 Spartan322 force-pushed the add/tunable-vector branch from d00c51f to 27db712 Compare May 20, 2026 16:40
@wvpm
Copy link
Copy Markdown
Contributor

wvpm commented May 20, 2026

Add tunable_vector memory alias to core/memory

It shouldn't depend on that based on the code I read.
And it adds the entire type, not merely an alias.
Still nice draft.

}

template<
typename T, _detail::tunable_growth_trait GrowthTrait = growth_traits<2, 1>, typename Allocator = std::allocator<T>>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Doubling creates memory fragmentation as the sum of your old memory would be 2^n -1 and you need 2^n.
The upper bound that avoids this is the golden ratio ~1.6 so 1.5 would be a better default

constexpr void append_range(RangeT&& range) {
if constexpr (std::ranges::forward_range<RangeT> || std::ranges::sized_range<RangeT>) {
reserve_minimum(std::ranges::distance(range));
std::ranges::move(range, std::back_inserter(*this));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not ranges v3?
or std::uninitialized_copy + resize?


constexpr void resize(size_type count) {
if (size() == capacity()) {
reserve_minimum(count);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

That's unexpected. resize normally does not trigger a reallocation unless you lack capacity

}

if (value > new_capacity) {
return value;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This seems wrong.
Say you reserve for value and fill it to or near value then add 1-3 elements, it would have to reallocate again.
It would make more sense if it:

  • reserves for value + buffer size
  • reserves for value x factor
  • reserves for smallest factor^n that fits value (+ buffer)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request topic:core

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants