Skip to content

Commit 271c496

Browse files
ondrejmirtesclaude
andcommitted
Answer hasExpressionType() without printing a key no tracked expression can match
ScopeOps::hasExpressionType() printed the expression's key before looking it up, and for a node that had never been printed that means a trip through the PHP pretty printer - ~60 % of the printer misses of an analysis, and most of those lookups answer No. A printed key determines the kind of node it was printed from: PHP syntax its node class, and every virtual node its own __phpstan marker (a short list prints like an array, so the two share a kind). The native hasExpressionType() now looks at the kinds of the tracked expressions first, and when none shares the node's, it answers No without printing. The PHP twin keeps printing; the answers are the same, only the printer's cache attribute of such a node is left unset. Exact since the previous commit stopped virtual nodes from inheriting another node's key. src/Analyser src/Rules src/Type single process, 8 interleaved pairs: 20.43 s -> 20.21 s user CPU (-1.08 %, 7/8), output identical, walk-trace identical. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QSGnoYacTfJv3taAhZkj5o
1 parent e89e9c8 commit 271c496

3 files changed

Lines changed: 88 additions & 2 deletions

File tree

‎turbo-ext/src/ScopeOps.cpp‎

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,47 @@ class ScopeOps
208208
if (name.isString()) return hasVariableType(scope, name.asString());
209209
}
210210

211-
zv::Str key = zv::Str::adopt(pt_node_key(node, exprPrinter));
212-
if (UNEXPECTED(key.isNull())) return zv::Val();
213211
zval *table = scopeArrayProp(scope, "expressionTypes", sizeof("expressionTypes") - 1);
214212
if (UNEXPECTED(table == NULL)) return zv::Val();
213+
214+
/* A key the node has never been printed for costs a trip through the
215+
* PHP pretty printer, and most such asks answer No. When the table
216+
* holds no expression of the node's print kind, no key in it can equal
217+
* the node's - a printed key determines its kind: PHP syntax its node
218+
* class (a short list, printed like an array, shares the array's
219+
* kind, see pt_node_print_kind()) and every virtual node its own
220+
* __phpstan marker - so the answer is No without printing. The twin
221+
* prints here; only the printer's cache attribute is left unset. */
222+
pt_init_strs();
223+
zval *cachedKey = pt_node_attribute(node, pt_str_cache_printer);
224+
if (cachedKey == NULL || Z_TYPE_P(cachedKey) != IS_STRING) {
225+
uint32_t kind = pt_node_print_kind(node);
226+
if (UNEXPECTED(kind == 0)) return zv::Val();
227+
bool present = false;
228+
for (auto entry : zv::ArrRef(table)) {
229+
zval *holder = entry.value().deref().raw();
230+
if (UNEXPECTED(Z_TYPE_P(holder) != IS_OBJECT)) {
231+
present = true;
232+
break;
233+
}
234+
zval *expr = OBJ_PROP_NUM(Z_OBJ_P(holder), PT_ETH_PROP_EXPR);
235+
ZVAL_DEREF(expr);
236+
if (UNEXPECTED(Z_TYPE_P(expr) != IS_OBJECT)) {
237+
present = true;
238+
break;
239+
}
240+
uint32_t entryKind = pt_node_print_kind(Z_OBJ_P(expr));
241+
if (UNEXPECTED(entryKind == 0)) return zv::Val();
242+
if (entryKind == kind) {
243+
present = true;
244+
break;
245+
}
246+
}
247+
if (!present) return trinarySingleton(PT_TRI_NO);
248+
}
249+
250+
zv::Str key = zv::Str::adopt(pt_node_key(node, exprPrinter));
251+
if (UNEXPECTED(key.isNull())) return zv::Val();
215252
zval *found = zend_symtable_find(Z_ARRVAL_P(table), key.get());
216253
if (found == NULL) return trinarySingleton(PT_TRI_NO);
217254
zv::Ref holder = zv::Ref(found).deref();

‎turbo-ext/src/support.cpp‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,7 @@ static bool pt_strs_inited = false;
506506

507507
static HashTable pt_node_class_cache;
508508
static bool pt_node_class_cache_inited = false;
509+
static HashTable pt_print_kind_ids; /* getType() string -> IS_LONG id, lives with pt_node_class_cache */
509510

