Skip to content

Conversation

@Ivorforce
Copy link
Member

@Ivorforce Ivorforce commented Jan 9, 2026

This adds back access to LocalVector, so that it can be efficiently used as a stack.
The current prevalent class for stack-like containers is List, which is very inefficient.

In draft until it's actually needed.

Discussion

I added an access method back() (const and non-const form), which returns a reference to the last element. This can be error prone if called when the vector is empty. In this case, we can do naught but crash.
An alternative would be returning an optional, but that would (slightly) increase the cost of the call too.

pop_back in List returns no element. This can lead to inefficient code, because elements have to be copied out of the List on pop.
Instead, I return the element (moved out of the list), like other languages (eg Rust and Python). This can lead to much more efficient code.
However, this can still be inefficient if the element need only be removed without fetching it. For this case, remove_back is added. Practically, this may not be needed because after inlining, pop_back might be optimized to the same standard as remove_back.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant