Skip to content

Commit

Permalink
querystring: reduce memory usage by Int8Array
Browse files Browse the repository at this point in the history
PR-URL: #34179
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Denys Otrishko <shishugi@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
  • Loading branch information
sapics authored and BethGriggs committed Dec 15, 2020
1 parent b05cdfe commit e161249
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
5 changes: 3 additions & 2 deletions lib/internal/querystring.js
Expand Up @@ -2,6 +2,7 @@

const {
Array,
Int8Array,
} = primordials;

const { ERR_INVALID_URI } = require('internal/errors').codes;
Expand All @@ -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
Expand All @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions lib/internal/url.js
Expand Up @@ -2,6 +2,7 @@

const {
Array,
Int8Array,
Number,
ObjectCreate,
ObjectDefineProperties,
Expand Down Expand Up @@ -817,7 +818,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
*/
Expand All @@ -829,7 +830,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();
Expand Down
9 changes: 5 additions & 4 deletions lib/querystring.js
Expand Up @@ -26,6 +26,7 @@
const {
Array,
ArrayIsArray,
Int8Array,
MathAbs,
ObjectCreate,
ObjectKeys,
Expand Down Expand Up @@ -53,7 +54,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
Expand All @@ -70,7 +71,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);
Expand Down Expand Up @@ -130,7 +131,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
Expand All @@ -139,7 +140,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) {
Expand Down
5 changes: 3 additions & 2 deletions lib/url.js
Expand Up @@ -22,6 +22,7 @@
'use strict';

const {
Int8Array,
ObjectCreate,
ObjectKeys,
SafeSet,
Expand Down Expand Up @@ -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
Expand All @@ -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 || '';
Expand Down

0 comments on commit e161249

Please sign in to comment.