Skip to content

Commit

Permalink
Reduce performance overhead of verbose logging
Browse files Browse the repository at this point in the history
1) Log functions now accept function as an argument, which is lazily executed only when the particular logging level is actually enabled
2) Serialization of component tree in verbose logging is now done lazily using the above option
  • Loading branch information
hejtmii authored and ibash committed Apr 16, 2022
1 parent ef6e96d commit a90174c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/lib/logging/EventStrategyLogger.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ class EventStrategyLogger extends Logger {
this.verbose(
this.nonKeyEventPrefix(componentId),
'New component options:\n',
printComponent(componentOptions)
() => printComponent(componentOptions)
);
}

logKeyHistory(keyHistory, componentId) {
this.verbose(
this.keyEventPrefix(componentId),
`Key history: ${printComponent(keyHistory.toJSON())}.`
() => `Key history: ${printComponent(keyHistory.toJSON())}.`
);
}
}
Expand Down
12 changes: 11 additions & 1 deletion src/lib/logging/Logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,17 @@ class Logger {
return;
}

this[level] = ['debug', 'verbose'].indexOf(level) === -1 ? console[level] : console.log;
const logToConsole = ['debug', 'verbose'].indexOf(level) === -1 ? console[level] : console.log;

this[level] = (...args) => {
const materializedArgs = args.map(arg =>
// Function arguments are evaluated lazily to reduce performance overhead
typeof arg === 'function'
? arg()
: arg
);
logToConsole(...materializedArgs);
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/matching/ActionResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class ActionResolver {
this.logger.verbose(
this.logger.keyEventPrefix(componentId),
'Internal key mapping:\n',
`${printComponent(keyHistoryMatcher.toJSON())}`
() => `${printComponent(keyHistoryMatcher.toJSON())}`
);

const keyHistory = this._eventStrategy.keyHistory;
Expand Down
6 changes: 3 additions & 3 deletions src/lib/strategies/AbstractKeyEventStrategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class AbstractKeyEventStrategy {
this.logger.verbose(
this.logger.nonKeyEventPrefix(this.componentId, { focusTreeId: false }),
'Registered component in application key map:\n',
`${printComponent(this.componentTree.get(this.componentId))}`
() => `${printComponent(this.componentTree.get(this.componentId))}`
);

return this.componentId;
Expand All @@ -176,7 +176,7 @@ class AbstractKeyEventStrategy {
this.logger.verbose(
this.logger.nonKeyEventPrefix(componentId),
'Registered component mount:\n',
`${printComponent(this.componentTree.get(componentId))}`
() => `${printComponent(this.componentTree.get(componentId))}`
);
}

Expand All @@ -191,7 +191,7 @@ class AbstractKeyEventStrategy {
this.logger.verbose(
this.logger.nonKeyEventPrefix(componentId),
'De-registered component. Remaining component Registry:\n',
`${printComponent(this.componentTree.toJSON())}`
() => `${printComponent(this.componentTree.toJSON())}`
);

if (this.componentTree.isRootId(componentId)) {
Expand Down

0 comments on commit a90174c

Please sign in to comment.