@@ -548,7 +548,7 @@ class VariableLivenessResolver
548548 zv::Val literalItems = emptyArray();
549549 zv::Val coveredIds = emptyArray();
550550 zv::Val overwriteMarkers = emptyArray();
551- zv::Val overwriteKeys = emptyArray();
551+ zv::Val markersBySlot = emptyArray(); /* name => slot => marker => writer id */
552552 zv::Val overwrittenIds = emptyArray();
553553 zv::Val allReadKeys = emptyArray();
554554 zv::Val loopStatements = emptyArray();
@@ -816,14 +816,14 @@ class VariableLivenessResolver
816816 zend_long id = view.id ;
817817 if (kind != PT_VLR_DISCARD ) {
818818 observeWrite (id, next);
819- observeOverwrite (id , next);
819+ observeOverwrite (view , next);
820820 HashTable *items = innerTable (literalItems, id);
821821 if (items != NULL ) {
822822 for (auto entry : zv::TableRef (items)) {
823- zend_long itemId ;
824- if (UNEXPECTED (!writeId (entry.value ().deref ().raw (), itemId ))) return zv::Val ();
825- observeWrite (itemId , next);
826- observeOverwrite (itemId , next);
823+ WriteView item ;
824+ if (UNEXPECTED (!item. load (entry.value ().deref ().raw ()))) return zv::Val ();
825+ observeWrite (item. id , next);
826+ observeOverwrite (item , next);
827827 }
828828 }
829829 }
@@ -836,6 +836,7 @@ class VariableLivenessResolver
836836 }
837837 }
838838 }
839+ killOverwriteMarkers (view, next);
839840 zval *marker = zend_hash_index_find (tableOf (overwriteMarkers), (zend_ulong) id);
840841 if (marker != NULL ) {
841842 setTrue (next, Z_STR_P (marker));
@@ -1415,7 +1416,7 @@ class VariableLivenessResolver
14151416 /* Mirrors compileOverwrites(); false = pending exception. */
14161417 [[nodiscard]] bool compileOverwrites (zend_string *name, HashTable *accessTable)
14171418 {
1418- zv::Val markersBySlot = emptyArray ();
1419+ zv::Val slotMarkers = emptyArray ();
14191420 for (auto entry : zv::TableRef (accessTable)) {
14201421 zend_object *access = Z_OBJ_P (entry.value ().deref ().raw ());
14211422 zval *write = slot (access, slots.accessWrite , " write" );
@@ -1438,68 +1439,20 @@ class VariableLivenessResolver
14381439 smart_str_append_long (&key, view.id );
14391440 smart_str_0 (&key);
14401441 zv::Str marker = zv::Str::adopt (key.s );
1441- zval *bySlot = innerSlotByKey (markersBySlot , slotName.get ());
1442+ zval *bySlot = innerSlotByKey (slotMarkers , slotName.get ());
14421443 zval idValue;
14431444 ZVAL_LONG (&idValue, view.id );
14441445 zend_hash_update (Z_ARRVAL_P (bySlot), marker.get (), &idValue);
14451446 zval markerValue;
14461447 ZVAL_STR (&markerValue, marker.get ());
14471448 setIndex (overwriteMarkers, view.id , &markerValue);
14481449 }
1449- if (countOf (markersBySlot) == 0 ) return true ;
1450- for (auto entry : zv::TableRef (accessTable)) {
1451- zend_object *access = Z_OBJ_P (entry.value ().deref ().raw ());
1452- zval *write = slot (access, slots.accessWrite , " write" );
1453- if (UNEXPECTED (write == NULL )) return false ;
1454- if (Z_TYPE_P (write) != IS_OBJECT ) continue ;
1455- WriteView view;
1456- if (UNEXPECTED (!view.load (write))) return false ;
1457- zend_long id = view.id ;
1458- observeOverwrites (view, innerTableByKey (markersBySlot, ZSTR_EMPTY_ALLOC ()));
1459- if (view.offsetWrite && !view.offsetIsNull ) {
1460- zv::Str offsetSlot = offsetKey (view.offset .raw ());
1461- observeOverwrites (view, innerTableByKey (markersBySlot, offsetSlot.get ()));
1462- }
1463- zv::Str slotName;
1464- if (!replacedSlot (view, slotName)) continue ;
1465- bool whole = ZSTR_LEN (slotName.get ()) == 0 ;
1466- for (auto slotEntry : zv::TableRef (tableOf (markersBySlot))) {
1467- zend_string *markerSlot = slotEntry.stringKeyOrNull ();
1468- if (markerSlot == NULL ) continue ;
1469- if (!whole && !zend_string_equals (markerSlot, slotName.get ())) continue ;
1470- zv::Ref markers = slotEntry.value ().deref ();
1471- if (!markers.isArray ()) continue ;
1472- for (auto markerEntry : zv::TableRef (markers.asArrayTable ())) {
1473- zend_string *marker = markerEntry.stringKeyOrNull ();
1474- if (marker == NULL ) continue ;
1475- zval *killed = innerSlot (killedKeys, id);
1476- zval trueValue;
1477- ZVAL_TRUE (&trueValue);
1478- zend_hash_update (Z_ARRVAL_P (killed), marker, &trueValue);
1479- }
1480- }
1481- }
1482- return true ;
1483- }
1450+ if (countOf (slotMarkers) == 0 ) return true ;
14841451
1485- /* The markers of $markers written by another write than $write and the
1486- * array literal it is an item of. */
1487- void observeOverwrites (const WriteView &write, HashTable *markers)
1488- {
1489- if (markers == NULL ) return ;
1490- zend_long id = write.id ;
1491- for (auto entry : zv::TableRef (markers)) {
1492- zend_string *marker = entry.stringKeyOrNull ();
1493- if (marker == NULL ) continue ;
1494- // a loop running the same write again replaces nothing it
1495- // wrote - nor the items of the array literal it assigns
1496- zend_long writerId = entry.value ().deref ().toLong ();
1497- if (writerId == id || (!write.parentIdIsNull && writerId == write.parentId )) continue ;
1498- zval *observed = innerSlot (overwriteKeys, id);
1499- zval trueValue;
1500- ZVAL_TRUE (&trueValue);
1501- zend_hash_update (Z_ARRVAL_P (observed), marker, &trueValue);
1502- }
1452+ zval *stored = innerSlotByKey (markersBySlot, name);
1453+ zval_ptr_dtor (stored);
1454+ ZVAL_COPY (stored, slotMarkers.raw ());
1455+ return true ;
15031456 }
15041457
15051458 /* Mirrors replacedSlot(); false for null. */
@@ -1515,16 +1468,83 @@ class VariableLivenessResolver
15151468 }
15161469
15171470 /* Mirrors observeOverwrite(). */
1518- void observeOverwrite (zend_long id , const zv::Val &next)
1471+ void observeOverwrite (WriteView &write , const zv::Val &next)
15191472 {
1520- HashTable *overwrites = innerTable (overwriteKeys, id);
1521- if (overwrites == NULL ) return ;
1522- for (auto entry : zv::TableRef (overwrites)) {
1523- zend_string *key = entry.stringKeyOrNull ();
1524- if (key == NULL || !isTrueAt (next, key)) continue ;
1525- setTrueIndex (overwrittenIds, id);
1473+ HashTable *bySlot = innerTableByKey (markersBySlot, write.nameStr );
1474+ if (bySlot == NULL ) return ;
1475+ if (observesOverwriteMarker (write, zend_hash_find (bySlot, ZSTR_EMPTY_ALLOC ()), next)) {
1476+ setTrueIndex (overwrittenIds, write.id );
15261477 return ;
15271478 }
1479+ if (write.offsetWrite && !write.offsetIsNull ) {
1480+ zv::Str offsetSlot = offsetKey (write.offset .raw ());
1481+ if (observesOverwriteMarker (write, zend_hash_find (bySlot, offsetSlot.get ()), next)) {
1482+ setTrueIndex (overwrittenIds, write.id );
1483+ }
1484+ }
1485+ }
1486+
1487+ /* whether $next holds one of $markers left by another write than $write
1488+ * and the array literal it is an item of - walking the smaller of the two */
1489+ static bool observesOverwriteMarker (const WriteView &write, zval *markersValue, const zv::Val &next)
1490+ {
1491+ if (markersValue == NULL ) return false ;
1492+ ZVAL_DEREF (markersValue);
1493+ if (Z_TYPE_P (markersValue) != IS_ARRAY ) return false ;
1494+ HashTable *markers = Z_ARRVAL_P (markersValue);
1495+ bool walkMarkers = zend_hash_num_elements (markers) <= countOf (next);
1496+ for (auto entry : zv::TableRef (walkMarkers ? markers : tableOf (next))) {
1497+ zend_string *key = entry.stringKeyOrNull ();
1498+ if (key == NULL ) continue ;
1499+ zval *writer;
1500+ if (walkMarkers) {
1501+ if (!isTrueAt (next, key)) continue ;
1502+ writer = entry.value ().deref ().raw ();
1503+ } else {
1504+ writer = zend_hash_find (markers, key);
1505+ if (writer == NULL ) continue ;
1506+ ZVAL_DEREF (writer);
1507+ }
1508+ // a loop running the same write again replaces nothing it
1509+ // wrote - nor the items of the array literal it assigns
1510+ zend_long writerId = zval_get_long (writer);
1511+ if (writerId == write.id || (!write.parentIdIsNull && writerId == write.parentId )) continue ;
1512+ return true ;
1513+ }
1514+ return false ;
1515+ }
1516+
1517+ /* Mirrors killOverwriteMarkers(). */
1518+ void killOverwriteMarkers (WriteView &write, zv::Val &next)
1519+ {
1520+ HashTable *bySlot = innerTableByKey (markersBySlot, write.nameStr );
1521+ if (bySlot == NULL ) return ;
1522+ zv::Str slotName;
1523+ if (!replacedSlot (write, slotName)) return ;
1524+ bool whole = ZSTR_LEN (slotName.get ()) == 0 ;
1525+ for (auto slotEntry : zv::TableRef (bySlot)) {
1526+ zend_string *markerSlot = slotEntry.stringKeyOrNull ();
1527+ if (markerSlot == NULL ) continue ;
1528+ if (!whole && !zend_string_equals (markerSlot, slotName.get ())) continue ;
1529+ zv::Ref markersRef = slotEntry.value ().deref ();
1530+ if (!markersRef.isArray ()) continue ;
1531+ HashTable *markers = markersRef.asArrayTable ();
1532+ if (zend_hash_num_elements (markers) <= countOf (next)) {
1533+ for (auto markerEntry : zv::TableRef (markers)) {
1534+ zend_string *marker = markerEntry.stringKeyOrNull ();
1535+ if (marker == NULL ) continue ;
1536+ unsetKey (next, marker);
1537+ }
1538+ continue ;
1539+ }
1540+ /* a snapshot to walk while the live set loses keys */
1541+ zv::Val live = shareArray (next);
1542+ for (auto entry : zv::TableRef (tableOf (live))) {
1543+ zend_string *key = entry.stringKeyOrNull ();
1544+ if (key == NULL || !zend_hash_exists (markers, key)) continue ;
1545+ unsetKey (next, key);
1546+ }
1547+ }
15281548 }
15291549
15301550 /* Mirrors observeWrite(). */
0 commit comments