querystring: reduce memory usage by Int8Array

PR-URL: https://github.com/nodejs/node/pull/34179
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Denys Otrishko <shishugi@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
pull/35779/head
sapics 2020-07-05 09:47:39 +09:00 committed by Node.js GitHub Bot
parent 0dee49800d
commit 00b5ee6083
4 changed files with 14 additions and 10 deletions

View File

@ -2,6 +2,7 @@
const {
Array,
Int8Array,
} = primordials;
const { ERR_INVALID_URI } = require('internal/errors').codes;
@ -10,7 +11,7 @@ const hexTable = new Array(256);
for (let i = 0; i < 256; ++i)
hexTable[i] = '%' + ((i < 16 ? '0' : '') + i.toString(16)).toUpperCase();
const isHexTable = [
const isHexTable = new Int8Array([
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0 - 15
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16 - 31
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 32 - 47
@ -27,7 +28,7 @@ const isHexTable = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // ... 256
];
]);
function encodeStr(str, noEscapeTable, hexTable) {
const len = str.length;

View File

@ -2,6 +2,7 @@
const {
Array,
Int8Array,
Number,
ObjectCreate,
ObjectDefineProperties,
@ -819,7 +820,7 @@ function parseParams(qs) {
// Adapted from querystring's implementation.
// Ref: https://url.spec.whatwg.org/#concept-urlencoded-byte-serializer
const noEscape = [
const noEscape = new Int8Array([
/*
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
*/
@ -831,7 +832,7 @@ const noEscape = [
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, // 0x50 - 0x5F
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x60 - 0x6F
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 // 0x70 - 0x7F
];
]);
// Special version of hexTable that uses `+` for U+0020 SPACE.
const paramHexTable = hexTable.slice();

View File

@ -26,6 +26,7 @@
const {
Array,
ArrayIsArray,
Int8Array,
MathAbs,
NumberIsFinite,
ObjectCreate,
@ -54,7 +55,7 @@ const QueryString = module.exports = {
decode: parse
};
const unhexTable = [
const unhexTable = new Int8Array([
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0 - 15
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 16 - 31
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 32 - 47
@ -71,7 +72,7 @@ const unhexTable = [
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 // ... 255
];
]);
// A safe fast alternative to decodeURIComponent
function unescapeBuffer(s, decodeSpaces) {
const out = Buffer.allocUnsafe(s.length);
@ -131,7 +132,7 @@ function qsUnescape(s, decodeSpaces) {
// digits
// alpha (uppercase)
// alpha (lowercase)
const noEscape = [
const noEscape = new Int8Array([
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0 - 15
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16 - 31
0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, // 32 - 47
@ -140,7 +141,7 @@ const noEscape = [
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, // 80 - 95
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 96 - 111
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0 // 112 - 127
];
]);
// QueryString.escape() replaces encodeURIComponent()
// https://www.ecma-international.org/ecma-262/5.1/#sec-15.1.3.4
function qsEscape(str) {

View File

@ -22,6 +22,7 @@
'use strict';
const {
Int8Array,
ObjectCreate,
ObjectKeys,
SafeSet,
@ -561,7 +562,7 @@ function urlFormat(urlObject, options) {
// digits
// alpha (uppercase)
// alpha (lowercase)
const noEscapeAuth = [
const noEscapeAuth = new Int8Array([
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x00 - 0x0F
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x10 - 0x1F
0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, // 0x20 - 0x2F
@ -570,7 +571,7 @@ const noEscapeAuth = [
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, // 0x50 - 0x5F
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x60 - 0x6F
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0 // 0x70 - 0x7F
];
]);
Url.prototype.format = function format() {
let auth = this.auth || '';