From 16ce0848fec706dca4db6eb4613020d2fd075975 Mon Sep 17 00:00:00 2001 From: miliu Date: Sun, 29 Sep 2019 13:58:30 -0700 Subject: [PATCH] Revert "fix cache issue" This reverts commit 5d4c1e2b3b65a01ae1e215445e716bf4f7e4b95a. --- .../src/antlr4/PredictionContext.js | 32 +++++++++--------- runtime/JavaScript/src/antlr4/Utils.js | 33 +++++++++---------- .../JavaScript/src/antlr4/atn/ATNSimulator.js | 2 +- .../src/antlr4/atn/PredictionMode.js | 2 +- 4 files changed, 34 insertions(+), 35 deletions(-) diff --git a/runtime/JavaScript/src/antlr4/PredictionContext.js b/runtime/JavaScript/src/antlr4/PredictionContext.js index e6de9caef..8b2e2ece0 100644 --- a/runtime/JavaScript/src/antlr4/PredictionContext.js +++ b/runtime/JavaScript/src/antlr4/PredictionContext.js @@ -79,7 +79,7 @@ function calculateHashString(parent, returnState) { // can be used for both lexers and parsers. function PredictionContextCache() { - this.cache = new Map(); + this.cache = {}; return this; } @@ -91,16 +91,16 @@ PredictionContextCache.prototype.add = function(ctx) { if (ctx === PredictionContext.EMPTY) { return PredictionContext.EMPTY; } - var existing = this.cache.get(ctx) || null; + var existing = this.cache[ctx] || null; if (existing !== null) { return existing; } - this.cache.set(ctx, ctx); + this.cache[ctx] = ctx; return ctx; }; PredictionContextCache.prototype.get = function(ctx) { - return this.cache.get(ctx) || null; + return this.cache[ctx] || null; }; Object.defineProperty(PredictionContextCache.prototype, "length", { @@ -642,16 +642,16 @@ function mergeArrays(a, b, rootIsWildcard, mergeCache) { // ones. // / function combineCommonParents(parents) { - var uniqueParents = new Map(); + var uniqueParents = {}; for (var p = 0; p < parents.length; p++) { var parent = parents[p]; - if (!uniqueParents.get(parent)) { - uniqueParents.set(parent, parent); + if (!(parent in uniqueParents)) { + uniqueParents[parent] = parent; } } for (var q = 0; q < parents.length; q++) { - parents[q] = uniqueParents.get(parents[q]); + parents[q] = uniqueParents[parents[q]]; } } @@ -659,13 +659,13 @@ function getCachedPredictionContext(context, contextCache, visited) { if (context.isEmpty()) { return context; } - var existing = visited.get(context) || null; + var existing = visited[context] || null; if (existing !== null) { return existing; } existing = contextCache.get(context); if (existing !== null) { - visited.set(context, existing); + visited[context] = existing; return existing; } var changed = false; @@ -685,7 +685,7 @@ function getCachedPredictionContext(context, contextCache, visited) { } if (!changed) { contextCache.add(context); - visited.set(context, context); + visited[context] = context; return context; } var updated = null; @@ -698,8 +698,8 @@ function getCachedPredictionContext(context, contextCache, visited) { updated = new ArrayPredictionContext(parents, context.returnStates); } contextCache.add(updated); - visited.set(updated, updated); - visited.set(context, updated); + visited[updated] = updated; + visited[context] = updated; return updated; } @@ -710,13 +710,13 @@ function getAllContextNodes(context, nodes, visited) { nodes = []; return getAllContextNodes(context, nodes, visited); } else if (visited === null) { - visited = new Map(); + visited = {}; return getAllContextNodes(context, nodes, visited); } else { - if (context === null || visited.get(context) !== null) { + if (context === null || visited[context] !== null) { return nodes; } - visited.set(context, context); + visited[context] = context; nodes.push(context); for (var i = 0; i < context.length; i++) { getAllContextNodes(context.getParent(i), nodes, visited); diff --git a/runtime/JavaScript/src/antlr4/Utils.js b/runtime/JavaScript/src/antlr4/Utils.js index 8449b776f..9dad93c18 100644 --- a/runtime/JavaScript/src/antlr4/Utils.js +++ b/runtime/JavaScript/src/antlr4/Utils.js @@ -197,14 +197,14 @@ BitSet.prototype.toString = function () { return "{" + this.values().join(", ") + "}"; }; -function Dictionary(hashFunction, equalsFunction) { +function Map(hashFunction, equalsFunction) { this.data = {}; this.hashFunction = hashFunction || standardHashCodeFunction; this.equalsFunction = equalsFunction || standardEqualsFunction; return this; } -Object.defineProperty(Dictionary.prototype, "length", { +Object.defineProperty(Map.prototype, "length", { get: function () { var l = 0; for (var hashKey in this.data) { @@ -216,7 +216,7 @@ Object.defineProperty(Dictionary.prototype, "length", { } }); -Dictionary.prototype.put = function (key, value) { +Map.prototype.put = function (key, value) { var hashKey = "hash_" + this.hashFunction(key); if (hashKey in this.data) { var entries = this.data[hashKey]; @@ -236,7 +236,7 @@ Dictionary.prototype.put = function (key, value) { } }; -Dictionary.prototype.containsKey = function (key) { +Map.prototype.containsKey = function (key) { var hashKey = "hash_" + this.hashFunction(key); if(hashKey in this.data) { var entries = this.data[hashKey]; @@ -249,7 +249,7 @@ Dictionary.prototype.containsKey = function (key) { return false; }; -Dictionary.prototype.get = function (key) { +Map.prototype.get = function (key) { var hashKey = "hash_" + this.hashFunction(key); if(hashKey in this.data) { var entries = this.data[hashKey]; @@ -262,7 +262,7 @@ Dictionary.prototype.get = function (key) { return null; }; -Dictionary.prototype.entries = function () { +Map.prototype.entries = function () { var l = []; for (var key in this.data) { if (key.indexOf("hash_") === 0) { @@ -273,21 +273,21 @@ Dictionary.prototype.entries = function () { }; -Dictionary.prototype.getKeys = function () { +Map.prototype.getKeys = function () { return this.entries().map(function(e) { return e.key; }); }; -Dictionary.prototype.getValues = function () { +Map.prototype.getValues = function () { return this.entries().map(function(e) { return e.value; }); }; -Dictionary.prototype.toString = function () { +Map.prototype.toString = function () { var ss = this.entries().map(function(entry) { return '{' + entry.key + ':' + entry.value + '}'; }); @@ -324,7 +324,6 @@ AltDict.prototype.values = function () { }; function DoubleDict() { - this['doubleDictMap'] = this['doubleDictMap'] || new Map(); return this; } @@ -390,17 +389,17 @@ function hashStuff() { } DoubleDict.prototype.get = function (a, b) { - var d = this['doubleDictMap'].get(a) || null; - return d === null ? null : (d.get(b) || null); + var d = this[a] || null; + return d === null ? null : (d[b] || null); }; DoubleDict.prototype.set = function (a, b, o) { - var d = this['doubleDictMap'].get(a) || null; + var d = this[a] || null; if (d === null) { - d = new Map(); - this['doubleDictMap'].set(a, d); + d = {}; + this[a] = d; } - d.set(b, o); + d[b] = o; }; @@ -439,7 +438,7 @@ function equalArrays(a, b) exports.Hash = Hash; exports.Set = Set; -exports.Dictionary = Dictionary; +exports.Map = Map; exports.BitSet = BitSet; exports.AltDict = AltDict; exports.DoubleDict = DoubleDict; diff --git a/runtime/JavaScript/src/antlr4/atn/ATNSimulator.js b/runtime/JavaScript/src/antlr4/atn/ATNSimulator.js index 510357918..ea87dabbf 100644 --- a/runtime/JavaScript/src/antlr4/atn/ATNSimulator.js +++ b/runtime/JavaScript/src/antlr4/atn/ATNSimulator.js @@ -44,7 +44,7 @@ ATNSimulator.prototype.getCachedContext = function(context) { if (this.sharedContextCache ===null) { return context; } - var visited = new Map(); + var visited = {}; return getCachedPredictionContext(context, this.sharedContextCache, visited); }; diff --git a/runtime/JavaScript/src/antlr4/atn/PredictionMode.js b/runtime/JavaScript/src/antlr4/atn/PredictionMode.js index 67ed8c448..217a533d5 100644 --- a/runtime/JavaScript/src/antlr4/atn/PredictionMode.js +++ b/runtime/JavaScript/src/antlr4/atn/PredictionMode.js @@ -10,7 +10,7 @@ // ambiguities. var Set = require('./../Utils').Set; -var Map = require('./../Utils').Dictionary; +var Map = require('./../Utils').Map; var BitSet = require('./../Utils').BitSet; var AltDict = require('./../Utils').AltDict; var ATN = require('./ATN').ATN;