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

Use 1.5x growth factor for LocalVector #100944

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions core/templates/local_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ class LocalVector {

_FORCE_INLINE_ void push_back(T p_elem) {
if (unlikely(count == capacity)) {
capacity = tight ? (capacity + 1) : MAX((U)1, capacity << 1);
data = (T *)memrealloc(data, capacity * sizeof(T));
CRASH_COND_MSG(!data, "Out of memory");
reserve(count + 1);
}

if constexpr (!std::is_trivially_constructible_v<T> && !force_trivial) {
Expand Down Expand Up @@ -137,10 +135,16 @@ class LocalVector {
}
_FORCE_INLINE_ bool is_empty() const { return count == 0; }
_FORCE_INLINE_ U get_capacity() const { return capacity; }
_FORCE_INLINE_ void reserve(U p_size) {
p_size = tight ? p_size : nearest_power_of_2_templated(p_size);
void reserve(U p_size) {
if (p_size > capacity) {
capacity = p_size;
if (tight) {
Nazarwadim marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

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

You can just use capacity = tight ? p_size : MAX((U)2, capacity + ((1 + capacity) >> 1));

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it needs for

if (p_size > capacity) {

capacity = p_size;
} else {
capacity = MAX((U)2, capacity + ((1 + capacity) >> 1));
if (p_size > capacity) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is always true since you already tested this above. If it weren't true after the new capacity was found you'd get memory issues anyway, lol.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No. If we expand from 12 to 13 elements, then this condition will be false because 12 * 1.5 = 18; 12 > 18 == false.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah... I'm just now seeing that your implementation is different than I had thought it is.
The previous implementation used to grow to the nearest power of 2 of the requested size.
You implementation grows to 1.5x the previous capacity, and grows tightly to exactly the requested size if this exceeds the 1.5x growth.

I'm not sure which is better, tbh. I can imagine arguments for either implementation. But this is definitely a change of behavior that goes beyond the 1.5x growth factor instead of 2x.

Copy link
Contributor

Choose a reason for hiding this comment

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

@Nazarwadim This is the one I meant, it definitely warrants discussion before a merge.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So what's wrong with my implementation?

Copy link
Contributor

Choose a reason for hiding this comment

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

It's a change of behavior, as explained above.
Specifically, your implementation will be slower if reserve is called and more elements are added than reserved for, and slightly faster otherwise.
This may be a trade-off worth making, but I'm not sure it's appropriate for this PR. If it's possible with a 1.5x growth factor, I would keep it agnostic and use the same behavior as before.

capacity = p_size;
}
}
data = (T *)memrealloc(data, capacity * sizeof(T));
CRASH_COND_MSG(!data, "Out of memory");
}
Expand All @@ -156,11 +160,7 @@ class LocalVector {
}
count = p_size;
} else if (p_size > count) {
if (unlikely(p_size > capacity)) {
capacity = tight ? p_size : nearest_power_of_2_templated(p_size);
data = (T *)memrealloc(data, capacity * sizeof(T));
CRASH_COND_MSG(!data, "Out of memory");
}
reserve(p_size);
if constexpr (!std::is_trivially_constructible_v<T> && !force_trivial) {
for (U i = count; i < p_size; i++) {
memnew_placement(&data[i], T);
Expand Down
2 changes: 1 addition & 1 deletion servers/rendering/rendering_device_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ class RenderingDeviceDriver : public RenderingDeviceCommons {
};

struct AttachmentReference {
static const uint32_t UNUSED = 0xffffffff;
static constexpr uint32_t UNUSED = 0xffffffff;
uint32_t attachment = UNUSED;
TextureLayout layout = TEXTURE_LAYOUT_UNDEFINED;
BitField<TextureAspectBits> aspect;
Expand Down