Skip to content

refactor: call tracer only after isEnabled check #1500

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

Merged
merged 3 commits into from
Sep 7, 2018

Conversation

sis0k0
Copy link
Contributor

@sis0k0 sis0k0 commented Aug 27, 2018

original PR: #958

@sis0k0
Copy link
Contributor Author

sis0k0 commented Aug 31, 2018

test

traceLog(`NativeScriptAnimationPlayer.play`);
if (isLogEnabled()) {
traceLog(`NativeScriptAnimationPlayer.play`);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

@sis0k0 just curious - couldn't this perhaps just be called in tracer itself to avoid having to have these conditionals all over the codebase? ie:

import { isEnabled } from "tns-core-modules/trace";

export function animationsLog(message: string): void {
    if (isEnabled()) {
      write(message, animationsTraceCategory);
   }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The disadvantage of having the check inside the tracer itself is that this will result in two function calls - one for animationsLog and one for isEnabled.

Copy link
Contributor Author

@sis0k0 sis0k0 Sep 1, 2018

Choose a reason for hiding this comment

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

Also if the check was inside the animationsLog function and we invoked animationsLog("something" + random), we would have the overhead of the string concatenation as well.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for explanation makes sense :) Perhaps there’s a possibility we could also add something to webpack config to strip these all out in production mode as well to coincide with this pr. Ideally with the whole trace enabler control we should probably have ways that devs can strip them when desired as well since keeping core libs slim and performant as possible is always highest goal (which thank you for attention to that here!).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a great idea! 10x :)

@NathanWalker
Copy link
Contributor

For anyone that comes across this PR and is also wondering @NathanaelA provided an additional explanation which is very helpful:


Performance. Take this example :

if (trace.isEnabled()) {  
  var generateSomeData = doA()+"-"+doB();
  trace.log("We are tracing some data", generateSomeData, moreInfo ? dox() : doy());  
}

By calling trace.isEnabled() and getting back a false it skips:

  1. Setting up another call into the trace system.
  2. Elimnates doA, doB, and a variable allocation that is NOT needed.
  3. Allocating stack space to pass in 3 values
  4. evaluating moreInfo
  5. eliminate DoX() or doY() calls
  6. Cleanup the stack on exit of each of those functions.
    In addition if you use webpack; you should be able to make trace.isEnabled() a nop instruction which would then allow webpack strip out the entire if statement, giving you even more performance as your JS file is smaller. 🙂

It is always recommended to check if you can log before blindly calculating and logging. Logging can be expensive; especially if any of the do?() functions are expensive...

For those who have trace logging enabled in that area to debug something; the ms time used that occurs to run the isEnabled() check is tiny compared to the actual log command. So they don't experience any slow down by doing it that way..

@sis0k0
Copy link
Contributor Author

sis0k0 commented Sep 7, 2018

Thank you, @NathanWalker, for taking the time to explain this in detail!

@sis0k0 sis0k0 merged commit 70d5457 into master Sep 7, 2018
@ghost ghost removed the in progress label Sep 7, 2018
@sis0k0 sis0k0 deleted the sis0k0/tracer-is-enabled branch September 7, 2018 09:00
NathanWalker pushed a commit to NathanWalker/nativescript-angular that referenced this pull request Oct 10, 2018
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.

3 participants