Skip to content

Commit f200298

Browse files
committed
Avoid double copying
1 parent 8c7aa88 commit f200298

File tree

2 files changed

+23
-49
lines changed

2 files changed

+23
-49
lines changed

ext/opcache/zend_accelerator_util_funcs.c

+22-49
Original file line numberDiff line numberDiff line change
@@ -99,27 +99,17 @@ void zend_accel_move_user_functions(HashTable *src, zend_script *script)
9999
src->pDestructor = orig_dtor;
100100
}
101101

102-
static void zend_hash_clone_constants(HashTable *ht, HashTable *source)
102+
static void zend_hash_clone_constants(HashTable *ht)
103103
{
104104
Bucket *p, *end;
105105
zend_class_constant *c;
106106

107-
ht->nTableSize = source->nTableSize;
108-
ht->nTableMask = source->nTableMask;
109-
ht->nNumUsed = source->nNumUsed;
110-
ht->nNumOfElements = source->nNumOfElements;
111-
ht->nNextFreeElement = source->nNextFreeElement;
112-
ht->pDestructor = NULL;
113-
HT_FLAGS(ht) = HT_FLAGS(source);
114-
ht->nInternalPointer = 0;
115-
116107
if (!(HT_FLAGS(ht) & HASH_FLAG_INITIALIZED)) {
117-
ht->arData = source->arData;
118108
return;
119109
}
120110

121111
p = emalloc(HT_SIZE(ht));
122-
memcpy(p, HT_GET_DATA_ADDR(source), HT_USED_SIZE(ht));
112+
memcpy(p, HT_GET_DATA_ADDR(ht), HT_USED_SIZE(ht));
123113
HT_SET_DATA_ADDR(ht, p);
124114

125115
p = ht->arData;
@@ -138,27 +128,19 @@ static void zend_hash_clone_constants(HashTable *ht, HashTable *source)
138128
}
139129
}
140130

141-
static void zend_hash_clone_methods(HashTable *ht, HashTable *source, zend_class_entry *old_ce, zend_class_entry *ce)
131+
static void zend_hash_clone_methods(HashTable *ht)
142132
{
143133
Bucket *p, *end;
144134
zend_op_array *new_entry;
145135

146-
ht->nTableSize = source->nTableSize;
147-
ht->nTableMask = source->nTableMask;
148-
ht->nNumUsed = source->nNumUsed;
149-
ht->nNumOfElements = source->nNumOfElements;
150-
ht->nNextFreeElement = source->nNextFreeElement;
151136
ht->pDestructor = ZEND_FUNCTION_DTOR;
152-
HT_FLAGS(ht) = HT_FLAGS(source);
153-
ht->nInternalPointer = 0;
154137

155138
if (!(HT_FLAGS(ht) & HASH_FLAG_INITIALIZED)) {
156-
ht->arData = source->arData;
157139
return;
158140
}
159141

160142
p = emalloc(HT_SIZE(ht));
161-
memcpy(p, HT_GET_DATA_ADDR(source), HT_USED_SIZE(ht));
143+
memcpy(p, HT_GET_DATA_ADDR(ht), HT_USED_SIZE(ht));
162144
HT_SET_DATA_ADDR(ht, p);
163145

164146
p = ht->arData;
@@ -186,27 +168,17 @@ static void zend_hash_clone_methods(HashTable *ht, HashTable *source, zend_class
186168
}
187169
}
188170

