Skip to content

Commit cd5fbce

Browse files
committed
Lexer performance improvement
1 parent 6c18d45 commit cd5fbce

File tree

4 files changed

+10
-29
lines changed

4 files changed

+10
-29
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ node_modules/
22
.npm
33
.eslintcache
44
.antlr
5+
.vscode
56

67
bin/
78
build/

.npmignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ node_modules/
55
.github/
66
.gitignore
77
.npmignore
8+
.vscode
89

910
# Build
1011
bin/
@@ -38,3 +39,4 @@ npm-debug.log*
3839
yarn-debug.log*
3940
yarn-error.log*
4041
lerna-debug.log*
42+
*.cpuprofile

src/lib/parse.js

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
/**
32
* @internal
43
* Very basic GLSL parsing handling #version and #extension preprocessor directives
@@ -8,25 +7,6 @@ export function* simpleParse(input) {
87
yield* parser(lexer(input));
98
}
109

11-
/**
12-
* @internal
13-
* @param {string} input
14-
* @return {Generator<[string,string],void,void>}
15-
*/
16-
function* iterateStringLookahead(input) {
17-
const inputGenerator = input[Symbol.iterator]();
18-
let cur = inputGenerator.next();
19-
let next = inputGenerator.next();
20-
while (!cur.done && !next.done) {
21-
yield [cur.value, next.value];
22-
cur = next;
23-
next = inputGenerator.next();
24-
}
25-
if (!cur.done) {
26-
yield [cur.value, undefined];
27-
}
28-
}
29-
3010
/**
3111
* @typedef {1|2|3|4|5|6|7|8} TokenID Token ID
3212
* @return {TokenID}
@@ -107,7 +87,13 @@ function* lexer(input) {
10787
line++; col = 0;
10888
};
10989

110-
for (const [cur, next] of iterateStringLookahead(input)) {
90+
let next = input[0];
91+
let cur;
92+
/* Even though GLSL uses UTF-8 encoding, the actual syntactic charset is ASCII
93+
So we can use JS indexed iteration (UTF-16) for a speedup compared to full Unicode iteration (for of) */
94+
for (let i = 1; i <= input.length; i++) {
95+
cur = next;
96+
next = i < input.length ? input[i] : undefined;
11197
col++;
11298
if (skipOne) {
11399
skipOne = false;
@@ -325,7 +311,6 @@ const printToken = (t) => {
325311
};
326312

327313
export const test = {
328-
iterateStringLookahead,
329314
lexer,
330315
parser,
331316
printToken,

test/tests/parser.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,6 @@ function unMockConsoleError() {
1818

1919
export function parserTests() {
2020
describe('Parser', function() {
21-
describe('#iterateStringLookahead()', function() {
22-
it('should correctly yield lookahead for a string', function() {
23-
const input = 'test';
24-
const result = [...parse.test.iterateStringLookahead(input)];
25-
assert.deepStrictEqual(result, [['t', 'e'], ['e', 's'], ['s', 't'], ['t', undefined]]);
26-
});
27-
});
2821
describe('#lexer()', function() {
2922
const lexerTest = (input) => [...parse.test.lexer(input)].map(parse.test.printToken);
3023
it('should lex empty input', function() {

0 commit comments

Comments
 (0)