Skip to content

Commit 50d7b35

Browse files
committed
Answer overwrite markers against the live set instead of per write
Each write that replaces a variable leaves a marker the previous replacing write of the same slot observes and kills. compileOverwrites() materialized, for every write, the list of every other write's marker it observes and kills, so a variable assigned W times cost O(W^2) in memory and time, and the backward walk then unset all W markers at each write. Only the markers are compiled now, by variable and slot. A write intersects the live set with the markers of its slots when it is reached, walking the smaller of the two, so the live sets, and with them what is reported, stay the same. Amp-Thread-ID: https://ampcode.com/threads/T-01a1b134-fc8f-7115-81cd-f19bb1fdbf46
1 parent 2e71852 commit 50d7b35

3 files changed

Lines changed: 186 additions & 116 deletions

File tree

‎src/Analyser/VariableLivenessResolver.php‎

Lines changed: 86 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ final class VariableLivenessResolver
8686
/** @var array<int, string> */
8787
private array $overwriteMarkers = [];
8888

89-
/** @var array<int, array<string, true>> */
90-
private array $overwriteKeys = [];
89+
/** @var array<string, array<string, array<string, int>>> variable name => replaced slot => marker => id of the write leaving it */
90+
private array $markersBySlot = [];
9191

9292
/** @var array<int, true> */
9393
private array $overwrittenIds = [];
@@ -258,15 +258,16 @@ private function liveBefore(?VariableFlow $flow, array $next, VariableFlowContex
258258
$id = $flow->write->getId();
259259
if ($flow->kind !== VariableFlow::DISCARD) {
260260
$this->observeWrite($id, $next);
261-
$this->observeOverwrite($id, $next);
261+
$this->observeOverwrite($flow->write, $next);
262262
foreach ($this->literalItems[$id] ?? [] as $item) {
263263
$this->observeWrite($item->getId(), $next);
264-
$this->observeOverwrite($item->getId(), $next);
264+
$this->observeOverwrite($item, $next);
265265
}
266266
}
267267
foreach (array_keys($this->killedKeys[$id] ?? []) as $key) {
268268
unset($next[$key]);
269269
}
270+
$next = $this->killOverwriteMarkers($flow->write, $next);
270271
if (isset($this->overwriteMarkers[$id])) {
271272
$next[$this->overwriteMarkers[$id]] = true;
272273
}
@@ -569,6 +570,10 @@ private function compileAccesses(): void
569570
* write finding another write's marker after it reaches that replacement
570571
* on some path, with no replacement in between.
571572
*
573+
* Only the markers are compiled, by slot: which of them a write observes
574+
* or kills is answered against the live set when the write is reached, so
575+
* the work stays linear in the variable's writes.
576+
*
572577
* @param list<VariableAccessFlow> $accesses
573578
*/
574579
private function compileOverwrites(string $name, array $accesses): void
@@ -590,38 +595,8 @@ private function compileOverwrites(string $name, array $accesses): void
590595
if ($markersBySlot === []) {
591596
return;
592597
}
593-
foreach ($accesses as $access) {
594-
$write = $access->write;
595-
if ($write === null) {
596-
continue;
597-
}
598-
$id = $write->getId();
599-
$offset = $write->getOffset();
600-
$observed = $markersBySlot[''] ?? [];
601-
if ($write->isOffsetWrite() && $offset !== null) {
602-
$observed += $markersBySlot[self::offsetKey($offset)] ?? [];
603-
}
604-
foreach ($observed as $marker => $writerId) {
605-
// a loop running the same write again replaces nothing it
606-
// wrote - nor the items of the array literal it assigns
607-
if ($writerId === $id || $writerId === $write->getParentId()) {
608-
continue;
609-
}
610-
$this->overwriteKeys[$id][$marker] = true;
611-
}
612-
$slot = self::replacedSlot($write);
613-
if ($slot === null) {
614-
continue;
615-
}
616-
foreach ($markersBySlot as $markerSlot => $markers) {
617-
if ($slot !== '' && $markerSlot !== $slot) {
618-
continue;
619-
}
620-
foreach (array_keys($markers) as $marker) {
621-
$this->killedKeys[$id][$marker] = true;
622-
}
623-
}
624-
}
598+
599+
$this->markersBySlot[$name] = $markersBySlot;
625600
}
626601

627602
/** The slot a write replaces: '' for the whole variable, null when it keeps the old value. */
@@ -655,15 +630,85 @@ private function observeWrite(int $id, array $next): void
655630
}
656631

657632
/** @param array<string, true> $next */
658-
private function observeOverwrite(int $id, array $next): void
633+
private function observeOverwrite(VariableWrite $write, array $next): void
659634
{
660-
foreach (array_keys($this->overwriteKeys[$id] ?? []) as $key) {
661-
if (!isset($next[$key])) {
635+
$markersBySlot = $this->markersBySlot[$write->getVariableName()] ?? null;
636+
if ($markersBySlot === null) {
637+
return;
638+
}
639+
$id = $write->getId();
640+
$parentId = $write->getParentId();
641+
$observedSlots = [''];
642+
$offset = $write->getOffset();
643+
if ($write->isOffsetWrite() && $offset !== null) {
644+
$observedSlots[] = self::offsetKey($offset);
645+
}
646+
foreach ($observedSlots as $slot) {
647+
$markers = $markersBySlot[$slot] ?? [];
648+
// walk the smaller of the two sets
649+
if (count($markers) <= count($next)) {
650+
foreach ($markers as $marker => $writerId) {
651+
if (!isset($next[$marker])) {
652+
continue;
653+
}
654+
// a loop running the same write again replaces nothing it
655+
// wrote - nor the items of the array literal it assigns
656+
if ($writerId === $id || $writerId === $parentId) {
657+
continue;
658+
}
659+
$this->overwrittenIds[$id] = true;
660+
return;
661+
}
662662
continue;
663663
}
664-
$this->overwrittenIds[$id] = true;
665-
return;
664+
foreach (array_keys($next) as $key) {
665+
$writerId = $markers[$key] ?? null;
666+
if ($writerId === null || $writerId === $id || $writerId === $parentId) {
667+
continue;
668+
}
669+
$this->overwrittenIds[$id] = true;
670+
return;
671+
}
672+
}
673+
}
674+
675+
/**
676+
* Drops the markers of the slots $write replaces - all of the variable's
677+
* when it replaces the whole variable.
678+
*
679+
* @param array<string, true> $next
680+
* @return array<string, true>
681+
*/
682+
private function killOverwriteMarkers(VariableWrite $write, array $next): array
683+
{
684+
$markersBySlot = $this->markersBySlot[$write->getVariableName()] ?? null;
685+
if ($markersBySlot === null) {
686+
return $next;
687+
}
688+
$slot = self::replacedSlot($write);
689+
if ($slot === null) {
690+
return $next;
691+
}
692+
foreach ($markersBySlot as $markerSlot => $markers) {
693+
if ($slot !== '' && $markerSlot !== $slot) {
694+
continue;
695+
}
696+
// walk the smaller of the two sets
697+
if (count($markers) <= count($next)) {
698+
foreach (array_keys($markers) as $marker) {
699+
unset($next[$marker]);
700+
}
701+
continue;
702+
}
703+
foreach (array_keys($next) as $key) {
704+
if (!isset($markers[$key])) {
705+
continue;
706+
}
707+
unset($next[$key]);
708+
}
666709
}
710+
711+
return $next;
667712
}
668713

669714
private function resolveDependencies(): void

‎turbo-ext/src/VariableLivenessResolver.cpp‎

Lines changed: 88 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)