Skip to content

Commit

Permalink
os: improve network interface performance
Browse files Browse the repository at this point in the history
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 <[email protected]>
PR-URL: #46598
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
BridgeAR authored and targos committed Mar 13, 2023
1 parent 7b5c00a commit e5b8597
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions lib/os.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ const {
ObjectDefineProperties,
StringPrototypeEndsWith,
StringPrototypeSlice,
StringPrototypeSplit,
SymbolToPrimitive,
} = primordials;

Expand Down Expand Up @@ -225,28 +224,38 @@ function getCIDR(address, netmask, family) {
let range = 10;
let groupLength = 8;
let hasZeros = false;
let lastPos = 0;

if (family === 'IPv6') {
split = ':';
range = 16;
groupLength = 16;
}

const parts = StringPrototypeSplit(netmask, split);
for (let i = 0; i < parts.length; i++) {
if (parts[i] !== '') {
const binary = NumberParseInt(parts[i], range);
const tmp = countBinaryOnes(binary);
ones += tmp;
for (let i = 0; i < netmask.length; i++) {
if (netmask[i] !== split) {
if (i + 1 < netmask.length) {
continue;
}
i++;
}
const part = StringPrototypeSlice(netmask, lastPos, i);
lastPos = i + 1;
if (part !== '') {
if (hasZeros) {
if (tmp !== 0) {
if (part !== '0') {
return null;
}
} else if (tmp !== groupLength) {
if ((binary & 1) !== 0) {
return null;
} else {
const binary = NumberParseInt(part, range);
const binaryOnes = countBinaryOnes(binary);
ones += binaryOnes;
if (binaryOnes !== groupLength) {
if ((binary & 1) !== 0) {
return null;
}
hasZeros = true;
}
hasZeros = true;
}
}
}
Expand Down

0 comments on commit e5b8597

Please sign in to comment.