Skip to content

Commit 594bb9d

Browse files
BridgeARdanielleadams
authored andcommitted
os: improve network interface performance
This reduces the overhead of getCIDR() to a minimum. No array is allocated anymore and parts are directly sliced out of the netmask string instead. Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de> PR-URL: #46598 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 4cf3de8 commit 594bb9d

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

lib/os.js

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ const {
2828
ObjectDefineProperties,
2929
StringPrototypeEndsWith,
3030
StringPrototypeSlice,
31-
StringPrototypeSplit,
3231
SymbolToPrimitive,
3332
} = primordials;
3433

@@ -225,28 +224,38 @@ function getCIDR(address, netmask, family) {
225224
let range = 10;
226225
let groupLength = 8;
227226
let hasZeros = false;
227+
let lastPos = 0;
228228

229229
if (family === 'IPv6') {
230230
split = ':';
231231
range = 16;
232232
groupLength = 16;
233233
}
234234

235-
const parts = StringPrototypeSplit(netmask, split);
236-
for (let i = 0; i < parts.length; i++) {
237-
if (parts[i] !== '') {
238-
const binary = NumberParseInt(parts[i], range);
239-
const tmp = countBinaryOnes(binary);
240-
ones += tmp;
235+
for (let i = 0; i < netmask.length; i++) {
236+
if (netmask[i] !== split) {
237+
if (i + 1 < netmask.length) {
238+
continue;
239+
}
240+
i++;
241+
}
242+
const part = StringPrototypeSlice(netmask, lastPos, i);
243+
lastPos = i + 1;
244+
if (part !== '') {
241245
if (hasZeros) {
242-
if (tmp !== 0) {
246+
if (part !== '0') {
243247
return null;
244248
}
245-
} else if (tmp !== groupLength) {
246-
if ((binary & 1) !== 0) {
247-
return null;
249+
} else {
250+
const binary = NumberParseInt(part, range);
251+
const binaryOnes = countBinaryOnes(binary);
252+
ones += binaryOnes;
253+
if (binaryOnes !== groupLength) {
254+
if ((binary & 1) !== 0) {
255+
return null;
256+
}
257+
hasZeros = true;
248258
}
249-
hasZeros = true;
250259
}
251260
}
252261
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy