• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

krakjoe / parallel / 20431104443

22 Dec 2025 11:51AM UTC coverage: 94.99% (-1.9%) from 96.873%
20431104443

push

github

realFlowControl
bump version

2844 of 2994 relevant lines covered (94.99%)

6449.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.6
/src/cache.c
1
/*
2
  +----------------------------------------------------------------------+
3
  | parallel                                                             |
4
  +----------------------------------------------------------------------+
5
  | Copyright (c) Joe Watkins 2019-2024                                  |
6
  +----------------------------------------------------------------------+
7
  | This source file is subject to version 3.01 of the PHP license,      |
8
  | that is bundled with this package in the file LICENSE, and is        |
9
  | available through the world-wide-web at the following url:           |
10
  | http://www.php.net/license/3_01.txt                                  |
11
  | If you did not receive a copy of the PHP license and are unable to   |
12
  | obtain it through the world-wide-web, please send a note to          |
13
  | license@php.net so we can mail you a copy immediately.               |
14
  +----------------------------------------------------------------------+
15
  | Author: krakjoe                                                      |
16
  +----------------------------------------------------------------------+
17
 */
18
#ifndef HAVE_PARALLEL_CACHE
19
#define HAVE_PARALLEL_CACHE
20

21
#include "parallel.h"
22

23
static struct {
24
        pthread_mutex_t mutex;
25
        HashTable       table;
26
        struct {
27
                size_t size;
28
                size_t used;
29
                void  *mem;
30
                void  *block;
31
        } memory;
32
} php_parallel_cache_globals = {PTHREAD_MUTEX_INITIALIZER};
33

34
#define PCG(e) php_parallel_cache_globals.e
35
#define PCM(e) PCG(memory).e
36

37
#define PARALLEL_CACHE_CHUNK PARALLEL_PLATFORM_ALIGNED((1024 * 1024) * 8)
38

39
/* {{{ */
40
static zend_always_inline void *php_parallel_cache_alloc(size_t size)
5,374✔
41
{
42
        void  *mem;
5,374✔
43
        size_t aligned = PARALLEL_PLATFORM_ALIGNED(size);
5,374✔
44

45
        ZEND_ASSERT(size < PARALLEL_CACHE_CHUNK);
5,374✔
46

47
        if ((PCM(used) + aligned) >= PCM(size)) {
5,374✔
48
                PCM(size) = PARALLEL_PLATFORM_ALIGNED(PCM(size) + PARALLEL_CACHE_CHUNK);
×
49
                PCM(mem) = (void *)realloc(PCM(mem), PCM(size));
×
50

51
                if (!PCM(mem)) {
×
52
                        /* out of memory */
53
                        return NULL;
54
                }
55

56
                PCM(block) = (void *)(((char *)PCM(mem)) + PCM(used));
×
57
        }
58

59
        mem = PCM(block);
5,374✔
60
        PCM(block) = (void *)(((char *)PCM(block)) + aligned);
5,374✔
61
        PCM(used) += aligned;
5,374✔
62

63
        return mem;
5,374✔
64
}
65

66
static zend_always_inline void *php_parallel_cache_copy_mem(void *source, zend_long size)
3,987✔
67
{
68
        void *destination = php_parallel_cache_alloc(size);
120✔
69

70
        memcpy(destination, source, size);
3,867✔
71

72
        return destination;
3,987✔
73
} /* }}} */
74

75
static zend_always_inline HashTable *php_parallel_cache_statics(HashTable *statics)
21✔
76
{ /* {{{ */
77
        HashTable *cached = zend_hash_index_find_ptr(&PCG(table), (zend_ulong)statics);
42✔
78

79
        if (cached) {
×
80
                return cached;
81
        }
82

83
        cached = php_parallel_copy_hash_persistent(statics, php_parallel_copy_string_interned, php_parallel_cache_copy_mem);
21✔
84

85
        return zend_hash_index_update_ptr(&PCG(table), (zend_ulong)statics, cached);
21✔
86
} /* }}} */
87

