8000 An attempt to implemnt "preloading" ability. · php/php-src@cf819ee · GitHub
[go: up one dir, main page]

Skip to content

Commit cf819ee

Browse files
committed
An attempt to implemnt "preloading" ability.
On startup, PHP may execute a script defined by opcache.preload configuration directive. All function and classes loaded by this script became permanent and available to all the following requests. For example, it's possible to preload almost whole Zend Framework, and save significant time on each request. This is an unfinished PoC yet.
1 parent 056b551 commit cf819ee

11 files changed

+955
-38
lines changed

Zend/zend_API.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3676,6 +3676,7 @@ ZEND_API int zend_declare_property_ex(zend_class_entry *ce, zend_string *name, z
36763676
} else {
36773677
property_info = zend_arena_alloc(&CG(arena), sizeof(zend_property_info));
36783678
if (Z_TYPE_P(property) == IS_CONSTANT_AST) {
3679+
ce->ce_flags |= ZEND_HAS_CONSTANTS;
36793680
ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
36803681
}
36813682
}
@@ -3851,6 +3852,7 @@ ZEND_API int zend_declare_class_constant_ex(zend_class_entry *ce, zend_string *n
38513852
c->doc_comment = doc_comment;
38523853
c->ce = ce;
38533854
if (Z_TYPE_P(value) == IS_CONSTANT_AST) {
3855+
ce->ce_flags |= ZEND_HAS_CONSTANTS;
38543856
ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
38553857
}
38563858

Zend/zend_compile.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6317,6 +6317,10 @@ void zend_compile_class_decl(zend_ast *ast, zend_bool toplevel) /* {{{ */
63176317

63186318
CG(active_class_entry) = original_ce;
63196319

6320+
if (toplevel) {
6321+
ce->ce_flags |= ZEND_ACC_TOP_LEVEL;
6322+
}
6323+
63206324
if (toplevel
63216325
/* We currently don't early-bind classes that implement interfaces or use traits */
63226326
&& !(ce->ce_flags & (ZEND_ACC_IMPLEMENT_INTERFACES|ZEND_ACC_IMPLEMENT_TRAITS))) {

Zend/zend_compile.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ typedef struct _zend_oparray_context {
224224
/* Function has typed arguments / class has typed props | | | */
225225
#define ZEND_ACC_HAS_TYPE_HINTS (1 << 8) /* ? | X | | */
226226
/* | | | */
227-
/* Class Flags (unused: 15...) | | | */
227+
/* Class Flags (unused: 17...) | | | */
228228
/* =========== | | | */
229229
/* | | | */
230230
/* Special class types | | | */
@@ -258,6 +258,12 @@ typedef struct _zend_oparray_context {
258258
/* User class has methods with static variables | | | */
259259
#define ZEND_HAS_STATIC_IN_METHODS (1 << 14) /* X | | | */
260260
/* | | | */
261+
/* User class has methods with static variables | | | */
262+
#define ZEND_HAS_CONSTANTS (1 << 15) /* X | | | */
263+
/* | | | */
264+
/* Top-level class declaration | | | */
265+
#define ZEND_ACC_TOP_LEVEL (1 << 16) /* X | | | */
266+
/* | | | */
261267
/* Function Flags (unused: 25...30) | | | */
262268
/* ============== | | | */
263269
/* | | | */

Zend/zend_inheritance.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,7 @@ static void do_inherit_class_constant(zend_string *name, zend_class_constant *pa
770770
}
771771
} else if (!(Z_ACCESS_FLAGS(parent_const->value) & ZEND_ACC_PRIVATE)) {
772772
if (Z_TYPE(parent_const->value) == IS_CONSTANT_AST) {
773+
ce->ce_flags |= ZEND_HAS_CONSTANTS;
773774
ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
774775
}
775776
if (ce->type & ZEND_INTERNAL_CLASS) {
@@ -855,6 +856,7 @@ ZEND_API void zend_do_inheritance(zend_class_entry *ce, zend_class_entry *parent
855856
src--;
856857
ZVAL_COPY_OR_DUP(dst, src);
857858
if (Z_OPT_TYPE_P(dst) == IS_CONSTANT_AST) {
859+
ce->ce_flags |= ZEND_HAS_CONSTANTS;
858860
ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
859861
}
860862
continue;
@@ -865,6 +867,7 @@ ZEND_API void zend_do_inheritance(zend_class_entry *ce, zend_class_entry *parent
865867
src--;
866868
ZVAL_COPY(dst, src);
867869
if (Z_OPT_TYPE_P(dst) == IS_CONSTANT_AST) {
870+
ce->ce_flags |= ZEND_HAS_CONSTANTS;
868871
ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
869872
}
870873
continue;
@@ -920,6 +923,7 @@ ZEND_API void zend_do_inheritance(zend_class_entry *ce, zend_class_entry *parent
920923
ZVAL_INDIRECT(dst, src);
921924
}
922925
if (Z_TYPE_P(Z_INDIRECT_P(dst)) == IS_CONSTANT_AST) {
926+
ce->ce_flags |= ZEND_HAS_CONSTANTS;
923927
ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
924928
}
925929
} while (dst != end);
@@ -1030,6 +1034,7 @@ static void do_inherit_iface_constant(zend_string *name, zend_class_constant *c,
10301034
if (do_inherit_constant_check(&ce->constants_table, c, name, iface)) {
10311035
zend_class_constant *ct;
10321036
if (Z_TYPE(c->value) == IS_CONSTANT_AST) {
1037+
ce->ce_flags |= ZEND_HAS_CONSTANTS;
10331038
ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
10341039
}
10351040
if (ce->type & ZEND_INTERNAL_CLASS) {

Zend/zend_opcode.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,12 +212,16 @@ ZEND_API void destroy_zend_class(zval *zv)
212212
if (ce->ce_flags & ZEND_ACC_IMMUTABLE) {
213213
zend_op_array *op_array;
214214

215-
zend_cleanup_internal_class_data(ce);
216-
ZEND_HASH_FOREACH_PTR(&ce->function_table, op_array) {
217-
if (op_array->type == ZEND_USER_FUNCTION) {
218-
destroy_op_array(op_array);
219-
}
220-
} ZEND_HASH_FOREACH_END();
215+
if (ce->default_static_members_count) {
216+
zend_cleanup_internal_class_data(ce);
217+
}
218+
if (ce->ce_flags & ZEND_HAS_STATIC_IN_METHODS) {
219+
ZEND_HASH_FOREACH_PTR(&ce->function_table, op_array) {
220+
if (op_array->type == ZEND_USER_FUNCTION) {
221+
destroy_op_array(op_array);
222+
}
223+
} ZEND_HASH_FOREACH_END();
224+
}
221225
return;
222226
} else if (--ce->refcount > 0) {
223227
return;

0 commit comments

Comments
 (0)
0