Skip to content

Commit

Permalink
Make sure STATIC_SOURCE_FILE PropListItem nodes are not duplicated un…
Browse files Browse the repository at this point in the history
…necessarily.

This patch reduces the total number of STATIC_SOURCE_FILE nodes by 10x in a typical project.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=164177760
  • Loading branch information
rluble authored and dimvar committed Aug 3, 2017
1 parent 51fe34e commit 6c6d864
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/CheckSideEffects.java
Expand Up @@ -193,7 +193,7 @@ private void addExtern() {
var.setJSDocInfo(builder.build());
CompilerInput input = compiler.getSynthesizedExternsInput();
name.setStaticSourceFile(input.getSourceFile());
var.setStaticSourceFile(input.getSourceFile());
var.setStaticSourceFileFrom(name);
input.getAstRoot(compiler).addChildToBack(var);
compiler.reportChangeToEnclosingScope(var);
}
Expand Down
41 changes: 30 additions & 11 deletions src/com/google/javascript/rhino/Node.java
Expand Up @@ -1266,6 +1266,25 @@ private static void toStringTreeHelper(Node n, int level, Appendable sb)
//==========================================================================
// Source position management

public void setStaticSourceFileFrom(Node other) {
// Make sure source file prop nodes are not duplicated.
if (other.propListHead != null
&& (this.propListHead == null
|| (this.propListHead.propType == STATIC_SOURCE_FILE
&& this.propListHead.next == null))) {
// Either the node has only STATIC_SOURCE_FILE as a property or has not properties.
PropListItem tail = other.propListHead;
while (tail.next != null) {
tail = tail.next;
}
if (tail.propType == STATIC_SOURCE_FILE) {
propListHead = tail;
return;
}
}
setStaticSourceFile(other.getStaticSourceFile());
}

public void setStaticSourceFile(@Nullable StaticSourceFile file) {
this.putProp(STATIC_SOURCE_FILE, file);
}
Expand Down Expand Up @@ -2225,15 +2244,15 @@ public Node cloneTree(boolean cloneTypeExprs) {
// TODO(nicksantos): The semantics of this method are ill-defined. Delete it.
@Deprecated
public Node useSourceInfoWithoutLengthIfMissingFrom(Node other) {
if (getProp(ORIGINALNAME_PROP) == null) {
putProp(ORIGINALNAME_PROP, other.getProp(ORIGINALNAME_PROP));
}

if (getStaticSourceFile() == null) {
setStaticSourceFile(other.getStaticSourceFile());
setStaticSourceFileFrom(other);
sourcePosition = other.sourcePosition;
}

if (getProp(ORIGINALNAME_PROP) == null) {
putProp(ORIGINALNAME_PROP, other.getProp(ORIGINALNAME_PROP));
}

return this;
}

Expand All @@ -2258,8 +2277,8 @@ public Node useSourceInfoWithoutLengthIfMissingFromForTree(Node other) {
* that of {@code other}.
*/
public Node useSourceInfoFrom(Node other) {
setStaticSourceFileFrom(other);
putProp(ORIGINALNAME_PROP, other.getProp(ORIGINALNAME_PROP));
setStaticSourceFile(other.getStaticSourceFile());
sourcePosition = other.sourcePosition;
length = other.length;
return this;
Expand Down Expand Up @@ -2291,16 +2310,16 @@ public Node srcrefTree(Node other) {
* that of {@code other} iff the source info is missing.
*/
public Node useSourceInfoIfMissingFrom(Node other) {
if (getProp(ORIGINALNAME_PROP) == null) {
putProp(ORIGINALNAME_PROP, other.getProp(ORIGINALNAME_PROP));
}

if (getStaticSourceFile() == null) {
setStaticSourceFile(other.getStaticSourceFile());
setStaticSourceFileFrom(other);
sourcePosition = other.sourcePosition;
length = other.length;
}

if (getProp(ORIGINALNAME_PROP) == null) {
putProp(ORIGINALNAME_PROP, other.getProp(ORIGINALNAME_PROP));
}

return this;
}

Expand Down

0 comments on commit 6c6d864

Please sign in to comment.