-
-
Notifications
You must be signed in to change notification settings - Fork 87
Improved url-utils performance for sitemap-scale workloads #1095
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
Merged
+1,121
−106
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
59630b9
Added freeze() and cached root URL parsing to url-utils
acburdine c967e95
Sped up transformReadyToAbsolute
acburdine f9d6673
Computed permalink dates lazily without moment
acburdine d145bfa
Parsed permalink patterns once in replacePermalink
acburdine 979bc10
Added shared lru-cache memoize helper for url-utils caches
acburdine be8abbe
Simplified replacePermalink date parts
acburdine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import {LRUCache} from 'lru-cache'; | ||
|
|
||
| export interface Memoized<V> { | ||
| (key: string): V; | ||
| clear(): void; | ||
| } | ||
|
|
||
| /** | ||
| * Memoize a pure single-argument function keyed on its string input, keeping | ||
| * the `max` most recently used results so arbitrary input can't grow the cache | ||
| * unchecked. Errors thrown by `fn` are not cached. | ||
| * | ||
| * @param {Function} fn function to memoize, must be a pure function of its input | ||
| * @param {number} [max=100] maximum number of cached results | ||
| * @returns {Function} memoized function with a `clear()` method | ||
| */ | ||
| export default function memoize<V extends object | string>(fn: (key: string) => V, max: number = 100): Memoized<V> { | ||
| const cache = new LRUCache<string, V>({max}); | ||
|
|
||
| const memoized = function (key: string): V { | ||
| let value = cache.get(key); | ||
|
|
||
| if (value === undefined) { | ||
| value = fn(key); | ||
| cache.set(key, value); | ||
| } | ||
|
|
||
| return value; | ||
| }; | ||
|
|
||
| memoized.clear = function clear(): void { | ||
| cache.clear(); | ||
| }; | ||
|
|
||
| return memoized; | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import {URL} from 'url'; | ||
| import memoize from './memoize'; | ||
|
|
||
| export interface ParsedRootUrl { | ||
| readonly href: string; | ||
| readonly origin: string; | ||
| readonly protocol: string; | ||
| readonly host: string; | ||
| readonly hostname: string; | ||
| readonly pathname: string; | ||
| } | ||
|
|
||
| /** | ||
| * Parse a root URL, returning a cached immutable snapshot of the parts url-utils uses. | ||
| * Throws the same errors as `new URL()` for invalid input (errors are not cached). | ||
| * | ||
| * Root URLs (site url, admin url, CDN base urls) are a tiny, near-static set of | ||
| * strings that get re-parsed on almost every url-utils call. Parsing is a pure | ||
| * function of the input string so results can't go stale. | ||
| * | ||
| * @param {string} rootUrl | ||
| * @returns {ParsedRootUrl} | ||
| */ | ||
| const parseRootUrl = memoize(function parseRootUrl(rootUrl: string): ParsedRootUrl { | ||
| const url = new URL(rootUrl); | ||
|
|
||
| return Object.freeze({ | ||
| href: url.href, | ||
| origin: url.origin, | ||
| protocol: url.protocol, | ||
| host: url.host, | ||
| hostname: url.hostname, | ||
| pathname: url.pathname | ||
| }); | ||
| }); | ||
|
|
||
| export default parseRootUrl; |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.