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

Replace Map<number, T> with sparse array in DFAState #468

Open
wants to merge 31 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
1bc2883
chore: eliminate 'npm link' from the build scripts.
Apr 11, 2020
c03ef91
Update vscode tasks.json
Apr 11, 2020
6c32473
(tool) Update pom.xml removing ref to ../runtime
Apr 15, 2020
db0b346
Relocate runtime build files to runtime/typescript
Apr 15, 2020
5078dfe
Move custom tool under tool/typescript
Apr 15, 2020
63a2325
cleanup target files that should not be checked in
Apr 16, 2020
e260009
Upgrade TypeScript to v2.9.2.
Apr 16, 2020
6f9e2db
Simplify build scripts w/ npm-run-all
Apr 16, 2020
8956cef
Move src/ to runtime/typescript/src
Apr 16, 2020
5a63b25
upgrade to [email protected]
Apr 16, 2020
59d7c4d
Upgrade TypeScript to V3.7.5
Apr 17, 2020
7311cf0
Update build process for speed. Operates across packages in this rep…
Apr 17, 2020
2d5a18c
Reintegrate the cross-platform tests
Apr 17, 2020
6f48f5f
tslint 6.1.1 and fix gripes
Apr 17, 2020
7e5bf51
Fix TestPerformance.ts so tsc doesn't hang
Apr 18, 2020
6fa15a9
more tslint...
Apr 18, 2020
f109206
whitespace in generated file
Apr 18, 2020
436cb61
Integrate eslint, prettier, huskey
Apr 19, 2020
0b2f879
Update CI control files to reflect change in lint, dropping pack steps
Apr 19, 2020
1743b12
Eliminate empty decorators
Apr 19, 2020
2239c13
clean attempts to provide imports without 'dist'
Apr 21, 2020
60e90f4
remove unneeded constructor overloads
Apr 21, 2020
7d02dde
Switch mocha config to .mocharc.yml
Apr 25, 2020
1a19980
Eleminate dependency on mocha-typescriptAll the tests now use the sam…
Apr 25, 2020
53db32a
Setup tsconfig.base.json
Apr 26, 2020
012c274
Implement internal module pattern
Apr 26, 2020
a312203
Merge branch 'build_reorg' into build_update
Apr 26, 2020
5c67dce
Eliminate async after fix of load order problem
Apr 27, 2020
bbb45ac
Mark vscode tsc-watch task as background
Apr 28, 2020
0f6e64b
update profiling to use node CPU profing switch
Apr 29, 2020
6195b5e
Replace Map<number, T> w Array<T>in DFAState.ts as optimization
Apr 29, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions runtime/typescript/src/dfa/DFA.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export class DFA {
get isEmpty(): boolean {
if (this.isPrecedenceDfa) {
// s0 and s0full are never null for a precedence DFA
return this.s0!.getEdgeMap().size === 0 && this.s0full!.getEdgeMap().size === 0;
return this.s0!.getEdgeMap().length === 0 && this.s0full!.getEdgeMap().length === 0;
}

return this.s0 == null && this.s0full == null;
Expand All @@ -180,7 +180,7 @@ export class DFA {
get isContextSensitive(): boolean {
if (this.isPrecedenceDfa) {
// s0full is never null for a precedence DFA
return (this.s0full as DFAState).getEdgeMap().size > 0;
return (this.s0full as DFAState).getEdgeMap().length > 0;
}

return this.s0full != null;
Expand Down
15 changes: 10 additions & 5 deletions runtime/typescript/src/dfa/DFASerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,17 @@ export class DFASerializer {
states.sort((o1, o2) => o1.stateNumber - o2.stateNumber);

for (const s of states) {
const edges: Map<number, DFAState> = s.getEdgeMap();
const edges: Array<DFAState> = s.getEdgeMap();
Copy link
Member

Choose a reason for hiding this comment

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

💭 Why Array<DFAState> instead of DFAState[]? I'm sure I used to know the answer...

Copy link
Collaborator Author

@BurtHarris BurtHarris Apr 29, 2020

Choose a reason for hiding this comment

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

They are the same thing. It just was easier to textually substitute without changing the trailing '>'.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Nice to hear from you.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

P.S. most of these changes are from earlier PR #454.

const edgeKeys = [...edges.keys()].sort((a, b) => a - b);
const contextEdges: Map<number, DFAState> = s.getContextEdgeMap();
const contextEdgeKeys = [...contextEdges.keys()].sort((a, b) => a - b);
const contextEdges: Array<DFAState> = s.getContextEdgeMap();
let contextEdgeKeys = new Array<number>();
contextEdges.forEach((value, index) => {
if (value != undefined)
contextEdgeKeys.push(index);
});
contextEdgeKeys = contextEdgeKeys.sort((a, b) => a - b);
for (const entry of edgeKeys) {
const value = edges.get(entry);
const value = edges[entry];
if ((value == null || value === ATNSimulator.ERROR) && !s.isContextSymbol(entry)) {
continue;
}
Expand All @@ -91,7 +96,7 @@ export class DFASerializer {
+ ("-")
+ (this.getContextLabel(entry))
+ ("->")
+ (this.getStateString(contextEdges.get(entry)!))
+ (this.getStateString(contextEdges[entry]))
+ ("\n");
}
}
Expand Down
40 changes: 22 additions & 18 deletions runtime/typescript/src/dfa/DFAState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@ export class DFAState {

public configs: ATNConfigSet;

/** `edges.get(symbol)` points to target of symbol.
/** `edges[symbol]` points to target of symbol.
*/

private readonly edges: Map<number, DFAState>;
private readonly edges: Array<DFAState>;

private _acceptStateInfo: AcceptStateInfo | undefined;

/** These keys for these edges are the top level element of the global context. */

private readonly contextEdges: Map<number, DFAState>;
private readonly contextEdges: Array<DFAState>;

/** Symbols in this set require a global context transition before matching an input symbol. */
private contextSymbols: BitSet | undefined;
Expand All @@ -74,8 +74,8 @@ export class DFAState {
*/
constructor(configs: ATNConfigSet) {
this.configs = configs;
this.edges = new Map<number, DFAState>();
this.contextEdges = new Map<number, DFAState>();
this.edges = new Array<DFAState>();
this.contextEdges = new Array<DFAState>();
}

get isContextSensitive(): boolean {
Expand Down Expand Up @@ -135,14 +135,14 @@ export class DFAState {
}

public getTarget(symbol: number): DFAState | undefined {
return this.edges.get(symbol);
return this.edges[symbol];
}

public setTarget(symbol: number, target: DFAState): void {
this.edges.set(symbol, target);
this.edges[symbol] = target;
}

public getEdgeMap(): Map<number, DFAState> {
public getEdgeMap(): Array<DFAState> {
return this.edges;
}

Expand All @@ -151,7 +151,7 @@ export class DFAState {
invokingState = -1;
}

return this.contextEdges.get(invokingState);
return this.contextEdges[invokingState];
}

public setContextTarget(invokingState: number, target: DFAState): void {
Expand All @@ -163,21 +163,25 @@ export class DFAState {
invokingState = -1;
}

this.contextEdges.set(invokingState, target);
this.contextEdges[invokingState] = target;
}

public getContextEdgeMap(): Map<number, DFAState> {
const map = new Map<number, DFAState>(this.contextEdges);
const existing = map.get(-1);
public getContextEdgeMap(): Array<DFAState> {
const map = new Array<DFAState>();
this.contextEdges.forEach((element, index) => {
if (element != undefined)
map[index] = element;
})
const existing = map[-1];
if (existing !== undefined) {
if (map.size === 1) {
const result = new Map<number, DFAState>();
result.set(PredictionContext.EMPTY_FULL_STATE_KEY, existing);
if (map.length === 1) {
const result = new Array<DFAState>();
result[PredictionContext.EMPTY_FULL_STATE_KEY] = existing;
return result;
}
else {
map.delete(-1);
map.set(PredictionContext.EMPTY_FULL_STATE_KEY, existing);
delete map[-1];
map[PredictionContext.EMPTY_FULL_STATE_KEY] = existing;
}
}

Expand Down