From 93c1fac7e6547342cef60b3a732380f4d0f6f4d5 Mon Sep 17 00:00:00 2001 From: William Bert Date: Mon, 22 Jan 2024 15:07:02 -0500 Subject: [PATCH 1/3] Cache with Map. --- lib/parsers/string.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/parsers/string.js b/lib/parsers/string.js index 5523fb2c6a..39f06593c2 100644 --- a/lib/parsers/string.js +++ b/lib/parsers/string.js @@ -1,13 +1,22 @@ 'use strict'; const Iconv = require('iconv-lite'); +const decoderCache = new Map(); -exports.decode = function(buffer, encoding, start, end, options) { +exports.decode = function (buffer, encoding, start, end, options) { if (Buffer.isEncoding(encoding)) { return buffer.toString(encoding, start, end); } - const decoder = Iconv.getDecoder(encoding, options || {}); + const decoderArgs = { encoding, options: options || {} }; + const decoder = + decoderCache.get(decoderArgs) || + decoderCache + .set( + decoderArgs, + Iconv.getDecoder(decoderArgs.encoding, decoderArgs.options), + ) + .get(decoderArgs); const res = decoder.write(buffer.slice(start, end)); const trail = decoder.end(); @@ -15,7 +24,7 @@ exports.decode = function(buffer, encoding, start, end, options) { return trail ? res + trail : res; }; -exports.encode = function(string, encoding, options) { +exports.encode = function (string, encoding, options) { if (Buffer.isEncoding(encoding)) { return Buffer.from(string, encoding); } From 5f4ed8464a05daba52cc2717ee64a3d293abde1f Mon Sep 17 00:00:00 2001 From: William Bert Date: Mon, 22 Jan 2024 15:41:08 -0500 Subject: [PATCH 2/3] Cache with lru-cache. --- lib/parsers/string.js | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/lib/parsers/string.js b/lib/parsers/string.js index 39f06593c2..0e6e0ae1dc 100644 --- a/lib/parsers/string.js +++ b/lib/parsers/string.js @@ -1,22 +1,34 @@ 'use strict'; const Iconv = require('iconv-lite'); -const decoderCache = new Map(); +const LRU = require('lru-cache'); + +const decoderCache = new LRU({ + max: 500, +}); exports.decode = function (buffer, encoding, start, end, options) { if (Buffer.isEncoding(encoding)) { return buffer.toString(encoding, start, end); } - const decoderArgs = { encoding, options: options || {} }; - const decoder = - decoderCache.get(decoderArgs) || - decoderCache - .set( - decoderArgs, - Iconv.getDecoder(decoderArgs.encoding, decoderArgs.options), - ) - .get(decoderArgs); + // Optimize for common case: encoding="short_string", options=undefined. + let decoder; + if (!options) { + decoder = decoderCache.get(encoding); + if (!decoder) { + decoder = Iconv.getDecoder(encoding); + decoderCache.set(encoding, decoder); + } + } else { + const decoderArgs = { encoding, options }; + const decoderKey = JSON.stringify(decoderArgs); + decoder = decoderCache.get(decoderKey); + if (!decoder) { + decoder = Iconv.getDecoder(decoderArgs.encoding, decoderArgs.options); + decoderCache.set(decoderKey, decoder); + } + } const res = decoder.write(buffer.slice(start, end)); const trail = decoder.end(); From 15eec77947080d280c899d5324d5364d5ad72da3 Mon Sep 17 00:00:00 2001 From: William Bert Date: Tue, 23 Jan 2024 08:50:24 -0500 Subject: [PATCH 3/3] Require default explicitly. --- lib/parsers/string.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/parsers/string.js b/lib/parsers/string.js index 0e6e0ae1dc..47c9bfa8a0 100644 --- a/lib/parsers/string.js +++ b/lib/parsers/string.js @@ -1,7 +1,7 @@ 'use strict'; const Iconv = require('iconv-lite'); -const LRU = require('lru-cache'); +const LRU = require('lru-cache').default; const decoderCache = new LRU({ max: 500,