88
static zend_always_inline void php_parallel_cache_type(zend_type *type)
266✔
89
{ /* {{{ */
90
        zend_type *single;
266✔
91

92
        if (!ZEND_TYPE_IS_SET(*type)) {
266✔
93
                return;
94
        }
95

96
        if (ZEND_TYPE_HAS_LIST(*type)) {
150✔
97
                zend_type_list *list = ZEND_TYPE_LIST(*type);
6✔
98

99
                list = php_parallel_cache_copy_mem(list, ZEND_TYPE_LIST_SIZE(list->num_types));
6✔
100

101
                if (ZEND_TYPE_USES_ARENA(*type)) {
6✔
102
                        ZEND_TYPE_FULL_MASK(*type) &= ~_ZEND_TYPE_ARENA_BIT;
5✔
103
                }
104

105
                ZEND_TYPE_SET_PTR(*type, list);
6✔
106
        }
107

108
        ZEND_TYPE_FOREACH(*type, single)
150✔
109
        {
110
                if (ZEND_TYPE_HAS_NAME(*single)) {
156✔
111
                        zend_string *name = ZEND_TYPE_NAME(*single);
90✔
112

113
                        ZEND_TYPE_SET_PTR(*single, php_parallel_copy_string_interned(name));
90✔
114
                }
115
        }
116
        ZEND_TYPE_FOREACH_END();
156✔
117
} /* }}} */
118