510511
void pt_init_strs()
511512
{
@@ -564,6 +565,7 @@ void pt_support_rshutdown()
564565
}
565566
if (pt_node_class_cache_inited) {
566567
zend_hash_destroy(&pt_node_class_cache);
568+
zend_hash_destroy(&pt_print_kind_ids);
567569
pt_node_class_cache_inited = false;
568570
}
569571
pt_native_visitor_index_reset();
@@ -713,6 +715,7 @@ pt_node_class_info *pt_get_node_class_info(zend_class_entry *ce)
713715

714716
if (!pt_node_class_cache_inited) {
715717
zend_hash_init(&pt_node_class_cache, 64, NULL, pt_node_class_info_free, 0);
718+
zend_hash_init(&pt_print_kind_ids, 64, NULL, NULL, 0);
716719
pt_node_class_cache_inited = true;
717720
}
718721

@@ -734,6 +737,46 @@ pt_node_class_info *pt_get_node_class_info(zend_class_entry *ce)
734737
return info;
735738
}
736739

740+
uint32_t pt_node_print_kind(zend_object *node)
741+
{
742+
pt_node_class_info *info = pt_get_node_class_info(node->ce);
743+
if (UNEXPECTED(info == NULL)) return 0;
744+
if (EXPECTED(info->print_kind != 0)) return info->print_kind;
745+
746+
zend_function *fn = (zend_function *) zend_hash_str_find_ptr(&node->ce->function_table, "gettype", sizeof("gettype") - 1);
747+
if (UNEXPECTED(fn == NULL)) {
748+
zend_throw_error(NULL, "phpstan_turbo: %s has no getType()", ZSTR_VAL(node->ce->name));
749+
return 0;
750+
}
751+
zval type;
752+
zend_call_known_function(fn, node, node->ce, &type, 0, NULL, NULL);
753+
if (UNEXPECTED(EG(exception))) {
754+
zval_ptr_dtor(&type);
755+
return 0;
756+
}
757+
if (UNEXPECTED(Z_TYPE(type) != IS_STRING)) {
758+
zval_ptr_dtor(&type);
759+
zend_throw_error(NULL, "phpstan_turbo: %s::getType() did not return a string", ZSTR_VAL(node->ce->name));
760+
return 0;
761+
}
762+
/* the printer prints a short list exactly like an array */
763+
zend_string *kindName = zend_string_equals_literal(Z_STR(type), "Expr_List") ? ZSTR_INIT_LITERAL("Expr_Array", 0) : zend_string_copy(Z_STR(type));
764+
zval_ptr_dtor(&type);
765+
zval *known = zend_hash_find(&pt_print_kind_ids, kindName);
766+
uint32_t id;
767+
if (known != NULL) {
768+
id = (uint32_t) Z_LVAL_P(known);
769+
} else {
770+
id = zend_hash_num_elements(&pt_print_kind_ids) + 1;
771+
zval idValue;
772+
ZVAL_LONG(&idValue, (zend_long) id);
773+
zend_hash_add_new(&pt_print_kind_ids, kindName, &idValue);
774+
}
775+
zend_string_release(kindName);
776+
info->print_kind = id;
777+
return id;
778+
}
779+
737780
zv::Val pt_attributes_without_expression_key(zval *attributes)
738781
{
739782
zv::Val copy = zv::Val::copyOf(zv::Ref(attributes).deref());

‎turbo-ext/src/support.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,9 @@ typedef struct _pt_node_class_info {
871871
int32_t attributes_offset;
872872
int32_t name_offset;
873873
bool is_variable;
874+
/* the printer's dispatch kind (getType(), a short List_ counted as an
875+
* Array_ since both print alike), as a small id; 0 = not resolved yet */
876+
uint32_t print_kind;
874877
} pt_node_class_info;
875878

876879
#define PT_HAS_SUBNODES(info) ((info)->subnode_offsets != NULL && (info)->subnode_count != UINT32_MAX)
@@ -891,6 +894,9 @@ extern zend_string *pt_str_start_file_pos;
891894
void pt_init_strs();
892895

893896
zval *pt_node_attribute(zend_object *node, zend_string *name);
897+
/* pt_node_class_info::print_kind of the node's class, resolved on first use;
898+
* 0 with an exception pending */
899+
uint32_t pt_node_print_kind(zend_object *node);
894900
bool pt_node_set_attribute(zend_object *node, zend_string *name, zval *value);
895901

896902
/* the shadowing ExprPrinter (ExprPrinter.cpp) — the miss half of

0 commit comments

Comments
 (0)