Skip to content

Add back() and pop_back() to LocalVector#99955

Closed
YYF233333 wants to merge 1 commit intogodotengine:masterfrom
YYF233333:stack
Closed

Add back() and pop_back() to LocalVector#99955
YYF233333 wants to merge 1 commit intogodotengine:masterfrom
YYF233333:stack

Conversation

@YYF233333
Copy link
Contributor

@YYF233333 YYF233333 commented Dec 3, 2024

Follow up #99936.

Having some difficulties so I upload this as draft for discussion.

Which type should LocalVector::back() return?

  • Return T&.
    Simplify caller's code, but not that compitable with the semantic of reference, since one can get a nullptr if container is empty. What should be returned if empty? (We do have bound check so this is unlikely to happen)
  • Return T *.
    Shift the responsibility of checking nullptr to caller, resulting in a lot of explicit dereference. Also not sure if deref a pointer has the same effect as deref a reference for complex types.
  • Return Option<T>.
    Too rusty I think :)

What's your opinion?

Split the LocalVector change and List change to keep PR small and easy to review.

This one add back() and pop_back() method for LocalVector so it's capable as a stack. It will be used to replace List based stack in following PR.

@Spartan322
Copy link
Contributor

Isn't this where the CRASH macros would be used? Like

CRASH_BAD_UNSIGNED_INDEX(p_index, count);
does it for out of bounds.

@YYF233333
Copy link
Contributor Author

Sounds great, I'll try this.

@YYF233333 YYF233333 changed the title Replace List used as stack with LocalVector Add back() and pop_back() to LocalVector Dec 12, 2024
@YYF233333 YYF233333 marked this pull request as ready for review December 12, 2024 10:04
@YYF233333 YYF233333 requested a review from a team as a code owner December 12, 2024 10:04
Ivorforce
Ivorforce previously approved these changes Dec 12, 2024
Copy link
Member

@Ivorforce Ivorforce left a comment

Choose a reason for hiding this comment

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

Looks good to me! Useful functions.

Copy link
Member

@Ivorforce Ivorforce Dec 12, 2024

Choose a reason for hiding this comment

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

It's customary to return the value instead of destructing it, so that it can be used in structures like:

while (element = list.pop_pack()) {
}

But this is, unfortunately, not the case with equivalent functions of other list types. So I think it would be better to keep it as you have it, and address this shortcoming for all list types in a separate PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I remember std::vector::pop_back return void because of multi-thread concern so I follow this prcatice.

Copy link
Member

Choose a reason for hiding this comment

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

Why not use CRASH_BAD_UNSIGNED_INDEX here too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I remember godot prefer throw error and continue than crash, follow that rule.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, right, the other function crashes. Makes sense to me!

@clayjohn
Copy link
Member

This looks great! But just a heads up that, as a rule, we don't merge things into core without a tangible benefit (i.e. fixing a bug, needed for a new piece of code, improving performance).

In line with our best practices document the problem must always come before the solution. In other words, we don't add new things just because we can, or just because we think it might be useful to someone in the future. We add things when they become useful right now.

So this PR will remain on hold until someone implements something and really needs these functions for something they are implementing

@Ivorforce
Copy link
Member

So this PR will remain on hold until someone implements something and really needs these functions for something they are implementing

Does harmonizing LocalVector with List suffice as a reason to add the functions? (although notably, List.back() returns a pointer, not a reference).

@clayjohn
Copy link
Member

clayjohn commented Dec 13, 2024

So this PR will remain on hold until someone implements something and really needs these functions for something they are implementing

Does harmonizing LocalVector with List suffice as a reason to add the functions? (although notably, List.back() returns a pointer, not a reference).

No. It needs to be something with a tangible benefit.

The purpose of godotengine/godot-proposals#5144 isn't to just make Vector and LocalVector look the same. It's to allow us to avoid copying arrays as we pass them around. Avoiding copy operations is an example of a tangible benefit

@YYF233333
Copy link
Contributor Author

This looks great! But just a heads up that, as a rule, we don't merge things into core without a tangible benefit (i.e. fixing a bug, needed for a new piece of code, improving performance).

Note the title change, it is a prerequisite to replace some List. I'm a bit busy now so push this for review first. Will implement remain work if I get time.

It is fine if you don't want to merge it now, but do make a consensus over these methods. I don't want to end up like #97777 again :)

@YYF233333
Copy link
Contributor Author

Now we have #100433, can this be considered useful? 👀

@YYF233333 YYF233333 force-pushed the stack branch 2 times, most recently from 1e8ffbf to f202866 Compare January 14, 2025 13:55
@Ivorforce Ivorforce dismissed their stale review March 21, 2025 20:01

Dismissing as my checkmark turned green now, and I don't want to misrepresent the state of the PR.
Personally, I think we'll add back() and pop_back sooner or later anyway, but we may also just add it when it's needed.

@YYF233333
Copy link
Contributor Author

I have some ongoing changes that use this, will submit this along with those.

@YYF233333 YYF233333 closed this Mar 31, 2025
@YYF233333 YYF233333 deleted the stack branch March 31, 2025 14:40
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.

5 participants