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

Implement frameless static calls #16471

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

iluuu1994
Copy link
Member

@iluuu1994 iluuu1994 commented Oct 16, 2024

The very synthetic _ZendTestClass::framelessStaticMethod() shows a significant improvement, iterated 100 000 000 times.

Before:
Benchmark 1: sapi/cli/php test.php
  Time (mean ± σ):      1.221 s ±  0.026 s    [User: 1.215 s, System: 0.002 s]
  Range (min … max):    1.188 s …  1.252 s    10 runs
 
After:
Benchmark 1: sapi/cli/php test.php
  Time (mean ± σ):     878.3 ms ±  15.9 ms    [User: 874.4 ms, System: 1.1 ms]
  Range (min … max):   852.7 ms … 900.3 ms    10 runs

Obviously, this difference will be more noticeable if the implementation is simple, as in this case. The difference for useful methods will be smaller.

/* Update opline in case it got invalidated. */
zend_op_array *op_array = CG(active_op_array);
opline = &op_array->opcodes[init_opnum];
literal_dtor(&ZEND_OP1_LITERAL(opline));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I actually forgot that these operands may contain multiple literals... Optimally we could attempt to compile frameless calls before emitting any opcodes, but the loading of fbc currently uses lcname emitted by zend_set_class_name_op1, a bit annoying.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. It would be better to check for frame-less variant before emitting INIT_STATIC_METHOD_CALL
This may be done with the alternative following patch.
The only drawback is that zend_string_tolower() called twice times more.

diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c
index 7e5351ddfaf..c479d97b27a 100644
--- a/Zend/zend_compile.c
+++ b/Zend/zend_compile.c
@@ -5329,6 +5329,47 @@ static void zend_compile_static_call(znode *result, zend_ast *ast, uint32_t type
 		}
 	}
 
+	/* Check if we already know which method we're calling */
+	if (method_node.op_type == IS_CONST) {
+		zend_class_entry *ce = NULL;
+		if (class_node.op_type == IS_CONST) {
+			zend_string *lcname = zend_string_tolower(Z_STR(class_node.u.constant));
+			ce = zend_hash_find_ptr(CG(class_table), lcname);
+			if (ce) {
+				if (zend_compile_ignore_class(ce, CG(active_op_array)->filename)) {
+					ce = NULL;
+				}
+			} else if (CG(active_class_entry)
+					&& zend_string_equals_ci(CG(active_class_entry)->name, lcname)) {
+				ce = CG(active_class_entry);
+			}
+			zend_string_release(lcname);
+		} else if (class_node.op_type == IS_UNUSED
+				&& (class_node.u.op.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_SELF
+				&& zend_is_scope_known()) {
+			ce = CG(active_class_entry);
+		}
+		if (ce) {
+			zend_string *lcname = zend_string_tolower(Z_STR(method_node.u.constant));
+			fbc = zend_get_compatible_func_or_null(ce, lcname);
+			zend_string_release(lcname);
+
+			if (fbc
+			 && !(CG(compiler_options) & ZEND_COMPILE_NO_BUILTINS)
+			 && (fbc->type == ZEND_INTERNAL_FUNCTION)
+			 && zend_ast_is_list(args_ast)
+			 && !zend_args_contain_unpack_or_named(zend_ast_get_list(args_ast))) {
+				if (zend_compile_frameless_icall(result, zend_ast_get_list(args_ast), fbc, type) != (uint32_t)-1) {
+					zval_ptr_dtor(&method_node.u.constant);
+					if (class_node.op_type == IS_CONST) {
+						zval_ptr_dtor(&class_node.u.constant);
+					}
+					return;
+				}
+			}
+		}
+	}
+
 	opline = get_next_op();
 	opline->opcode = ZEND_INIT_STATIC_METHOD_CALL;
 
@@ -5346,31 +5387,6 @@ static void zend_compile_static_call(znode *result, zend_ast *ast, uint32_t type
 		SET_NODE(opline->op2, &method_node);
 	}
 
-	/* Check if we already know which method we're calling */
-	if (opline->op2_type == IS_CONST) {
-		zend_class_entry *ce = NULL;
-		if (opline->op1_type == IS_CONST) {
-			zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op1) + 1);
-			ce = zend_hash_find_ptr(CG(class_table), lcname);
-			if (ce) {
-				if (zend_compile_ignore_class(ce, CG(active_op_array)->filename)) {
-					ce = NULL;
-				}
-			} else if (CG(active_class_entry)
-					&& zend_string_equals_ci(CG(active_class_entry)->name, lcname)) {
-				ce = CG(active_class_entry);
-			}
-		} else if (opline->op1_type == IS_UNUSED
-				&& (opline->op1.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_SELF
-				&& zend_is_scope_known()) {
-			ce = CG(active_class_entry);
-		}
-		if (ce) {
-			zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op2) + 1);
-			fbc = zend_get_compatible_func_or_null(ce, lcname);
-		}
-	}
-
 	zend_compile_call_common(result, args_ast, fbc, zend_ast_get_lineno(method_ast));
 }
 /* }}} */

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! The code looks good to me and I pushed it. It makes reviewing slightly harder.