119
/* {{{ */
120
static zend_op_array *php_parallel_cache_create(const zend_function *source, bool statics)
2,469✔
121
{
122
        zend_op_array *cached = php_parallel_cache_copy_mem((void *)source, sizeof(zend_op_array));
2,469✔
123
        uint32_t      *literal_map = NULL;
2,469✔
124
        uint32_t      *offset_map = NULL;
2,469✔
125
        uint32_t       new_last_literal = cached->last_literal;
2,469✔
126

127
        cached->fn_flags |= ZEND_ACC_IMMUTABLE;
2,469✔
128

129
        if (statics && cached->static_variables) {
2,469✔
130
                cached->static_variables = php_parallel_cache_statics(cached->static_variables);
42✔
131
        }
132

133
#if PHP_VERSION_ID >= 80200
134
        ZEND_MAP_PTR_INIT(cached->static_variables_ptr, cached->static_variables);
1,680✔
135
#else
136
        ZEND_MAP_PTR_INIT(cached->static_variables_ptr, &cached->static_variables);
789✔
137
#endif
138

139
        ZEND_MAP_PTR_INIT(cached->run_time_cache, NULL);
2,469✔
140

141
#if PHP_VERSION_ID >= 80100
142
        if (cached->num_dynamic_func_defs) {
2,100✔
143
                uint32_t it = 0;
180✔
144

145
                cached->dynamic_func_defs = php_parallel_cache_copy_mem(
360✔
146
                    cached->dynamic_func_defs, sizeof(zend_op_array *) * cached->num_dynamic_func_defs);
180✔
147

148
                while (it < cached->num_dynamic_func_defs) {
390✔
149
                        cached->dynamic_func_defs[it] =
420✔
150
                            (zend_op_array *)php_parallel_cache_create((zend_function *)cached->dynamic_func_defs[it], statics);
210✔
151
                        it++;
210✔
152
                }
153
        }
154
#endif
155

156
        if (!cached->refcount) {
2,469✔
157
                goto _php_parallel_cached_function_return;
1,616✔
158
        }
159

160
        cached->refcount = NULL;
853✔
161

162
        if (cached->last_literal) {
853✔
163
                zend_op *src_opline = source->op_array.opcodes;
853✔
164
                zend_op *src_end = src_opline + source->op_array.last;
853✔
165

166
                // A map to keep track of which literals are referenced by the
167
                // `ZEND_INIT_FCALL` opcodes we found so that we can expand those later
168
                literal_map = emalloc(sizeof(uint32_t) * cached->last_literal);
853✔
169
                memset(literal_map, 0, sizeof(uint32_t) * cached->last_literal);
853✔
170

171
                // Search for `ZEND_INIT_FCALL` opcodes and remember the indexes for the
172
                // literals, as we are rewriting them later to `ZEND_INIT_FCALL_BY_NAME`
173
                // which requires a second, lower cased literal just in the next literal
174
                // slot.
175
                while (src_opline < src_end) {
6,567✔
176
                        if (src_opline->opcode == ZEND_INIT_FCALL && src_opline->op2_type == IS_CONST) {
5,714✔
177
                                uint32_t idx;
270✔
178
#if ZEND_USE_ABS_CONST_ADDR
179
                                idx = (zval *)src_opline->op2.zv - source->op_array.literals;
180
#else
181
                                idx = ((zval *)((char *)src_opline + src_opline->op2.constant) - source->op_array.literals);
270✔
182
#endif
183
                                if (idx < cached->last_literal) {
270✔
184
                                        if (literal_map[idx] == 0) {
270✔
185
                                                literal_map[idx] = 1;
270✔
186
                                                new_last_literal++;
270✔
187
                                        }
188
                                }
189
                        }
190
                        src_opline++;
5,714✔
191
                }
192
        }
193

194
        if (new_last_literal) {
853✔
195
                zval    *literal = source->op_array.literals;
853✔
196
                zval    *slot = php_parallel_cache_alloc(sizeof(zval) * new_last_literal);
853✔
197
                uint32_t idx = 0;
853✔
198

199
                offset_map = emalloc(sizeof(uint32_t) * cached->last_literal);
853✔
200

201
                cached->literals = slot;
853✔
202

203
                for (uint32_t i = 0; i < cached->last_literal; i++) {
4,289✔
204
                        /* Record the mapping from old literal index (i) to new literal index (idx)
205
                           so we can update opcode operands later. */
206
                        offset_map[i] = idx;
3,436✔
207

208
                        if (Z_TYPE_P(literal) == IS_ARRAY) {
3,436✔
209
                                ZVAL_ARR(slot, php_parallel_copy_hash_persistent(Z_ARRVAL_P(literal), php_parallel_copy_string_interned,
42✔
210
                                                                                 php_parallel_cache_copy_mem));
211
                        } else if (Z_TYPE_P(literal) == IS_STRING) {
3,394✔
212
                                ZVAL_STR(slot, php_parallel_copy_string_interned(Z_STR_P(literal)));
1,974✔
213
                        } else {
214
                                *slot = *literal;
1,420✔
215
                        }
216

217
                        Z_TYPE_FLAGS_P(slot) &= ~(IS_TYPE_REFCOUNTED | IS_TYPE_COLLECTABLE);
3,436✔
218

219
                        /* If this literal was used by INIT_FCALL, insert its lowercased version next. */
220
                        if (literal_map[i]) {
3,436✔
221
                                zend_string *lower = zend_string_tolower(Z_STR_P(slot));
270✔
222
                                slot++;
270✔
223
                                idx++;
270✔
224
                                ZVAL_STR(slot, php_parallel_copy_string_interned(lower));
270✔
225
                                zend_string_release(lower);
270✔
226
                                Z_TYPE_FLAGS_P(slot) &= ~(IS_TYPE_REFCOUNTED | IS_TYPE_COLLECTABLE);
270✔
227
                        }
228

229
                        literal++;
3,436✔
230
                        slot++;
3,436✔
231
                        idx++;
3,436✔
232
                }
233
                cached->last_literal = new_last_literal;
853✔
234
        }
235

236
        if (cached->last_var) {
853✔
237
                zend_string **vars = cached->vars;
534✔
238
                uint32_t      it = 0, end = cached->last_var;
534✔
239
                zend_string **heap = php_parallel_cache_alloc(cached->last_var * sizeof(zend_string *));
534✔
240

241
                while (it < end) {
1,269✔
242
                        heap[it] = php_parallel_copy_string_interned(vars[it]);
735✔
243
                        it++;
735✔
244
                }
245
                cached->vars = heap;
534✔
246
        }
247

248
        if (cached->last) {
853✔
249
                zend_op *opcodes = php_parallel_cache_copy_mem(cached->opcodes, sizeof(zend_op) * cached->last);
853✔
250
                zend_op *opline = opcodes, *end = opline + cached->last;
853✔
251

252
                while (opline < end) {
6,567✔
253
                        /* Replace ZEND_INIT_FCALL with ZEND_INIT_FCALL_BY_NAME.
254
                           We must clear op1_type (IS_UNUSED) and op1.var (0) to invalidate the
255
                           original thread's cache slot. */
256
                        if (opline->opcode == ZEND_INIT_FCALL) {
5,714✔
257
                                opline->opcode = ZEND_INIT_FCALL_BY_NAME;
270✔
258
                                opline->op1_type = IS_UNUSED;
270✔
259
                                opline->op1.var = 0;
270✔
260
                                ZEND_VM_SET_OPCODE_HANDLER(opline);
270✔
261
                        }
262

263
                        /* Remap IS_CONST operands to their new locations in the expanded literal table
264
                           using the offset_map we built earlier. */
265
                        if (opline->op1_type == IS_CONST) {
5,714✔
266
                                uint32_t idx;
1,809✔
267
                                zend_op *src_opline = source->op_array.opcodes + (opline - opcodes);
1,809✔
268
#if ZEND_USE_ABS_CONST_ADDR
269
                                idx = (zval *)src_opline->op1.zv - source->op_array.literals;
270
                                opline->op1.zv = &cached->literals[offset_map[idx]];
271
#else
272
                                idx = ((zval *)((char *)src_opline + src_opline->op1.constant) - source->op_array.literals);
1,809✔
273
                                opline->op1.constant = (char *)&cached->literals[offset_map[idx]] - (char *)opline;
1,809✔
274
#endif
275
                                if (opline->opcode == ZEND_SEND_VAL || opline->opcode == ZEND_SEND_VAL_EX ||
1,809✔
276
                                    opline->opcode == ZEND_QM_ASSIGN) {
277
                                        zend_vm_set_opcode_handler_ex(opline, 0, 0, 0);
255✔
278
                                }
279
                        }
280
                        if (opline->op2_type == IS_CONST) {
5,714✔
281
                                uint32_t idx;
1,014✔
282
                                zend_op *src_opline = source->op_array.opcodes + (opline - opcodes);
1,014✔
283
#if ZEND_USE_ABS_CONST_ADDR
284
                                idx = (zval *)src_opline->op2.zv - source->op_array.literals;
285
                                opline->op2.zv = &cached->literals[offset_map[idx]];
286
#else
287
                                idx = ((zval *)((char *)src_opline + src_opline->op2.constant) - source->op_array.literals);
1,014✔
288
                                opline->op2.constant = (char *)&cached->literals[offset_map[idx]] - (char *)opline;
1,014✔
289
#endif
290
                        }
291
#if ZEND_USE_ABS_JMP_ADDR
292
                        switch (opline->opcode) {
293
                        case ZEND_JMP:
294
                        case ZEND_FAST_CALL:
295
                                opline->op1.jmp_addr = &opcodes[opline->op1.jmp_addr - source->op_array.opcodes];
296
                                break;
297
#if PHP_VERSION_ID < 80200
298
                        case ZEND_JMPZNZ:
299
#endif
300
                        case ZEND_JMPZ:
301
                        case ZEND_JMPNZ:
302
                        case ZEND_JMPZ_EX:
303
                        case ZEND_JMPNZ_EX:
304
                        case ZEND_JMP_SET:
305
                        case ZEND_COALESCE:
306
                        case ZEND_FE_RESET_R:
307
                        case ZEND_FE_RESET_RW:
308
                        case ZEND_ASSERT_CHECK:
309
                                opline->op2.jmp_addr = &opcodes[opline->op2.jmp_addr - source->op_array.opcodes];
310
                                break;
311

312
                        case ZEND_CATCH:
313
                                if (!(opline->extended_value & ZEND_LAST_CATCH)) {
314
                                        opline->op2.jmp_addr = &opcodes[opline->op2.jmp_addr - source->op_array.opcodes];
315
                                }
316
                                break;
317
                        }
318
#endif
319

320
                        opline++;
5,714✔
321
                }
322
                cached->opcodes = opcodes;
853✔
323
        }
324

325
        if (literal_map) {
853✔
326
                efree(literal_map);
853✔
327
        }
328

329
        if (offset_map) {
853✔
330
                efree(offset_map);
853✔
331
        }
332

333
        if (cached->arg_info) {
853✔
334
                zend_arg_info *it = cached->arg_info, *end = it + cached->num_args, *info;
218✔
335

336
                if (cached->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
218✔
337
                        it--;
36✔
338
                }
339
                if (cached->fn_flags & ZEND_ACC_VARIADIC) {
218✔
340
                        end++;
12✔
341
                }
342

343
                cached->arg_info = info = php_parallel_cache_copy_mem(it, (end - it) * sizeof(zend_arg_info));
218✔
344

345
                while (it < end) {
484✔
346
                        if (info->name) {
266✔
347
                                info->name = php_parallel_copy_string_interned(it->name);
230✔
348
                        }
349

350
                        php_parallel_cache_type(&info->type);
266✔
351

352
                        info++;
266✔
353
                        it++;
266✔
354
                }
355
                if (cached->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
218✔
356
                        cached->arg_info++;
36✔
357
                }
358
        }
359

360
        if (cached->try_catch_array) {
853✔
361
                cached->try_catch_array = php_parallel_cache_copy_mem(cached->try_catch_array,
36✔
362
                                                                      sizeof(zend_try_catch_element) * cached->last_try_catch);
36✔
363
        }
364

365
        if (cached->live_range) {
853✔
366
                cached->live_range =
210✔
367
                    php_parallel_cache_copy_mem(cached->live_range, sizeof(zend_live_range) * cached->last_live_range);
210✔
368
        }
369

370
        if (cached->function_name)
853✔
371
                cached->function_name = php_parallel_copy_string_interned(cached->function_name);
853✔
372

373
        if (cached->filename)
853✔
374
                cached->filename = php_parallel_copy_string_interned(cached->filename);
853✔
375

376
        if (cached->doc_comment)
853✔
377
                cached->doc_comment = php_parallel_copy_string_interned(cached->doc_comment);
6✔
378

379
_php_parallel_cached_function_return:
847✔
380
        return cached;
2,469✔
381
} /* }}} */
382

