Skip to content

Commit e8d7b79

Browse files
ondrejmirtesclaude
andcommitted
Keep a type's structural hash and memo keys in a header in front of the object
TypeCombinatorCache kept both per-object facts it needs in weak maps: the 128-bit structural hash of every hashed type (pt_type_hashes) and the list of memo keys a result object answers (pt_memo_results). Every entry is an EG(weakrefs) registration on the object, and every such object pays the engine's weak-reference notification when it dies. The shadowing classes the memo hashes structurally (PHPStan\Type\*, PHPStan\Php\*, TrinaryLogic) now get a create_object at activation that allocates a 32-byte header in front of the zend_object: the hash once computed, the memo key list and the memo generation the list belongs to. PHP subclasses inherit the allocator. The header's free_obj tombstones the object's memo slots, its clone_obj gives the clone a fresh header, and clear() bumps the generation so that lists from before a clear are ignored. Objects without the header keep using the weak maps. src/Analyser src/Rules src/Type single process, 8 interleaved pairs: 20.78 s -> 20.40 s user CPU (-1.82 %, 8/8, t = -14.7), peak RSS unchanged, output identical. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QSGnoYacTfJv3taAhZkj5o
1 parent 7714ff4 commit e8d7b79

3 files changed

Lines changed: 166 additions & 8 deletions

File tree

