Skip to content

Commit 548aa0b

Browse files
authored
Fix the inconsistent behavior between Spine vector's setSize and std::vector's resize, which causes the Spine vector to allocate additional space upfront. (#18678)
1 parent 7ead7e4 commit 548aa0b

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

native/cocos/editor-support/spine/3.8/spine/Vector.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@ class SP_API Vector : public SpineObject {
8080
size_t oldSize = _size;
8181
_size = newSize;
8282
if (_capacity < newSize) {
83-
_capacity = (int)(_size * 1.75f);
83+
if (_capacity == 0) {
84+
_capacity = _size;
85+
} else {
86+
_capacity = (int)(_size * 1.75f);
87+
}
8488
if (_capacity < 8) _capacity = 8;
8589
_buffer = spine::SpineExtension::realloc<T>(_buffer, _capacity, __SPINE_FILE__, __SPINE_LINE__);
8690
}

native/cocos/editor-support/spine/4.2/spine/Vector.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ namespace spine {
5858
deallocate(_buffer);
5959
}
6060

61-
inline void clear() {
61+
void clear() {
6262
for (size_t i = 0; i < _size; ++i) {
6363
destroy(_buffer + (_size - 1 - i));
6464
}
@@ -74,12 +74,16 @@ namespace spine {
7474
return _size;
7575
}
7676

77-
inline void setSize(size_t newSize, const T &defaultValue) {
77+
void setSize(size_t newSize, const T &defaultValue) {
7878
assert(newSize >= 0);
7979
size_t oldSize = _size;
8080
_size = newSize;
8181
if (_capacity < newSize) {
82-
_capacity = (int) (_size * 1.75f);
82+
if (_capacity == 0) {
83+
_capacity = _size;
84+
} else {
85+
_capacity = (int) (_size * 1.75f);
86+
}
8387
if (_capacity < 8) _capacity = 8;
8488
_buffer = spine::SpineExtension::realloc<T>(_buffer, _capacity, __FILE__, __LINE__);
8589
}
@@ -94,13 +98,13 @@ namespace spine {
9498
}
9599
}
96100

97-
inline void ensureCapacity(size_t newCapacity = 0) {
101+
void ensureCapacity(size_t newCapacity = 0) {
98102
if (_capacity >= newCapacity) return;
99103
_capacity = newCapacity;
100104
_buffer = SpineExtension::realloc<T>(_buffer, newCapacity, __FILE__, __LINE__);
101105
}
102106

103-
inline void add(const T &inValue) {
107+
void add(const T &inValue) {
104108
if (_size == _capacity) {
105109
// inValue might reference an element in this buffer
106110
// When we reallocate, the reference becomes invalid.
@@ -128,7 +132,7 @@ namespace spine {
128132
this->addAll(inValue);
129133
}
130134

131-
inline void removeAt(size_t inIndex) {
135+
void removeAt(size_t inIndex) {
132136
assert(inIndex < _size);
133137

134138
--_size;

native/external-config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
"type": "github",
44
"owner": "cocos",
55
"name": "cocos-engine-external",
6-
"checkout": "v3.8.7-9"
6+
"checkout": "v3.8.7-10"
77
}
88
}

0 commit comments

Comments
 (0)