-
Notifications
You must be signed in to change notification settings - Fork 8
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
Allow for compiler optimizations and logging configuration #300
Conversation
307c3e2
to
07abf7e
Compare
07abf7e
to
a006c5d
Compare
While I was at it I changed |
025f9fb
to
8af5be1
Compare
OK just runnin it back to make sure I get it, you are adding a conditional here based on a flag so the compiler can be smart and just toss out branches that will never be executed. Without this, things are always getting allocated on the heap even if the client wasn't running debug mode? I think my one big question is how does the compiler optimize a runtime flag? In your playground example (btw pretty dope that godbolt asm stuff is just built right in to rust playground) won't the compiler pick up on the |
Right, the compiler is smart enough to remove branches that will not execute. Even though the log level is set at runtime, it is known for the entire duration of This may already be common knowledge but I checkout from a detached head so there's no local branch hanging around. In this case:
|
Also just realized I normally compile all examples in release, so might need to slap a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK 8af5be1
Was a little thrown by the optimizations, but makes sense that a consistent conditional guard will protect against unnecessary log allocations.
Closes #298
The
format!
macro requires a heap allocation, and the&str
references are converted toString
when sent as aLog::Dialog
message. Because some applications might be more memory hungry than others, there should be a way to stop the node from allocating theseString
entirely. I thought about having this as a crate feature, but the language bindings used by BDK would not be able to make use of this performance gain, since they ship a binary. I think I strike the best of both worlds here and use a runtime variable that can still be optimized away by the compiler.To prove this works, I made a simple example. Check out this in the Rust playground and select "ASM" using the three dots on the top left.
The compiler can remove the branching and either completely ignore the expression, thus not allocating the
String
, or execute the expression and send a debug message. Only downside to this approach is theLog::Dialog
is still a member ofLog
even though we don't need it. Not sure how to get rid of it without introducing compile-time options though.