Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

[pr] minimize log message string allocations #117

Merged
merged 4 commits into from Apr 23, 2019

Conversation

stephen-palmer
Copy link
Contributor

  • short-circuit debug logging logic when logging level is below debug, to eliminate associated string allocations
  • in non-cluster mode, eliminate double allocations of log message strings by removing redundant template literal
  • in cluster mode, use cached string prefixes and eliminate double allocations of log message strings
  • eliminated use of the LOG_TEST level in production code. Just a handful of log lines were using it - they were redistributed to INFO or DEBUG.

Copy link

@pullrequest pullrequest bot left a comment

Choose a reason for hiding this comment

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

✅ A review job has been created and sent to the PullRequest network.


Check the status or cancel PullRequest code review here - or - cancel by adding [!pr] to the title of the pull request.

@coveralls
Copy link

coveralls commented Apr 12, 2019

Coverage Status

Coverage remained the same at 95.0% when pulling c35ad57 on dev/reduce-log-allocs into aaa1493 on master.

Copy link

@pullrequest pullrequest bot left a comment

Choose a reason for hiding this comment

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

Looks good to me.

Wish there was a cleaner way over adding shouldLog a bunch but can't think of anything off the top of my head.


Was this helpful? Yes | No

Copy link

@pullrequest pullrequest bot left a comment

Choose a reason for hiding this comment

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

I added one idea as an in-line comment, but I would not be surprised if it's something you've already considered.


Was this helpful? Yes | No

lib/helpers.js Outdated
if (lvl <= logLevel) {
const prefix = cluster.isMaster ? "[Cluster:M] " : `[Cluster:${cluster.worker.id}] `;
console.log(`${prefix}${msg}`);
if (!shouldLog(lvl)) return;
Copy link

Choose a reason for hiding this comment

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

Nice use of a guard clause

import.js Outdated
@@ -74,7 +74,7 @@ async function importTransactionFile(filePath, addressString, defaultPort) {
}

try {
helpers.log(consts.LOG_DBG, `Begin transaction for ${helpers.GUIDBufferToString(guid)}-${hash.toString('hex')}`);
helpers.shouldLog(consts.LOG_DBG) && helpers.log(consts.LOG_DBG, `Begin transaction for ${helpers.GUIDBufferToString(guid)}-${hash.toString('hex')}`);
Copy link

Choose a reason for hiding this comment

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

Have you tried to see the memory implications of using an arrow function to wrap the strings? I assume you probably did try that. If that worked it might make this a little cleaner and prevent having to repeat the log level twice (it'll be easy to accidentally change one without changing the other in the future).

If the arrow function is never called because of the log level the string wouldn't be evaluated. But I'm not sure how the memory usage compares to what you're currently experiencing. It would be something I would try if you haven't already.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a good suggestion - I'm going to look into it. I'll probably write a little test app to profile memory using the two approaches, against the baseline.

@stephen-palmer
Copy link
Contributor Author

I did some profiling on this and found that the string templates are actually not processed/de-referenced until they are used by console.log() ... therefore this optimization is largely unnecessary. There are a couple of things in this PR I want to keep regardless.

Separately, in the process, I found a more important optimization that can be done to specifically address performance and memory usage when downloading a very large number of files. I'll open a separate PR for that one.

@stephen-palmer stephen-palmer merged commit bef32e0 into master Apr 23, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants