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

[Core] Fix sharing of typed arrays from constructor #89197

Merged
merged 1 commit into from
Jun 26, 2024

Conversation

AThousandShips
Copy link
Member

This matches the copy constructor of Array when same typed, which I'd say is expected behavior

@AThousandShips AThousandShips added bug topic:core cherrypick:4.2 Considered for cherry-picking into a future 4.2.x release labels Mar 5, 2024
@AThousandShips AThousandShips added this to the 4.3 milestone Mar 5, 2024
@AThousandShips AThousandShips requested review from a team as code owners March 5, 2024 20:24
@@ -46,10 +46,15 @@ class TypedArray : public Array {
_ref(p_array);
}
_FORCE_INLINE_ TypedArray(const Variant &p_variant) :
Array(Array(p_variant), Variant::OBJECT, T::get_class_static(), Variant()) {
TypedArray(Array(p_variant)) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Simplified this by delegating to avoid copying code

Copy link
Member

Choose a reason for hiding this comment

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

I'm all in to simplify the code.

Comment on lines +51 to +52
_FORCE_INLINE_ TypedArray(const Array &p_array) {
set_typed(Variant::OBJECT, T::get_class_static(), Variant());
Copy link
Member Author

Choose a reason for hiding this comment

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

Suggested change
_FORCE_INLINE_ TypedArray(const Array &p_array) {
set_typed(Variant::OBJECT, T::get_class_static(), Variant());
_FORCE_INLINE_ TypedArray(const Array &p_array) :
TypedArray() {

Alternatively, to reduce duplication even further

@akien-mga akien-mga requested a review from dalexeev March 5, 2024 21:38
_ref(p_array);
} else {
assign(p_array);
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Alternatively this block can be replaced by:

		if (p_array.get_typed_builtin() == Variant::OBJECT && p_array.get_typed_class_name() == T::get_class_static() && p_array.get_typed_script() == Variant()) {
			_ref(p_array);
		} else {
			set_typed(Variant::OBJECT, T::get_class_static(), Variant());
			assign(p_array);
		}

To avoid doing the typing part, though in either case it will allocate private data possibly unnecessarily

Copy link
Member

Choose a reason for hiding this comment

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

though in either case it will allocate private data possibly unnecessarily

Then, I would keep the simplest version.

Copy link
Member Author

Choose a reason for hiding this comment

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

Then I will and a future cleanup or improvement can hand that

@allenwp
Copy link
Contributor

allenwp commented Jun 21, 2024

I just tried this PR and it fixes the issue using the minimum reproduction code that I shared in #89191.

Copy link
Contributor

@dsnopek dsnopek left a comment

Choose a reason for hiding this comment

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

This looks good to me!

After this change, is the Array(const Array &p_base, uint32_t p_type, const StringName &p_class_name, const Variant &p_script) constructor even used anywhere? If not, I wonder if it should be removed? Anyway, not important questions for now, things could be cleaned up more later :-)

@AThousandShips
Copy link
Member Author

AThousandShips commented Jun 26, 2024

I think it's used in scripting, as the template can't be used in dynamic code, only static code, so in GDScript etc. the explicit constructor is relevant

Edit: it is used, for example here:

*dst = Array(array, builtin_type, native_type, *script_type);

@akien-mga akien-mga merged commit 3d8562d into godotengine:master Jun 26, 2024
16 checks passed
@AThousandShips AThousandShips deleted the arr_typed_fix branch June 26, 2024 16:28
@akien-mga
Copy link
Member

Thanks!

@AThousandShips
Copy link
Member Author

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug cherrypick:4.2 Considered for cherry-picking into a future 4.2.x release topic:core
Projects
None yet
Development

Successfully merging this pull request may close these issues.

C++ A deep copy (COW) of TypedArray is sometimes incorrectly made
5 participants