generated from norskeld/serpent
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: improve performance by simplifying values passed around
- Got noticeable gains by getting rid of 'state' object and constantly copying input string. - Got rid of helper functions which were causing overhead and unnecessary pressure.
- Loading branch information
Showing
43 changed files
with
254 additions
and
252 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,33 @@ | ||
import { success, type Parser, type ToTuple } from '../state' | ||
import type { Parser, ToTuple } from '../state' | ||
|
||
export function sequence<T extends Array<Parser<unknown>>>(...ps: T): Parser<ToTuple<T>> | ||
export function sequence<T>(...ps: Array<Parser<T>>): Parser<Array<T>> { | ||
return { | ||
parse(state) { | ||
let values: Array<T> = [] | ||
let nextState = state | ||
parse(input, pos) { | ||
const values: Array<T> = [] | ||
let nextPos = pos | ||
|
||
for (const parser of ps) { | ||
const result = parser.parse(nextState) | ||
const result = parser.parse(input, nextPos) | ||
|
||
switch (result.kind) { | ||
case 'success': { | ||
values = [...values, result.value] | ||
nextState = result.state | ||
switch (result.isOk) { | ||
case true: { | ||
values.push(result.value) | ||
nextPos = result.pos | ||
break | ||
} | ||
|
||
case 'failure': { | ||
case false: { | ||
return result | ||
} | ||
} | ||
} | ||
|
||
return success(nextState, values) | ||
return { | ||
isOk: true, | ||
pos: nextPos, | ||
value: values | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
import { success, type Parser } from '../state' | ||
import { type Parser } from '../state' | ||
|
||
export function nothing(): Parser<null> { | ||
return { | ||
parse(state) { | ||
return success(state, null) | ||
parse(_, pos) { | ||
return { | ||
isOk: true, | ||
pos, | ||
value: null | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,13 @@ | ||
import { success, type Parser } from '../state' | ||
import { type Parser } from '../state' | ||
|
||
export function rest(): Parser<string> { | ||
return { | ||
parse(state) { | ||
return success( | ||
{ | ||
text: state.text, | ||
index: state.text.length | ||
}, | ||
state.text.substring(state.index) | ||
) | ||
parse(input, pos) { | ||
return { | ||
isOk: true, | ||
pos: input.length, | ||
value: input.substring(pos) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
505dad8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
sigma – ./
sigma-norskeld.vercel.app
sigma-pearl.vercel.app
sigma-git-master-norskeld.vercel.app
sigma.vm.codes