Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use arrayview for bit set #4549

Merged
merged 8 commits into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
110 changes: 110 additions & 0 deletions runtime/JavaScript/spec/BitSetSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import BitSet from "../src/antlr4/misc/BitSet.js";

describe('test BitSet', () => {

it("is empty", () => {
const bs = new BitSet();
expect(bs.length).toEqual(0);
})

it("sets 1 value", () => {
const bs = new BitSet();
bs.set(67);
expect(bs.length).toEqual(1);
expect(bs.get(67)).toBeTrue();
})

it("clears 1 value", () => {
const bs = new BitSet();
bs.set(67);
bs.clear(67)
expect(bs.length).toEqual(0);
expect(bs.get(67)).toBeFalse();
})

it("sets 2 consecutive values", () => {
const bs = new BitSet();
bs.set(67);
bs.set(68);
expect(bs.length).toEqual(2);
expect(bs.get(67)).toBeTrue();
expect(bs.get(68)).toBeTrue();
})

it("sets 2 close values", () => {
const bs = new BitSet();
bs.set(67);
bs.set(70);
expect(bs.length).toEqual(2);
expect(bs.get(67)).toBeTrue();
expect(bs.get(70)).toBeTrue();
})

it("sets 2 distant values", () => {
const bs = new BitSet();
bs.set(67);
bs.set(241);
expect(bs.length).toEqual(2);
expect(bs.get(67)).toBeTrue();
expect(bs.get(241)).toBeTrue();
})

it("combines 2 identical sets", () => {
const bs1 = new BitSet();
bs1.set(67);
const bs2 = new BitSet();
bs2.set(67);
bs1.or(bs2);
expect(bs1.length).toEqual(1);
expect(bs1.get(67)).toBeTrue();
})

it("combines 2 distinct sets", () => {
const bs1 = new BitSet();
bs1.set(67);
const bs2 = new BitSet();
bs2.set(69);
bs1.or(bs2);
expect(bs1.length).toEqual(2);
expect(bs1.get(67)).toBeTrue();
expect(bs1.get(69)).toBeTrue();
})

it("combines 2 overlapping sets", () => {
const bs1 = new BitSet();
bs1.set(67);
bs1.set(69);
const bs2 = new BitSet();
bs2.set(69);
bs2.set(71);
bs1.or(bs2);
expect(bs1.length).toEqual(3);
expect(bs1.get(67)).toBeTrue();
expect(bs1.get(69)).toBeTrue();
expect(bs1.get(71)).toBeTrue();
})

it("returns values", () => {
const bs = new BitSet();
bs.set(67);
bs.set(69);
const values = bs.values();
expect(values).toEqual([67, 69]);
})

it("counts bits", () => {
for(let i= 0; i <= 0xFF; i++) {
// count bits the slow but easy to understand way (Kernighan method)
let count1 = 0;
let value = i;
while(value) {
if(value & 1)
count1++;
value >>= 1;
}
// count bits the fast way
const count2 = BitSet._bitCount(i);
expect(count2).toEqual(count1);
}
})
})
12 changes: 6 additions & 6 deletions runtime/JavaScript/src/antlr4/atn/LL1Analyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,17 @@ export default class LL1Analyzer {
return;
}
if (ctx !== PredictionContext.EMPTY) {
const removed = calledRuleStack.has(s.ruleIndex);
const removed = calledRuleStack.get(s.ruleIndex);
try {
calledRuleStack.remove(s.ruleIndex);
calledRuleStack.clear(s.ruleIndex);
// run thru all possible stack tops in ctx
for (let i = 0; i < ctx.length; i++) {
const returnState = this.atn.states[ctx.getReturnState(i)];
this._LOOK(returnState, stopState, ctx.getParent(i), look, lookBusy, calledRuleStack, seeThruPreds, addEOF);
}
}finally {
if (removed) {
calledRuleStack.add(s.ruleIndex);
calledRuleStack.set(s.ruleIndex);
}
}
return;
Expand All @@ -153,15 +153,15 @@ export default class LL1Analyzer {
for(let j=0; j<s.transitions.length; j++) {
const t = s.transitions[j];
if (t.constructor === RuleTransition) {
if (calledRuleStack.has(t.target.ruleIndex)) {
if (calledRuleStack.get(t.target.ruleIndex)) {
continue;
}
const newContext = SingletonPredictionContext.create(ctx, t.followState.stateNumber);
try {
calledRuleStack.add(t.target.ruleIndex);
calledRuleStack.set(t.target.ruleIndex);
this._LOOK(t.target, stopState, newContext, look, lookBusy, calledRuleStack, seeThruPreds, addEOF);
} finally {
calledRuleStack.remove(t.target.ruleIndex);
calledRuleStack.clear(t.target.ruleIndex);
}
} else if (t instanceof AbstractPredicateTransition ) {
if (seeThruPreds) {
Expand Down
10 changes: 5 additions & 5 deletions runtime/JavaScript/src/antlr4/atn/ParserATNSimulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -1010,7 +1010,7 @@ export default class ParserATNSimulator extends ATNSimulator {
let altToPred = [];
for(let i=0;i<configs.items.length;i++) {
const c = configs.items[i];
if(ambigAlts.has( c.alt )) {
if(ambigAlts.get( c.alt )) {
altToPred[c.alt] = SemanticContext.orContext(altToPred[c.alt] || null, c.semanticContext);
}
}
Expand Down Expand Up @@ -1039,7 +1039,7 @@ export default class ParserATNSimulator extends ATNSimulator {
for (let i=1; i<altToPred.length;i++) {
const pred = altToPred[i];
// unpredicated is indicated by SemanticContext.NONE
if( ambigAlts!==null && ambigAlts.has( i )) {
if( ambigAlts!==null && ambigAlts.get( i )) {
pairs.push(new PredPrediction(pred, i));
}
if (pred !== SemanticContext.NONE) {
Expand Down Expand Up @@ -1173,7 +1173,7 @@ export default class ParserATNSimulator extends ATNSimulator {
for(let i=0;i<predPredictions.length;i++) {
const pair = predPredictions[i];
if (pair.pred === SemanticContext.NONE) {
predictions.add(pair.alt);
predictions.set(pair.alt);
if (! complete) {
break;
}
Expand All @@ -1187,7 +1187,7 @@ export default class ParserATNSimulator extends ATNSimulator {
if (this.debug || this.dfa_debug) {
console.log("PREDICT " + pair.alt);
}
predictions.add(pair.alt);
predictions.set(pair.alt);
if (! complete) {
break;
}
Expand Down Expand Up @@ -1544,7 +1544,7 @@ export default class ParserATNSimulator extends ATNSimulator {
let conflictingAlts = null;
if (configs.uniqueAlt!== ATN.INVALID_ALT_NUMBER) {
conflictingAlts = new BitSet();
conflictingAlts.add(configs.uniqueAlt);
conflictingAlts.set(configs.uniqueAlt);
} else {
conflictingAlts = configs.conflictingAlts;
}
Expand Down
4 changes: 2 additions & 2 deletions runtime/JavaScript/src/antlr4/atn/PredictionMode.js
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ const PredictionMode = {
alts = new BitSet();
configToAlts.set(cfg, alts);
}
alts.add(cfg.alt);
alts.set(cfg.alt);
});
return configToAlts.getValues();
},
Expand All @@ -532,7 +532,7 @@ const PredictionMode = {
alts = new BitSet();
m.set(c.state, alts);
}
alts.add(c.alt);
alts.set(c.alt);
});
return m;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export default class DiagnosticErrorListener extends ErrorListener {
}
const result = new BitSet()
for (let i = 0; i < configs.items.length; i++) {
result.add(configs.items[i].alt);
result.set(configs.items[i].alt);
}
return `{${result.values().join(", ")}}`;
}
Expand Down
96 changes: 83 additions & 13 deletions runtime/JavaScript/src/antlr4/misc/BitSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,74 @@ import equalArrays from "../utils/equalArrays.js";
export default class BitSet {

constructor() {
this.data = [];
this.data = new Uint32Array(1);
}

add(value) {
this.data[value] = true;
set(index) {
BitSet._checkIndex(index)
this._resize(index);
this.data[index >>> 5] |= 1 << index % 32;
}

or(set) {
Object.keys(set.data).map(alt => this.add(alt), this);
get(index) {
BitSet._checkIndex(index)
const slot = index >>> 5;
if (slot >= this.data.length) {
return false;
}
return (this.data[slot] & 1 << index % 32) !== 0;
}

remove(value) {
delete this.data[value];
clear(index) {
BitSet._checkIndex(index)
const slot = index >>> 5;
if (slot < this.data.length) {
this.data[slot] &= ~(1 << index);
}
}

has(value) {
return this.data[value] === true;
or(set) {
const minCount = Math.min(this.data.length, set.data.length);
for (let k = 0; k < minCount; ++k) {
this.data[k] |= set.data[k];
}
if (this.data.length < set.data.length) {
this._resize((set.data.length << 5) - 1);
const c = set.data.length;
for (let k = minCount; k < c; ++k) {
this.data[k] = set.data[k];
}
}
}

values() {
return Object.keys(this.data);
const result = new Array(this.length);
let pos = 0;
const length = this.data.length;
for (let k = 0; k < length; ++k) {
let l = this.data[k];
while (l !== 0) {
const t = l & -l;
result[pos++] = (k << 5) + BitSet._bitCount(t - 1);
l ^= t;
}
}
return result;
}

minValue() {
return Math.min.apply(null, this.values());
for (let k = 0; k < this.data.length; ++k) {
let l = this.data[k];
if (l !== 0) {
let result = 0;
while ((l & 1) === 0) {
result++;
l >>= 1;
}
return result + (32 * k);
}
}
return 0;
}

hashCode() {
Expand All @@ -47,7 +90,34 @@ export default class BitSet {
return "{" + this.values().join(", ") + "}";
}

get length(){
return this.values().length;
get length() {
return this.data.map(l => BitSet._bitCount(l)).reduce((s, v) => s + v, 0);
}

_resize(index) {
const count = index + 32 >>> 5;
if (count <= this.data.length) {
return;
}
const data = new Uint32Array(count);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you constructing a new array when I think Uint32Array.resize() is available?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid your thinking is incorrect. You can resize an ArrayBuffer but not a TypedArray. I suspect the reason for this is that the underlying buffer is shared (a poor design decision if you ask me) so resizing a view may actually silently resize other views. Resizing the buffer would be a hack, not something I'm comfortable with

data.set(this.data);
data.fill(0, this.data.length);
this.data = data;
}

static _checkIndex(index) {
if (index < 0)
throw new RangeError("index cannot be negative");
}

static _bitCount(l) {
// see https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
let count = 0;
l = l - ((l >> 1) & 0x55555555);
l = (l & 0x33333333) + ((l >> 2) & 0x33333333);
l = (l + (l >> 4)) & 0x0f0f0f0f;
l = l + (l >> 8);
l = l + (l >> 16);
return count + l & 0x3f;
}
}