Merge pull request #159 from gaearon/l-to-the-r-to-the-u

Use LRU for encoded strings
This commit is contained in:
Brian Vaughn
2019-04-15 07:09:10 -07:00
committed by GitHub
+14 -1
View File
@@ -1,8 +1,14 @@
// @flow
const LRU = require('lru-cache');
const FB_MODULE_RE = /^(.*) \[from (.*)\]$/;
const cachedDisplayNames: WeakMap<Function, string> = new WeakMap();
// On large trees, encoding takes significant time.
// Try to reuse the already encoded strings.
let encodedStringCache = new LRU({ max: 1000 });
export function getDisplayName(
type: Function,
fallbackName: string = 'Unknown'
@@ -56,8 +62,15 @@ export function utfDecodeString(array: Uint32Array): string {
}
export function utfEncodeString(string: string): Uint32Array {
let cached = encodedStringCache.get(string);
if (cached !== undefined) {
return cached;
}
// $FlowFixMe Flow's Uint32Array.from's type definition is wrong; first argument of mapFn will be string
return Uint32Array.from(string, toCodePoint);
const encoded = Uint32Array.from(string, toCodePoint);
encodedStringCache.set(string, encoded);
return encoded;
}
function toCodePoint(string: string) {