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 <ruben@bridgewater.de>
  • Loading branch information
BridgeAR committed Feb 10, 2023
1 parent 7d68b7b commit 2a41858
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions lib/os.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,28 +225,39 @@ 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;
} else {
i++;
}
}
const part = netmask.slice(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 = Number.parseInt(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 2a41858

Please sign in to comment.