383
/* {{{ */
384
static zend_always_inline zend_function *php_parallel_cache_function_ex(const zend_function *source, bool statics)
2,943✔
385
{
386
        zend_op_array *cached;
2,943✔
387

388
        pthread_mutex_lock(&PCG(mutex));
2,943✔
389

390
        if ((cached = zend_hash_index_find_ptr(&PCG(table), (zend_ulong)source->op_array.opcodes))) {
3,627✔
391
                goto _php_parallel_cached_function_return;
684✔
392
        }
393

394
        cached = php_parallel_cache_create(source, statics);
2,259✔
395

396
        zend_hash_index_add_ptr(&PCG(table), (zend_ulong)source->op_array.opcodes, cached);
2,259✔
397

398
_php_parallel_cached_function_return:
2,943✔
399
        pthread_mutex_unlock(&PCG(mutex));
2,943✔
400

401
        return (zend_function *)cached;
2,943✔
402
} /* }}} */
403

404
zend_function *php_parallel_cache_closure(const zend_function *source, zend_function *closure)
2,907✔
405
{ /* {{{ */
406
        zend_op_array *cache;
2,907✔
407

408
        cache = (zend_op_array *)php_parallel_cache_function_ex((zend_function *)source, 0);
2,907✔
409

410
        if (!closure) {
2,907✔
411
                closure = php_parallel_copy_mem(cache, sizeof(zend_op_array), 1);
2,235✔
412
        } else {
413
                memcpy(closure, cache, sizeof(zend_op_array));
672✔
414
        }
415

416
        if (source->op_array.static_variables) {
2,907✔
417
                HashTable *statics = ZEND_MAP_PTR_GET(source->op_array.static_variables_ptr);
693✔
418

419
                if (statics) {
693✔
420
                        closure->op_array.static_variables = php_parallel_copy_hash_ctor(statics, 1);
558✔
421

422
#if PHP_VERSION_ID >= 80200
423
                        ZEND_MAP_PTR_INIT(closure->op_array.static_variables_ptr, closure->op_array.static_variables);
372✔
424
#else
425
                        ZEND_MAP_PTR_INIT(closure->op_array.static_variables_ptr, &closure->op_array.static_variables);
186✔
426
#endif
427
                }
428
        }
429

430
#if PHP_VERSION_ID >= 80100
431
        if (source->op_array.num_dynamic_func_defs) {
2,470✔
432
                uint32_t it = 0;
195✔
433
                /* Use regular persistent memory for dynamic_func_defs array, not cache pool */
434
                closure->op_array.dynamic_func_defs =
390✔
435
                    pemalloc(sizeof(zend_op_array *) * source->op_array.num_dynamic_func_defs, 1);
195✔
436
                memcpy(closure->op_array.dynamic_func_defs, source->op_array.dynamic_func_defs,
615✔
437
                       sizeof(zend_op_array *) * source->op_array.num_dynamic_func_defs);
195✔
438
                while (it < source->op_array.num_dynamic_func_defs) {
420✔
439
                        closure->op_array.dynamic_func_defs[it] = (zend_op_array *)php_parallel_cache_closure(
450✔
440
                            (zend_function *)source->op_array.dynamic_func_defs[it], NULL);
225✔
441
                        it++;
225✔
442
                }
443
        }
444
#endif
445

446
        return closure;
2,907✔
447
} /* }}} */
448

449
zend_function *php_parallel_cache_function(const zend_function *source)
36✔
450
{ /* {{{ */
451
        return php_parallel_cache_function_ex(source, 1);
36✔
452
} /* }}} */
453

454
/* {{{ */
455
PHP_MINIT_FUNCTION(PARALLEL_CACHE)
3,210✔
456
{
457
        zend_hash_init(&PCG(table), 32, NULL, NULL, 1);
3,210✔
458

459
        PCM(size) = PARALLEL_CACHE_CHUNK;
3,210✔
460
        PCM(mem) = PCM(block) = malloc(PCM(size));
3,210✔
461

462
        if (!PCM(mem)) {
3,210✔
463
                /* out of memory */
464
        }
3,210✔
465

466
        return SUCCESS;
3,210✔
467
}
468

469
PHP_MSHUTDOWN_FUNCTION(PARALLEL_CACHE)
3,210✔
470
{
471
        zend_hash_destroy(&PCG(table));
3,210✔
472

473
        if (PCM(mem))
3,210✔
474
                free(PCM(mem));
3,210✔
475

476
        return SUCCESS;
3,210✔
477
} /* }}} */
478
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc