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

Add option to enable/disable numbers cache #29

Merged
merged 7 commits into from
Jun 21, 2017

Conversation

kirilknysh
Copy link
Contributor

Fixes #28

Add a flag cacheNumbers to control the numbers cache. Disabling the cache may save up to 14Mb of memory (node process).

Performance (benchmarks/writeToStream.js, average of 10 runs, Packet/s):

  • before changes: 386675.1239634539
  • after changes (w/ cache): 376886.5427892937
  • after changes (w/o cache): 353188.7654481942

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

Can you add docs as well?

writeToStream.js Outdated
@@ -41,6 +41,7 @@ function generate (packet, stream) {
return false
}
}
generate.cacheNumbers = true
Copy link
Member

Choose a reason for hiding this comment

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

I think we can use a setter here, and if it is setted to false, clear numCache.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Then it would require:
either load and generate cache and then potentially clean it
or set cacheNumbers always (either true or false) to trigger the selection of required writeNumber.

Copy link
Member

Choose a reason for hiding this comment

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

You can populate the cache when writeToStream is called.

writeToStream.js Outdated
@@ -520,7 +521,7 @@ function writeString (stream, string) {
* @api private
*/
function writeNumber (stream, number) {
return stream.write(numCache[number])
return stream.write(numCache.get(number, generate.cacheNumbers))
}
Copy link
Member

Choose a reason for hiding this comment

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

I think we can have two functions, writeNumberCached and writeNumberAllocate, and we can assign them to writeNumber in the cacheNumbers setter.

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

LGTM, how are perf looking now?

@kirilknysh
Copy link
Contributor Author

  • after changes w/ cache: 362711.861420035
  • after changes w/o cache: 328807.3007557509

Memory consumption is the same as in the PR description

writeToStream.js Outdated
@@ -520,7 +535,7 @@ function writeString (stream, string) {
* @api private
*/
function writeNumber (stream, number) {
return stream.write(numCache[number])
return stream.write(getNumber(number))
}
Copy link
Member

Choose a reason for hiding this comment

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

I think we can replace that function, and have numCache ported here. We should really avoid that getNumber() function call.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure I understand you correctly. We anyway need a function call in case of buffer allocation (w/o cache). So getNumber is a function only for common interface of getting buffers.

Copy link
Member

Choose a reason for hiding this comment

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

I've added the change that I had in mind, have a look!

Also, can you add some unit tests for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, will add

@mcollina
Copy link
Member

Can you add a unit test?

@kirilknysh
Copy link
Contributor Author

Yes, it's in progress. Sorry, takes more time than expected

@mcollina mcollina merged commit a0e3f1d into mqttjs:master Jun 21, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants