Skip to content

Avoid large memory allocations when doing forEach in case netmask is large (e.g. /8) #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib/netmask.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,15 @@ class Netmask
return new Netmask(long2ip(@netLong + (@size * count)), @mask)

forEach: (fn) ->
range = [ip2long(@first)..ip2long(@last)]
fn long2ip(long), long, index for long, index in range
# this implementation is not idiomatic but avoids large memory allocations (2 arrays, one for range and one for the results) in cases when then netmask is large
long=ip2long(@first)
lastLong=ip2long(@last)
index=0
while long<=lastLong
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add spaces around operators.

fn long2ip(long), long, index
index++
long++
return

# Returns the complete netmask formatted as `base/bitmask`
toString: ->
Expand Down
125 changes: 125 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 16 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
{
"author": "Olivier Poitrey <rs@dailymotion.com>",
"name": "netmask",
"name": "@dschenkelman/netmask",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't override account.

"description": "Parse and lookup IP network blocks",
"version": "1.0.6",
"version": "1.0.8",
"homepage": "https://github.com/rs/node-netmask",
"bugs": "https://github.com/rs/node-netmask/issues",
"license": "MIT",
"repository":
{
"repository": {
"type": "git",
"url": "git://github.com/rs/node-netmask.git"
},
"keywords": ["net", "mask", "ip", "network", "cidr", "netmask", "subnet", "ipcalc"],
"keywords": [
"net",
"mask",
"ip",
"network",
"cidr",
"netmask",
"subnet",
"ipcalc"
],
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't change format.

"main": "./lib/netmask",
"scripts":
{
"scripts": {
"prepublish": "coffee -c lib/*.coffee",
"test": "vows --spec test/*"
},
"engines":
{
"engines": {
"node": ">= 0.4.0"
},
"devDependencies":
{
"devDependencies": {
"coffee-script": ">=1.2.0",
"vows": "*"
}
Expand Down