Android Question Optimization of generated code

Alessandro71

Well-Known Member
Licensed User
Longtime User
One question, i think only Erel can answer.

In the generated object code, sub and constants not actually used (as indicated by the IDE) are still included, or the compiler/optimizer prunes unused code?

The reasoning behind this question is the usage of common modules across different projects, where project A uses sub 1,2, and 3, while project B uses only sub 1.

All subs are in the same code module.
 

DonManfred

Expert
Licensed User
Longtime User
B4X does not exclude anything. You can use different builconfigurations to exclude something not needed for this build.
 
Upvote 0

Sandman

Expert
Licensed User
Longtime User
I haven't tried this myself, but I imagine it would be possible to test easily by creating an unused sub with a variable containing a unique string. Then compile (with no obfuscation) to app, then decompile the apk and finally do a simple text search in the decompiled source.

Presence of the string could probably be considered a strong indication whether things are evicted or not?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
since there's a java compiler at the end of the chain, eventual eviction may be performed by java
do you miss something after compilation?

Java does not exlude anything when compiling.

If you WANT to exlude code in app x then you should use conditional compiling an encapsulate the code which you want to exclude.

B4X:
#if myspecialbuild
' this code is included in the build myyspecialbuild'
#end if

 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Looking at that would only get you a partial answer as some things might be changed/discarded during the Java compilation.
A string defined in Main you´ll also find in the main.java.

Everything discarded is in a conditional compiling block
 
Upvote 0

Sandman

Expert
Licensed User
Longtime User
Everything discarded is in a conditional compiling block
Granted, I know absolutely nothing about Java, but aren't there any Java compilation flags that makes Java strip out unused code? (...goes investigating on the net for five minutes...) Nope, seems there aren't any flags or anything like that, as best as I can tell. Which means that the suggestion by @rosippc64a is quite sufficient.

I'm also fairly certain that @Erel could say something with certainty here, he lives and breathes this stuff every day. :)
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This is a preoptimization discussion (watch the performance video tutorial for more information).

In the generated object code, sub and constants not actually used (as indicated by the IDE) are still included, or the compiler/optimizer prunes unused code?
They don't have any effect on performance. The JVM is very sophisticated and will only load whatever it needs.
 
Upvote 0

Sandman

Expert
Licensed User
Longtime User
This is a preoptimization discussion
Just for completeness, there is one edge case that I can think of that isn't related to preoptimization. I can very well see myself doing the mistake of having two subs like this:

B4X:
' The one used in production
Private Sub ConnectToServer
    dim username as String = "myApp"
    dim password as String = "kjdsfhsdkjfhsdkfj"
    dim address as String = "32.43.54.118"
    Connect(username, password, address)
End Sub

' The one used while testing
Private Sub ConnectToServerDISABLED
    dim username as String = "myApp"
    dim password as String = ""
    dim address as String = "16.20.42.1"
    Connect(username, password, address)
End Sub

And then just rename-flip them as I test things back and forth while developing. And just leaving the testing server sub in the code when compiling the release app, mistakenly thinking that it will be removed. Granted, I have not made this mistake, but before this thread I think it would be quite possible for me to make it. Good thread. :)

(All IP numbers and credentials are fake.)
 
Upvote 0
Top