caching stringLiteralReplacer values: 80% performance boost

This commit is contained in:
sanex3339
2016-12-22 22:21:25 +03:00
parent 728b2d7274
commit 6d136fecce
2 changed files with 47 additions and 8 deletions
+21 -5
View File
@@ -90,7 +90,7 @@ module.exports =
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 140);
/******/ return __webpack_require__(__webpack_require__.s = 141);
/******/ })
/************************************************************************/
/******/ ([
@@ -5806,6 +5806,10 @@ exports.NumberLiteralReplacer = NumberLiteralReplacer;
"use strict";
var _map = __webpack_require__(11);
var _map2 = _interopRequireDefault(_map);
var _getPrototypeOf = __webpack_require__(5);
var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
@@ -5844,6 +5848,7 @@ var StringLiteralReplacer = StringLiteralReplacer_1 = function (_AbstractReplace
var _this = (0, _possibleConstructorReturn3.default)(this, (StringLiteralReplacer.__proto__ || (0, _getPrototypeOf2.default)(StringLiteralReplacer)).call(this, options));
_this.stringLiteralCache = new _map2.default();
_this.customNodeGroupStorage = customNodeGroupStorage;
_this.stringArrayStorage = stringArrayStorage;
return _this;
@@ -5853,10 +5858,20 @@ var StringLiteralReplacer = StringLiteralReplacer_1 = function (_AbstractReplace
key: "replace",
value: function replace(nodeValue) {
var replaceWithStringArrayFlag = nodeValue.length >= StringLiteralReplacer_1.minimumLengthForStringArray && RandomGeneratorUtils_1.RandomGeneratorUtils.getRandomFloat(0, 1) <= this.options.stringArrayThreshold;
var result = void 0;
if (this.options.stringArray && replaceWithStringArrayFlag) {
return this.replaceStringLiteralWithStringArrayCall(nodeValue);
if (this.stringLiteralCache.has(nodeValue) && this.options.stringArrayEncoding !== StringArrayEncoding_1.StringArrayEncoding.rc4) {
return this.stringLiteralCache.get(nodeValue);
}
result = this.replaceStringLiteralWithStringArrayCall(nodeValue);
} else {
if (this.stringLiteralCache.has(nodeValue)) {
return this.stringLiteralCache.get(nodeValue);
}
result = "'" + Utils_1.Utils.stringToUnicodeEscapeSequence(nodeValue, !this.options.unicodeEscapeSequence) + "'";
}
return "'" + Utils_1.Utils.stringToUnicodeEscapeSequence(nodeValue, !this.options.unicodeEscapeSequence) + "'";
this.stringLiteralCache.set(nodeValue, result);
return result;
}
}, {
key: "replaceStringLiteralWithStringArrayCall",
@@ -5884,7 +5899,7 @@ var StringLiteralReplacer = StringLiteralReplacer_1 = function (_AbstractReplace
var stringArrayStorageCallsWrapperName = "_" + Utils_1.Utils.hexadecimalPrefix + rotatedStringArrayStorageId;
var hexadecimalIndex = "" + Utils_1.Utils.hexadecimalPrefix + Utils_1.Utils.decToHex(indexOfValue);
if (this.options.stringArrayEncoding === StringArrayEncoding_1.StringArrayEncoding.rc4) {
return stringArrayStorageCallsWrapperName + "('" + hexadecimalIndex + "', '" + Utils_1.Utils.stringToUnicodeEscapeSequence(rc4Key) + "')";
return stringArrayStorageCallsWrapperName + "('" + hexadecimalIndex + "', '" + Utils_1.Utils.stringToUnicodeEscapeSequence(rc4Key, !this.options.unicodeEscapeSequence) + "')";
}
return stringArrayStorageCallsWrapperName + "('" + hexadecimalIndex + "')";
}
@@ -7208,7 +7223,8 @@ module.exports = require("mkdirp");
module.exports = require("reflect-metadata");
/***/ },
/* 140 */
/* 140 */,
/* 141 */
/***/ function(module, exports, __webpack_require__) {
"use strict";
@@ -25,6 +25,11 @@ export class StringLiteralReplacer extends AbstractReplacer {
private static readonly rc4Keys: string[] = RandomGeneratorUtils.getRandomGenerator()
.n(() => RandomGeneratorUtils.getRandomGenerator().string({length: 4}), 50);
/**
* @type {Map<string, string>}
*/
private readonly stringLiteralCache: Map <string, string> = new Map();
/**
* @type {IStorage<ICustomNodeGroup>}
*/
@@ -61,11 +66,29 @@ export class StringLiteralReplacer extends AbstractReplacer {
&& RandomGeneratorUtils.getRandomFloat(0, 1) <= this.options.stringArrayThreshold
);
let result: string;
if (this.options.stringArray && replaceWithStringArrayFlag) {
return this.replaceStringLiteralWithStringArrayCall(nodeValue);
/**
* we can't use values from cache with `stringArrayEncoding: rc4`
* because rc4 key will be the same for same values
*/
if (this.stringLiteralCache.has(nodeValue) && this.options.stringArrayEncoding !== StringArrayEncoding.rc4) {
return <string>this.stringLiteralCache.get(nodeValue);
}
result = this.replaceStringLiteralWithStringArrayCall(nodeValue);
} else {
if (this.stringLiteralCache.has(nodeValue)) {
return <string>this.stringLiteralCache.get(nodeValue);
}
result = `'${Utils.stringToUnicodeEscapeSequence(nodeValue, !this.options.unicodeEscapeSequence)}'`;
}
return `'${Utils.stringToUnicodeEscapeSequence(nodeValue, !this.options.unicodeEscapeSequence)}'`;
this.stringLiteralCache.set(nodeValue, result);
return result;
}
/**
@@ -106,7 +129,7 @@ export class StringLiteralReplacer extends AbstractReplacer {
const hexadecimalIndex: string = `${Utils.hexadecimalPrefix}${Utils.decToHex(indexOfValue)}`;
if (this.options.stringArrayEncoding === StringArrayEncoding.rc4) {
return `${stringArrayStorageCallsWrapperName}('${hexadecimalIndex}', '${Utils.stringToUnicodeEscapeSequence(rc4Key)}')`;
return `${stringArrayStorageCallsWrapperName}('${hexadecimalIndex}', '${Utils.stringToUnicodeEscapeSequence(rc4Key, !this.options.unicodeEscapeSequence)}')`;
}
return `${stringArrayStorageCallsWrapperName}('${hexadecimalIndex}')`;