From 073845093b925760caddb462d3affc1ccd0bae42 Mon Sep 17 00:00:00 2001 From: Travis Fisher Date: Tue, 9 Apr 2019 23:09:42 -0400 Subject: [PATCH] store symbol full names instead of constructing on demand. (~5% overall compiler speed improvement) --- .../dk/camelot64/kickc/model/symbols/Label.java | 11 ++++++++++- .../dk/camelot64/kickc/model/symbols/Scope.java | 17 ++++++++--------- .../kickc/model/symbols/SymbolVariable.java | 13 ++++++++++++- 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/main/java/dk/camelot64/kickc/model/symbols/Label.java b/src/main/java/dk/camelot64/kickc/model/symbols/Label.java index 888c4df72..7a8034cda 100644 --- a/src/main/java/dk/camelot64/kickc/model/symbols/Label.java +++ b/src/main/java/dk/camelot64/kickc/model/symbols/Label.java @@ -13,12 +13,21 @@ public class Label implements Symbol { /** The containing scope */ private Scope scope; + /** Full name of label (scope::name or name) */ + private String fullName; + private boolean intermediate; public Label(String name, Scope scope, boolean intermediate) { this.name = name; this.scope = scope; this.intermediate = intermediate; + setFullName(); + } + + private void setFullName() { + String scopeName = (scope == null) ? "" : scope.getFullName(); + fullName = (scopeName.length() > 0) ? scopeName + "::" + name : name; } @Override @@ -47,7 +56,7 @@ public class Label implements Symbol { @Override public String getFullName() { - return Scope.getFullName(this); + return fullName; } public boolean isIntermediate() { diff --git a/src/main/java/dk/camelot64/kickc/model/symbols/Scope.java b/src/main/java/dk/camelot64/kickc/model/symbols/Scope.java index a1ec4f9f9..3070e188f 100644 --- a/src/main/java/dk/camelot64/kickc/model/symbols/Scope.java +++ b/src/main/java/dk/camelot64/kickc/model/symbols/Scope.java @@ -20,27 +20,25 @@ public abstract class Scope implements Symbol { private int intermediateLabelCount = 1; private int blockCount = 1; private Scope parentScope; + private String fullName; public Scope(String name, Scope parentScope) { this.name = name; this.parentScope = parentScope; this.symbols = new LinkedHashMap<>(); + setFullName(); } public Scope() { this.name = ""; this.parentScope = null; this.symbols = new LinkedHashMap<>(); + setFullName(); } - public static String getFullName(Symbol symbol) { - if(symbol.getScope() != null) { - String scopeName = symbol.getScope().getFullName(); - if(scopeName.length() > 0) { - return scopeName + "::" + symbol.getLocalName(); - } - } - return symbol.getLocalName(); + private void setFullName() { + String scopeName = (parentScope == null) ? "" : parentScope.getFullName(); + fullName = (scopeName.length() > 0) ? scopeName + "::" + name : name; } public HashMap getSymbols() { @@ -54,7 +52,7 @@ public abstract class Scope implements Symbol { @Override public String getFullName() { - return getFullName(this); + return fullName; } public ScopeRef getRef() { @@ -69,6 +67,7 @@ public abstract class Scope implements Symbol { @Override public void setScope(Scope scope) { this.parentScope = scope; + setFullName(); } @Override diff --git a/src/main/java/dk/camelot64/kickc/model/symbols/SymbolVariable.java b/src/main/java/dk/camelot64/kickc/model/symbols/SymbolVariable.java index 8cc8f2d68..3714877c1 100644 --- a/src/main/java/dk/camelot64/kickc/model/symbols/SymbolVariable.java +++ b/src/main/java/dk/camelot64/kickc/model/symbols/SymbolVariable.java @@ -41,12 +41,21 @@ public abstract class SymbolVariable implements Symbol { /** Comments preceding the procedure in the source code. */ private List comments; + /** Full name of variable (scope::name or name) */ + private String fullName; + public SymbolVariable(String name, Scope scope, SymbolType type) { this.name = name; this.scope = scope; this.type = type; this.inferredType = false; this.comments = new ArrayList<>(); + setFullName(); + } + + private void setFullName() { + String scopeName = (scope == null) ? "" : scope.getFullName(); + fullName = (scopeName.length() > 0) ? scopeName + "::" + name : name; } @Override @@ -56,7 +65,7 @@ public abstract class SymbolVariable implements Symbol { @Override public String getFullName() { - return Scope.getFullName(this); + return this.fullName; } public SymbolType getType() { @@ -86,6 +95,7 @@ public abstract class SymbolVariable implements Symbol { public void setName(String name) { this.name = name; + setFullName(); } public String getAsmName() { @@ -102,6 +112,7 @@ public abstract class SymbolVariable implements Symbol { public void setScope(Scope scope) { this.scope = scope; + setFullName(); } @Override