189-
static void zend_hash_clone_prop_info(HashTable *ht, HashTable *source, zend_class_entry *old_ce)
171+
static void zend_hash_clone_prop_info(HashTable *ht)
190172
{
191173
Bucket *p, *end;
192174
zend_property_info *prop_info;
193175

194-
ht->nTableSize = source->nTableSize;
195-
ht->nTableMask = source->nTableMask;
196-
ht->nNumUsed = source->nNumUsed;
197-
ht->nNumOfElements = source->nNumOfElements;
198-
ht->nNextFreeElement = source->nNextFreeElement;
199-
ht->pDestructor = NULL;
200-
HT_FLAGS(ht) = HT_FLAGS(source);
201-
ht->nInternalPointer = 0;
202-
203176
if (!(HT_FLAGS(ht) & HASH_FLAG_INITIALIZED)) {
204-
ht->arData = source->arData;
205177
return;
206178
}
207179

208180
p = emalloc(HT_SIZE(ht));
209-
memcpy(p, HT_GET_DATA_ADDR(source), HT_USED_SIZE(ht));
181+
memcpy(p, HT_GET_DATA_ADDR(ht), HT_USED_SIZE(ht));
210182
HT_SET_DATA_ADDR(ht, p);
211183

212184
p = ht->arData;
@@ -236,48 +208,49 @@ static void zend_hash_clone_prop_info(HashTable *ht, HashTable *source, zend_cla
236208
static void zend_class_copy_ctor(zend_class_entry **pce)
237209
{
238210
zend_class_entry *ce = *pce;
239-
zend_class_entry *old_ce = ce;
240211
zval *src, *dst, *end;
241212

242-
*pce = ce = ARENA_REALLOC(old_ce);
213+
*pce = ce = ARENA_REALLOC(ce);
243214
ce->refcount = 1;
244215

245216
if ((ce->ce_flags & ZEND_ACC_LINKED) && IN_ARENA(ce->parent)) {
246217
ce->parent = ARENA_REALLOC(ce->parent);
247218
}
248219

249-
if (old_ce->default_properties_table) {
250-
ce->default_properties_table = emalloc(sizeof(zval) * old_ce->default_properties_count);
251-
src = old_ce->default_properties_table;
252-
end = src + old_ce->default_properties_count;
253-
dst = ce->default_properties_table;
220+
if (ce->default_properties_table) {
221+
dst = emalloc(sizeof(zval) * ce->default_properties_count);
222+
src = ce->default_properties_table;
223+
end = src + ce->default_properties_count;
224+
ce->default_properties_table = dst;
254225
for (; src != end; src++, dst++) {
255226
ZVAL_COPY_VALUE(dst, src);
256227
}
257228
}
258229

259-
zend_hash_clone_methods(&ce->function_table, &old_ce->function_table, old_ce, ce);
230+
zend_hash_clone_methods(&ce->function_table);
260231

261232
/* static members */
262-
if (old_ce->default_static_members_table) {
233+
if (ce->default_static_members_table) {
263234
int i, end;
264235
zend_class_entry *parent = !(ce->ce_flags & ZEND_ACC_LINKED) ? NULL : ce->parent;
265236

266-
ce->default_static_members_table = emalloc(sizeof(zval) * old_ce->default_static_members_count);
237+
dst = emalloc(sizeof(zval) * ce->default_static_members_count);
238+
src = ce->default_static_members_table;
239+
ce->default_static_members_table = dst;
267240
i = ce->default_static_members_count - 1;
268241

269242
/* Copy static properties in this class */
270243
end = parent ? parent->default_static_members_count : 0;
271244
for (; i >= end; i--) {
272-
zval *p = &ce->default_static_members_table[i];
273-
ZVAL_COPY_VALUE(p, &old_ce->default_static_members_table[i]);
245+
zval *p = &dst[i];
246+
ZVAL_COPY_VALUE(p, &src[i]);
274247
}
275248

276249
/* Create indirections to static properties from parent classes */
277250
while (parent && parent->default_static_members_table) {
278251
end = parent->parent ? parent->parent->default_static_members_count : 0;
279252
for (; i >= end; i--) {
280-
zval *p = &ce->default_static_members_table[i];
253+
zval *p = &dst[i];
281254
ZVAL_INDIRECT(p, &parent->default_static_members_table[i]);
282255
}
283256

@@ -287,10 +260,10 @@ static void zend_class_copy_ctor(zend_class_entry **pce)
287260
ZEND_MAP_PTR_INIT(ce->static_members_table, &ce->default_static_members_table);
288261

289262
/* properties_info */
290-
zend_hash_clone_prop_info(&ce->properties_info, &old_ce->properties_info, old_ce);
263+
zend_hash_clone_prop_info(&ce->properties_info);
291264

292265
/* constants table */
293-
zend_hash_clone_constants(&ce->constants_table, &old_ce->constants_table);
266+
zend_hash_clone_constants(&ce->constants_table);
294267

295268
if (ce->num_interfaces) {
296269
zend_class_name *interface_names;

ext/opcache/zend_persist.c

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ static void zend_hash_persist(HashTable *ht, zend_persist_func_t pPersistElement
9292

9393
HT_FLAGS(ht) |= HASH_FLAG_STATIC_KEYS;
9494
ht->pDestructor = NULL;
95+
ht->nInternalPointer = 0;
9596

9697
if (!(HT_FLAGS(ht) & HASH_FLAG_INITIALIZED)) {
9798
if (EXPECTED(!ZCG(current_persistent_script)->corrupted)) {

0 commit comments

Comments
 (0)