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
Merged
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: 16 additions & 6 deletions core/variant/typed_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.

}
_FORCE_INLINE_ TypedArray(const Array &p_array) :
Array(p_array, Variant::OBJECT, T::get_class_static(), Variant()) {
_FORCE_INLINE_ TypedArray(const Array &p_array) {
set_typed(Variant::OBJECT, T::get_class_static(), Variant());
Comment on lines +51 to +52
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

if (is_same_typed(p_array)) {
_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

}
_FORCE_INLINE_ TypedArray() {
set_typed(Variant::OBJECT, T::get_class_static(), Variant());
Expand Down Expand Up @@ -78,10 +83,15 @@ struct VariantInternalAccessor<const TypedArray<T> &> {
_ref(p_array); \
} \
_FORCE_INLINE_ TypedArray(const Variant &p_variant) : \
Array(Array(p_variant), m_variant_type, StringName(), Variant()) { \
TypedArray(Array(p_variant)) { \
} \
_FORCE_INLINE_ TypedArray(const Array &p_array) : \
Array(p_array, m_variant_type, StringName(), Variant()) { \
_FORCE_INLINE_ TypedArray(const Array &p_array) { \
set_typed(m_variant_type, StringName(), Variant()); \
if (is_same_typed(p_array)) { \
_ref(p_array); \
} else { \
assign(p_array); \
} \
} \
_FORCE_INLINE_ TypedArray() { \
set_typed(m_variant_type, StringName(), Variant()); \
Expand Down
37 changes: 37 additions & 0 deletions tests/core/variant/test_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,43 @@ TEST_CASE("[Array] Iteration and modification") {
a4.clear();
}

TEST_CASE("[Array] Typed copying") {
TypedArray<int> a1;
a1.push_back(1);

TypedArray<double> a2;
a2.push_back(1.0);

Array a3 = a1;
TypedArray<int> a4 = a3;

Array a5 = a2;
TypedArray<int> a6 = a5;

a3[0] = 2;
a4[0] = 3;

// Same typed TypedArray should be shared.
CHECK_EQ(a1[0], Variant(3));
CHECK_EQ(a3[0], Variant(3));
CHECK_EQ(a4[0], Variant(3));

a5[0] = 2.0;
a6[0] = 3.0;

// Different typed TypedArray should not be shared.
CHECK_EQ(a2[0], Variant(2.0));
CHECK_EQ(a5[0], Variant(2.0));
CHECK_EQ(a6[0], Variant(3.0));

a1.clear();
a2.clear();
a3.clear();
a4.clear();
a5.clear();
a6.clear();
}

} // namespace TestArray

#endif // TEST_ARRAY_H
Loading