Perf: Use autoclosures to prevent String construction#18
Merged
shmuelk merged 4 commits intoKitura:masterfrom Jan 5, 2017
djones6:autoclosures
Merged
Perf: Use autoclosures to prevent String construction#18shmuelk merged 4 commits intoKitura:masterfrom djones6:autoclosures
shmuelk merged 4 commits intoKitura:masterfrom
djones6:autoclosures
Conversation
...until we know a log message will be emitted
...until we know a log message will be emitted
Contributor
Author
|
I confirmed that autoclosures are implicitly non-escaping and there is no need to reference |
Collaborator
|
Merging as David has proved it doesn't affect the API (i.e. doesn't require the user to add self.) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Change the
Logfunctions to use@autoclosure, which automatically wraps the log message in a closure to delay evaluation of the String until we know the message will be logged.Capture semantics
There is no change required to existing log messages, as the closure is implicitly non-escaping. For example, for an existing log message:
There is no requirement to add
self.to the reference topoolhere, as would be the case if the signature was@escaping @autoclosure.Performance
This makes little difference when no logger is present, or at a conservative log level. At
.verbose, it costs a little performance (around 1% for 'Hello World') as there is no saving (all messages are logged), but we pay for the creation of the closures.However, our current code contains few log messages overall, and those we do have are relatively cheap to construct. Making this change would enable us to add log messages in the future that are expensive to construct, but with no penalty unless the log level actually emits them.
As an example, here's what happens with an artificially expensive log message logged with
Log.debugand a log level of.warn:Behaviour changes
This does not break the existing API, but it does introduce a subtle (intentional) difference in behaviour. Given the log statement:
Today, the function
countTheItems()will be invoked regardless of whether the message is logged. With autoclosures, it will only be invoked if the message is logged. It may be worth documenting this, as it is non-obvious to the caller, and although it would be questionable to rely on side-effects of evaluating a log message, someone might trip over it.