-
-
Notifications
You must be signed in to change notification settings - Fork 116
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
A more efficient alternative syntax #112
Comments
I really like the idea. However changing the syntax might break compatibility in a way that might be really inconvenient to migrate. In addition, issue #34 also discuss approaches to reduce the overhead. |
I don't like logger.debug?("Some $expensive message!") I'm not convinced this is better than using inline functions. Actually I think it's worse because you're relying on a specific compiler implementation. Is this how kotlin/native works? What if future versions of kotlin implement this differently? We would need a contract that kotlin will definitely never evaluate the string eagerly. We get that with a lambda. |
Closing the issue as it doesn't seem we're going to do that change ATM. |
@dnut logger.debug?("Some $expensive message!") One needs to use this syntax instead logger.debug?.invoke("Some $expensive message!") I think leveraging I do not think we are relying on some internal magic here. In the end, I'm also against this solution. The reason is simple: the lazy nature of the On the other hand Yes, logging is actually an ideal scenario where call-by-name is a great fit - it leads to the simplest API and easiest use. But even then I consider call-by-name hideous and counter-productive mechanism. Yes, it gives immense power (in Scala it is possible to create PS: Un/fortunately, without call-by-name, logging in Kotlin will always feel +- hacky as every solution will be an attempt to simulate call-by-name. |
I'd like to propose a syntax which is more efficient than current implementation.
Current Syntax
logger.debug { "Some $expensive message!" }
Is great because it becomes somewhat:
Decompiled code for current syntax
The truth is that using a decompiler (such as Jad), code produced looks like this:
Therefore, even when debug level is disabled, here are the penalties:
kotlin.jvm.functions.Function0
which makes final code bigger.debug()
will ignore it.Proposed Alternative New Syntax
Now compare with the following syntax:
Where
logger
is of type:Decompiled code for proposed syntax
Decompiled code will look like this:
Notice that nothing is allocated or assigned. Simple plain check against null before any cost.
The text was updated successfully, but these errors were encountered: