Skip to content

Commit

Permalink
[dart2js] Make _ConstantNodeIndexerVisitor DAG-aware
Browse files Browse the repository at this point in the history
Change-Id: Ie0dc25ef90805fd05e65c738bb010361e7ec9590
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/110186
Reviewed-by: Mark Zhou <[email protected]>
Commit-Queue: Stephen Adams <[email protected]>
  • Loading branch information
rakudrama authored and [email protected] committed Jul 24, 2019
1 parent 6ccfb4b commit d767363
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions pkg/compiler/lib/src/serialization/node_indexer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,16 @@ class _ConstantNodeIndexerVisitor implements ir.ConstantVisitor<void> {
int _currentIndex = 0;
final Map<int, ir.Constant> _indexToNodeMap = {};
final Map<ir.Constant, int> _nodeToIndexMap = {};
final Set<ir.Constant> _visitedNonindexedNodes = {};

void registerConstant(ir.Constant node) {
/// Returns `true` if node not already registered.
bool _register(ir.Constant node) {
int index = _nodeToIndexMap[node];
if (index != null) return false;
_indexToNodeMap[_currentIndex] = node;
_nodeToIndexMap[node] = _currentIndex;
_currentIndex++;
return true;
}

int getIndex(ir.Constant node) {
Expand Down Expand Up @@ -244,33 +249,38 @@ class _ConstantNodeIndexerVisitor implements ir.ConstantVisitor<void> {

@override
void visitInstanceConstant(ir.InstanceConstant node) {
node.fieldValues.forEach((_, ir.Constant value) {
value.accept(this);
});
if (_visitedNonindexedNodes.add(node)) {
node.fieldValues.forEach((_, ir.Constant value) {
value.accept(this);
});
}
}

@override
void visitSetConstant(ir.SetConstant node) {
registerConstant(node);
for (ir.Constant element in node.entries) {
element.accept(this);
if (_register(node)) {
for (ir.Constant element in node.entries) {
element.accept(this);
}
}
}

@override
void visitListConstant(ir.ListConstant node) {
registerConstant(node);
for (ir.Constant element in node.entries) {
element.accept(this);
if (_register(node)) {
for (ir.Constant element in node.entries) {
element.accept(this);
}
}
}

@override
void visitMapConstant(ir.MapConstant node) {
registerConstant(node);
for (ir.ConstantMapEntry entry in node.entries) {
entry.key.accept(this);
entry.value.accept(this);
if (_register(node)) {
for (ir.ConstantMapEntry entry in node.entries) {
entry.key.accept(this);
entry.value.accept(this);
}
}
}

Expand Down

0 comments on commit d767363

Please sign in to comment.