Skip to content
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

Don't store request specific data into the Layer object #113

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
23 changes: 11 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,20 +233,19 @@ Router.prototype.handle = function handle(req, res, callback) {

// find next matching layer
var layer
var match
var match = null
var route

while (match !== true && idx < stack.length) {
while (!match && idx < stack.length) {
layer = stack[idx++]
match = matchLayer(layer, path)
route = layer.route

if (typeof match !== 'boolean') {
if (match && typeof match.path !== 'string') {
// hold on to layerError
layerError = layerError || match
}
route = layer.route

if (match !== true) {
if (!match) {
continue
}

Expand All @@ -257,7 +256,7 @@ Router.prototype.handle = function handle(req, res, callback) {

if (layerError) {
// routes do not match with a pending error
match = false
match = null
continue
}

Expand All @@ -271,13 +270,13 @@ Router.prototype.handle = function handle(req, res, callback) {

// don't even bother matching route
if (!has_method && method !== 'HEAD') {
match = false
match = null
continue
}
}

// no match
if (match !== true) {
if (!match) {
return done(layerError)
}

Expand All @@ -288,9 +287,9 @@ Router.prototype.handle = function handle(req, res, callback) {

// Capture one-time layer values
req.params = self.mergeParams
? mergeParams(layer.params, parentParams)
: layer.params
var layerPath = layer.path
? mergeParams(match.params, parentParams)
: match.params
var layerPath = match.path

// this should be done for the layer
self.process_params(layer, paramcalled, req, res, function (err) {
Expand Down
31 changes: 14 additions & 17 deletions lib/layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ function Layer(path, options, fn) {

this.handle = fn
this.name = fn.name || '<anonymous>'
this.params = undefined
this.path = undefined
this.regexp = pathRegexp(path, this.keys = [], opts)

// set fast path flags
Expand Down Expand Up @@ -111,35 +109,31 @@ Layer.prototype.match = function match(path) {
if (path != null) {
// fast path non-ending match for / (any path matches)
if (this.regexp.fast_slash) {
this.params = {}
this.path = ''
return true
return {
path: '',
params: {}
}
}

// fast path for * (everything matched in a param)
if (this.regexp.fast_star) {
this.params = {'0': decode_param(path)}
this.path = path
return true
return {
path: path,
params: {'0': decode_param(path)}
}
Copy link
Member

Choose a reason for hiding this comment

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

This code has been around in this state for a very long time. My worry is that this new way creates garbage to collect (the new object added) on a hot path, which might be the reason it was done this way in the first place. Maybe @dougwilson will remember because this predates me by quite a bit. Without more context I would like to see us do some sort of performance pass on this since I don't think the feature addition would be worth it if we impacted performance in a negative way.

Copy link
Contributor

Choose a reason for hiding this comment

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

A big part was preventing bail out due to the same variable being different shapes (i.e. here we now have something that can be a bool or an object, and v8 will struggle to make a bytecode and may be falling back to slow interpreter mode). Ideally if we want to keep things in hot paths fast they should definitely keep the same shapes for the byte code generator.

I don't remember off hand all the details for why it was mutating properties, but it was very likely to reduce generating a bunch of little objects only to trash them immediately. That can be addressed if an object pool is used for them, however.

Copy link
Contributor

Choose a reason for hiding this comment

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

I haven't run through a perf yet as I am away, but I suspect using null instead of false would fix any bail outs since those would both be objects

Copy link
Member

Choose a reason for hiding this comment

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

Yeah that is a good point as well. I think ideally we have some real world benchmarks to base these decisions off of, but I think in theory both the de-opt from different shapes and increased GC from these little objects will be perf regressions. It would be nice to comment these with links to the benchmarks at some point so that the context is kept in the code for future contributors.

Copy link
Contributor

Choose a reason for hiding this comment

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

So far what I have done is run the perf with the test suite, which the deopts from there usually pretty low hanging fruit.

Copy link
Contributor

Choose a reason for hiding this comment

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

But ya, if it needs a shaped object can make a no match object at the top level and return that reference for the no match

Copy link
Author

Choose a reason for hiding this comment

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

I made the change to use null instead of false for the no match scenario (I added a new commit to help reading the new changes, but I can squash them if you prefer).

Copy link
Member

Choose a reason for hiding this comment

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

I am not sure I see how that addresses the worry here. The change you made does mean you could return some of those boolean checks back to their original form I think, which is overall good to reduce the change for change's sake, but I think this comment thread here is about the method returning a boolean and not producing unnecessary garbage in the process.

I am not suggesting removing the last changes, but I think ideally we find a way to not create these objects yet still clean up the state after the layers are finished being handled.

}

// match the path
match = this.regexp.exec(path)
}

if (!match) {
this.params = undefined
this.path = undefined
return false
return null
}

// store values
this.params = {}
this.path = match[0]

// iterate matches
var keys = this.keys
var params = this.params
var params = {}

for (var i = 1; i < match.length; i++) {
var key = keys[i - 1]
Expand All @@ -151,7 +145,10 @@ Layer.prototype.match = function match(path) {
}
}

return true
return {
path: match[0],
params: params
}
}

/**
Expand Down
22 changes: 22 additions & 0 deletions test/req.params.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var Router = require('..')
var utils = require('./support/utils')

var createServer = utils.createServer
var assert = utils.assert
var request = utils.request

describe('req.params', function () {
Expand Down Expand Up @@ -67,6 +68,27 @@ describe('req.params', function () {
.expect(200, '{"foo":"bar"}', done)
})

it('should not keep parameters in memory', function (done) {
var router = Router()
var server = createServer(function (req, res, next) {
router(req, res, function (err) {
if (err) return next(err)
sawParams(req, res)
})
})

router.get('/:fizz', hitParams(1))

request(server)
.get('/buzz')
.end(function(err) {
if (err) return done(err)

assert.strictEqual(router.stack[0].params, undefined)
done()
})
})

describe('when "mergeParams: true"', function () {
it('should merge outside object with params', function (done) {
var router = Router({ mergeParams: true })
Expand Down