Fix bad bug with the "fast child" path: log.child({...}, true).

Timing impact. Before (numbers from my Mac):
    $ node tools/timechild.js
    Time `log.child`:
     - adding no fields:  0.01307ms per iteration
     - adding one field:  0.01336ms per iteration
     - adding two fields: 0.0134ms per iteration
     - adding serializer and one field: 0.01839ms per iteration
     - adding a (stderr) stream and one field: 0.01905ms per iteration
     - [fast] adding no fields:  0.000821ms per iteration
     - [fast] adding one field:  0.001067ms per iteration
     - [fast] adding two fields: 0.00122ms per iteration

After:
    $ node tools/timechild.js
    Time `log.child`:
     - adding no fields:  0.01243ms per iteration
     - adding one field:  0.01345ms per iteration
     - adding two fields: 0.01343ms per iteration
     - adding serializer and one field: 0.01671ms per iteration
     - adding a (stderr) stream and one field: 0.01915ms per iteration
     - [fast] adding no fields:  0.001742ms per iteration
     - [fast] adding one field:  0.00193ms per iteration
     - [fast] adding two fields: 0.002388ms per iteration

I.e., not great, but still 5x faster for the "fast child" path.
This commit is contained in:
Trent Mick 2012-02-27 16:46:25 -08:00
parent 993f9d851b
commit 8ea2533620
2 changed files with 15 additions and 5 deletions

View file

@ -2,7 +2,12 @@
## bunyan 0.6.8 (not yet released)
(nothing yet)
- Fix bad bug in `log.child({...}, true);` where the added child fields **would
be added to the parent's fields**. This bug only existed for the "fast child"
path (that second `true` argument). A side-effect of fixing this is that
the "fast child" path is only 5 times as fast as the regular `log.child`,
instead of 10 times faster.
## bunyan 0.6.7

View file

@ -239,19 +239,24 @@ function Logger(options, _childOptions, _childSimple) {
// Fast path for simple child creation.
if (parent && _childSimple) {
// Single to stream close handling that this child owns none of its
// streams.
// `_isSimpleChild` is a signal to stream close handling that this child
// owns none of its streams.
this._isSimpleChild = true;
this._level = parent._level;
this.streams = parent.streams;
this.serializers = parent.serializers;
this.src = parent.src;
this.fields = parent.fields;
var fields = this.fields = {};
var parentFieldNames = Object.keys(parent.fields);
for (var i = 0; i < parentFieldNames.length; i++) {
var name = parentFieldNames[i];
fields[name] = parent.fields[name];
}
var names = Object.keys(options);
for (var i = 0; i < names.length; i++) {
var name = names[i];
this.fields[name] = options[name];
fields[name] = options[name];
}
return;
}