‎turbo-ext/src/Shadow.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ static bool pt_shadow_materialize(reg::ShadowPlan &plan, HashTable *twinFiles, z
187187
zend_string_release(lcName);
188188

189189
plan.ce = linked;
190+
pt_type_combinator_cache_adopt_class(linked, plan.name);
190191
if (plan.out != NULL) {
191192
*plan.out = linked;
192193
}

‎turbo-ext/src/TypeCombinatorCache.cpp‎

Lines changed: 164 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ namespace sigs = ptdecl::TypeCombinatorCache::sig;
7878
#include "zv.h"
7979

8080
#include <Zend/zend_weakrefs.h>
81+
#if PHP_VERSION_ID >= 80400
82+
#include <Zend/zend_lazy_objects.h>
83+
#endif
8184

8285
/* zend_weakrefs_hash_clean()/_destroy() only exist since PHP 8.5; on 8.4 the
8386
* same unregister-then-destroy is spelled out with the 8.4-available API. */
@@ -172,6 +175,37 @@ static uint64_t pt_next_serial = 1;
172175
static bool pt_cache_inited = false;
173176
static bool pt_invalidate_active = false;
174177

178+
/* The objects of the structurally hashed classes carry this header in front
179+
* of their zend_object (the classes get typeObjectCreate() as their
180+
* create_object when they are declared, see
181+
* pt_type_combinator_cache_adopt_class()): the object's structural hash once
182+
* computed, and the list of memo keys mapping to it as a result. For those
183+
* objects it replaces the weak maps pt_type_hashes and pt_memo_results — no
184+
* EG(weakrefs) registration and no weak-reference notification when the
185+
* object dies, and the lookup is a load instead of a hash probe. An object
186+
* without the header (a class with a create_object of its own) keeps using
187+
* the weak maps. memoGeneration ties the key list to the memo it was built
188+
* against: clear() bumps pt_memo_generation, and a list from an older
189+
* generation names slots that no longer exist. */
190+
struct TypeObjectHeader
191+
{
192+
Hash128 hash;
193+
KeyList *memoKeys;
194+
uint32_t hashComputed;
195+
uint32_t memoGeneration;
196+
zend_object std;
197+
};
198+
199+
static zend_object_handlers pt_type_object_handlers;
200+
static bool pt_type_object_handlers_inited = false;
201+
static uint32_t pt_memo_generation = 1;
202+
203+
static zend_always_inline TypeObjectHeader *typeObjectHeader(zend_object *obj)
204+
{
205+
if (obj->handlers != &pt_type_object_handlers) return NULL;
206+
return (TypeObjectHeader *) ((char *) obj - offsetof(TypeObjectHeader, std));
207+
}
208+
175209

176210

177211
/* {{{ 128-bit FNV-1a, two independent accumulators fed by one walk */
@@ -371,10 +405,18 @@ static bool hashObject(zend_object *obj, Hash128 &out, uint32_t depth)
371405
* instance to save nothing. */
372406
if (hashZeroSlotObject(obj, out)) return true;
373407

374-
Hash128 *cached = (Hash128 *) zend_hash_index_find_ptr(&pt_type_hashes, zend_object_to_weakref_key(obj));
375-
if (cached != NULL) {
376-
out = *cached;
377-
return true;
408+
TypeObjectHeader *header = typeObjectHeader(obj);
409+
if (header != NULL) {
410+
if (header->hashComputed) {
411+
out = header->hash;
412+
return true;
413+
}
414+
} else {
415+
Hash128 *cached = (Hash128 *) zend_hash_index_find_ptr(&pt_type_hashes, zend_object_to_weakref_key(obj));
416+
if (cached != NULL) {
417+
out = *cached;
418+
return true;
419+
}
378420
}
379421

380422
CePlan plan = cePlan(obj->ce);
@@ -392,10 +434,15 @@ static bool hashObject(zend_object *obj, Hash128 &out, uint32_t depth)
392434
* as a hole) and u2 is Z_NEXT, the collision chain, overwritten on insert.
393435
* NULL return = already registered by a re-entrant walk; the existing entry
394436
* holds the same bytes (the hash is a pure function of the object's value). */
395-
Hash128 *stored = (Hash128 *) emalloc(sizeof(Hash128));
396-
*stored = h;
397-
if (zend_weakrefs_hash_add_ptr(&pt_type_hashes, obj, stored) == NULL) {
398-
efree(stored);
437+
if (header != NULL) {
438+
header->hash = h;
439+
header->hashComputed = 1;
440+
} else {
441+
Hash128 *stored = (Hash128 *) emalloc(sizeof(Hash128));
442+
*stored = h;
443+
if (zend_weakrefs_hash_add_ptr(&pt_type_hashes, obj, stored) == NULL) {
444+
efree(stored);
445+
}
399446
}
400447

401448
out = h;
@@ -623,6 +670,28 @@ static void memoResultDtor(zval *zv)
623670

624671
static void memoTrackResult(zend_object *obj, Hash128 key)
625672
{
673+
TypeObjectHeader *header = typeObjectHeader(obj);
674+
if (header != NULL) {
675+
KeyList *list = header->memoKeys;
676+
if (list == NULL) {
677+
list = (KeyList *) emalloc(sizeof(KeyList));
678+
list->obj = obj;
679+
list->count = 0;
680+
list->cap = 4;
681+
header->memoKeys = list;
682+
} else if (header->memoGeneration != pt_memo_generation) {
683+
list->count = 0;
684+
}
685+
header->memoGeneration = pt_memo_generation;
686+
if (list->count == list->cap) {
687+
list->cap *= 2;
688+
list = (KeyList *) erealloc(list, sizeof(KeyList) + (list->cap - 4) * sizeof(Hash128));
689+
header->memoKeys = list;
690+
}
691+
list->keys[list->count++] = key;
692+
return;
693+
}
694+
626695
zval *existing = zend_hash_index_find(&pt_memo_results, zend_object_to_weakref_key(obj));
627696
if (existing != NULL) {
628697
KeyList *list = (KeyList *) Z_PTR_P(existing);
@@ -666,6 +735,76 @@ static void memoResultsClean()
666735

667736
/* }}} */
668737

738+
/* {{{ the header-carrying objects */
739+
740+
static zend_object *typeObjectCreate(zend_class_entry *ce)
741+
{
742+
TypeObjectHeader *header = (TypeObjectHeader *) zend_object_alloc(sizeof(TypeObjectHeader), ce);
743+
header->hashComputed = 0;
744+
header->memoKeys = NULL;
745+
header->memoGeneration = 0;
746+
zend_object_std_init(&header->std, ce);
747+
object_properties_init(&header->std, ce);
748+
header->std.handlers = &pt_type_object_handlers;
749+
750+
return &header->std;
751+
}
752+
753+
static void typeObjectFree(zend_object *obj)
754+
{
755+
TypeObjectHeader *header = (TypeObjectHeader *) ((char *) obj - offsetof(TypeObjectHeader, std));
756+
if (header->memoKeys != NULL) {
757+
if (pt_invalidate_active && header->memoGeneration == pt_memo_generation) {
758+
memoInvalidate(header->memoKeys);
759+
}
760+
efree(header->memoKeys);
761+
header->memoKeys = NULL;
762+
}
763+
zend_object_std_dtor(obj);
764+
}
765+
766+
/* zend_objects_clone_obj() with the header: the clone is a new object, its
767+
* hash is computed afresh and it is nobody's memo result. A lazy object is
768+
* cloned by the engine's own handler (zend_lazy_object_clone() is not
769+
* exported); that clone has no header and keeps to the weak maps. */
770+
static zend_object *typeObjectClone(zend_object *old)
771+
{
772+
#if PHP_VERSION_ID >= 80400
773+
if (UNEXPECTED(zend_object_is_lazy(old))) {
774+
return zend_objects_clone_obj(old);
775+
}
776+
#endif
777+
TypeObjectHeader *header = (TypeObjectHeader *) zend_object_alloc(sizeof(TypeObjectHeader), old->ce);
778+
header->hashComputed = 0;
779+
header->memoKeys = NULL;
780+
header->memoGeneration = 0;
781+
zend_object_std_init(&header->std, old->ce);
782+
header->std.handlers = &pt_type_object_handlers;
783+
if (old->ce->default_properties_count) {
784+
zval *p = header->std.properties_table;
785+
zval *end = p + old->ce->default_properties_count;
786+
do {
787+
ZVAL_UNDEF(p);
788+
p++;
789+
} while (p != end);
790+
}
791+
zend_objects_clone_members(&header->std, old);
792+
793+
return &header->std;
794+
}
795+
796+
static void typeObjectHandlersInit()
797+
{
798+
if (pt_type_object_handlers_inited) return;
799+
memcpy(&pt_type_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
800+
pt_type_object_handlers.offset = offsetof(TypeObjectHeader, std);
801+
pt_type_object_handlers.free_obj = typeObjectFree;
802+
pt_type_object_handlers.clone_obj = typeObjectClone;
803+
pt_type_object_handlers_inited = true;
804+
}
805+
806+
/* }}} */
807+
669808
/* Mirrors PHPStan\Type\TypeCombinatorCache. */
670809
class TypeCombinatorCache
671810
{
@@ -793,6 +932,7 @@ class TypeCombinatorCache
793932
{
794933
if (!pt_cache_inited) return;
795934
memoResultsClean();
935+
pt_memo_generation++;
796936
if (pt_memo_mask + 1 > MEMO_INITIAL_CAPACITY_LIMIT) {
797937
efree(pt_memo_slots);
798938
pt_memo_slots = (MemoSlot *) ecalloc(MEMO_INITIAL_CAPACITY_LIMIT, sizeof(MemoSlot));
@@ -881,6 +1021,22 @@ zv::Val pt_type_combinator_cache_remove(zval *fromType, zval *typeToRemove)
8811021
return TypeCombinatorCache::run(TypeCombinatorCache::REMOVE, TypeCombinatorCache::computeRemove, args, 2);
8821022
}
8831023

1024+
/* Gives the objects of a structurally hashed shadowing class the header (see
1025+
* TypeObjectHeader). Called for every class right after activation declares
1026+
* it; PHP subclasses inherit create_object from it. A class with an allocator
1027+
* of its own is left alone and keeps the weak maps. */
1028+
void pt_type_combinator_cache_adopt_class(zend_class_entry *ce, const char *realName)
1029+
{
1030+
if (ce->create_object != NULL) return;
1031+
size_t len = strlen(realName);
1032+
bool structural = (len > sizeof("PHPStan\\Type\\") - 1 && memcmp(realName, "PHPStan\\Type\\", sizeof("PHPStan\\Type\\") - 1) == 0)
1033+
|| (len > sizeof("PHPStan\\Php\\") - 1 && memcmp(realName, "PHPStan\\Php\\", sizeof("PHPStan\\Php\\") - 1) == 0)
1034+
|| strcmp(realName, "PHPStan\\TrinaryLogic") == 0;
1035+
if (!structural) return;
1036+
phpstanturbo::typeObjectHandlersInit();
1037+
ce->create_object = phpstanturbo::typeObjectCreate;
1038+
}
1039+
8841040
void pt_type_combinator_cache_clear()
8851041
{
8861042
TypeCombinatorCache::clear();

‎turbo-ext/src/support.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,7 @@ zv::Val pt_type_combinator_cache_union(uint32_t argc, zval *argv);
12861286
zv::Val pt_type_combinator_cache_intersect(uint32_t argc, zval *argv);
12871287
zv::Val pt_type_combinator_cache_remove(zval *fromType, zval *typeToRemove);
12881288
void pt_type_combinator_cache_clear();
1289+
void pt_type_combinator_cache_adopt_class(zend_class_entry *ce, const char *realName);
12891290

12901291
/* merged from the parallel port branch */
12911292
/* merged from the parallel port branch */

0 commit comments

Comments
 (0)