@iluuu1994 iluuu1994 force-pushed the frameless-static-calls branch from afa4e02 to 640c7e0 Compare October 16, 2024 23:17
@iluuu1994
Copy link
Member Author

@dstogov Have you ever had time to check whether this makes a meaningful difference for your FFI case? In the synthetic benchmark, it shows a 29% improvement compared to normal static method calls.

@dstogov
Copy link
Member

dstogov commented Nov 25, 2024

Sorry, I missed this. I'll take a careful look and do benchmarks later this week.

Copy link
Member

@dstogov dstogov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the implementation.

Think about the alternative zend_compile.c patch.
I'm not sure what version is better.
May be lc_name(s) may be cached to avoid the double work or may be it's better to keep the sources clean (speed of compiler is not very important).

/* Update opline in case it got invalidated. */
zend_op_array *op_array = CG(active_op_array);
opline = &op_array->opcodes[init_opnum];
literal_dtor(&ZEND_OP1_LITERAL(opline));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. It would be better to check for frame-less variant before emitting INIT_STATIC_METHOD_CALL
This may be done with the alternative following patch.
The only drawback is that zend_string_tolower() called twice times more.

diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c
index 7e5351ddfaf..c479d97b27a 100644
--- a/Zend/zend_compile.c
+++ b/Zend/zend_compile.c
@@ -5329,6 +5329,47 @@ static void zend_compile_static_call(znode *result, zend_ast *ast, uint32_t type
 		}
 	}
 
+	/* Check if we already know which method we're calling */
+	if (method_node.op_type == IS_CONST) {
+		zend_class_entry *ce = NULL;
+		if (class_node.op_type == IS_CONST) {
+			zend_string *lcname = zend_string_tolower(Z_STR(class_node.u.constant));
+			ce = zend_hash_find_ptr(CG(class_table), lcname);
+			if (ce) {
+				if (zend_compile_ignore_class(ce, CG(active_op_array)->filename)) {
+					ce = NULL;
+				}
+			} else if (CG(active_class_entry)
+					&& zend_string_equals_ci(CG(active_class_entry)->name, lcname)) {
+				ce = CG(active_class_entry);
+			}
+			zend_string_release(lcname);
+		} else if (class_node.op_type == IS_UNUSED
+				&& (class_node.u.op.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_SELF
+				&& zend_is_scope_known()) {
+			ce = CG(active_class_entry);
+		}
+		if (ce) {
+			zend_string *lcname = zend_string_tolower(Z_STR(method_node.u.constant));
+			fbc = zend_get_compatible_func_or_null(ce, lcname);
+			zend_string_release(lcname);
+
+			if (fbc
+			 && !(CG(compiler_options) & ZEND_COMPILE_NO_BUILTINS)
+			 && (fbc->type == ZEND_INTERNAL_FUNCTION)
+			 && zend_ast_is_list(args_ast)
+			 && !zend_args_contain_unpack_or_named(zend_ast_get_list(args_ast))) {
+				if (zend_compile_frameless_icall(result, zend_ast_get_list(args_ast), fbc, type) != (uint32_t)-1) {
+					zval_ptr_dtor(&method_node.u.constant);
+					if (class_node.op_type == IS_CONST) {
+						zval_ptr_dtor(&class_node.u.constant);
+					}
+					return;
+				}
+			}
+		}
+	}
+
 	opline = get_next_op();
 	opline->opcode = ZEND_INIT_STATIC_METHOD_CALL;
 
@@ -5346,31 +5387,6 @@ static void zend_compile_static_call(znode *result, zend_ast *ast, uint32_t type
 		SET_NODE(opline->op2, &method_node);
 	}
 
-	/* Check if we already know which method we're calling */
-	if (opline->op2_type == IS_CONST) {
-		zend_class_entry *ce = NULL;
-		if (opline->op1_type == IS_CONST) {
-			zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op1) + 1);
-			ce = zend_hash_find_ptr(CG(class_table), lcname);
-			if (ce) {
-				if (zend_compile_ignore_class(ce, CG(active_op_array)->filename)) {
-					ce = NULL;
-				}
-			} else if (CG(active_class_entry)
-					&& zend_string_equals_ci(CG(active_class_entry)->name, lcname)) {
-				ce = CG(active_class_entry);
-			}
-		} else if (opline->op1_type == IS_UNUSED
-				&& (opline->op1.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_SELF
-				&& zend_is_scope_known()) {
-			ce = CG(active_class_entry);
-		}
-		if (ce) {
-			zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op2) + 1);
-			fbc = zend_get_compatible_func_or_null(ce, lcname);
-		}
-	}
-
 	zend_compile_call_common(result, args_ast, fbc, zend_ast_get_lineno(method_ast));
 }
 /* }}} */

@iluuu1994 iluuu1994 force-pushed the frameless-static-calls branch from 640c7e0 to 466e7a4 Compare November 27, 2024 15:20
@iluuu1994
Copy link
Member Author

There is an issue on ARM (or possibly the configuration we're using for that build). I'll have a look at that.

@iluuu1994 iluuu1994 force-pushed the frameless-static-calls branch from 466e7a4 to ba9092a Compare March 26, 2025 22:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants