-
-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactors 'path' to use 'class LazyPath' instead of PathItem[]. Issue paths are only needed when flattening errors, so eagerly constructing them for the ParseInfo objects passed to every parser results in a lot of unnecessary allocations. Skipping most of these allocations and evaluating the paths only when they are needed improves performance.
- Loading branch information
1 parent
2429f44
commit 0619381
Showing
7 changed files
with
65 additions
and
37 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,48 @@ | ||
import type { PathItem } from '../../types.ts'; | ||
|
||
/** | ||
* Returns the current path. | ||
* Wrapper for issue paths that only concatenates the full path when it is needed. | ||
* | ||
* @param prevPath Previous, "parent", path. Optional. | ||
* @param pathItem The "leaf" path item to add to the full path. | ||
* | ||
* @returns instance of LazyPath | ||
*/ | ||
export class LazyPath { | ||
private prevPath: LazyPath | undefined; | ||
private pathItem: PathItem; | ||
private cachedPath: PathItem[] = []; | ||
constructor(prevPath: LazyPath | undefined, pathItem: PathItem) { | ||
this.prevPath = prevPath; | ||
this.pathItem = pathItem; | ||
} | ||
|
||
get evaluatedPath(): PathItem[] { | ||
if (!this.cachedPath.length) { | ||
if (this.prevPath) { | ||
const prevPath = this.prevPath.evaluatedPath; | ||
for (const pathItem of prevPath) { | ||
this.cachedPath.push(pathItem); | ||
} | ||
} | ||
this.cachedPath.push(this.pathItem); | ||
} | ||
|
||
return this.cachedPath; | ||
} | ||
} | ||
|
||
/** | ||
* Returns a lazy representation of the current path. `path.evaluatedPath` evaluates the path. | ||
* | ||
* TODO: Prüfen ob es wirklich erforderlich ist einen neuen Array zu erstellen. | ||
* TODO: Prüfen ob for loop nur wegen Profiler schneller als spread war | ||
* | ||
* @param info The parse info. | ||
* @param key The current key. | ||
* | ||
* @returns The current path. | ||
* @returns A lazy representation of the current path. | ||
*/ | ||
export function getPath(prevPath: PathItem[] | undefined, pathItem: PathItem) { | ||
// Note: Array is copied with for loop instead of spread operator for | ||
// performance reasons | ||
const path: PathItem[] = []; | ||
if (prevPath) { | ||
for (const pathItem of prevPath) { | ||
path.push(pathItem); | ||
} | ||
} | ||
path.push(pathItem); | ||
return path; | ||
export function getPath(prevPath: LazyPath | undefined, pathItem: PathItem) { | ||
return new LazyPath(prevPath, pathItem); | ||
} |
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