Turns outclass Foo {boolean bar;}results in fewer compiled bytes than:class Foo {boolean bar = false;}There's an explanation as to why this is the case here:
--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
You received this message because you are subscribed to the Google Groups "Chromium-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev+unsubscribe@chromium.org.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev...@chromium.org.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev...@chromium.org.
What Anthony is pointing out, and I agree with, is that in the example given the behavior is subtle and surprising even in the case where the variables are explicitly initialized. i.e., as a non-Java programmer, my reading of the stack overflow thread is that calling a method in the superclass constructor that is overridden by a subclass to mutate some instance state is an anti-pattern, full stop. Maybe this is common in Java though...if Java programmers have grown to rely on this behavior, then it seems like we can't actually give the recommendation in the OP (which is kind of what torne@ was saying).
In the case where someone writes a subclass like this without knowing it and the ivar is initialized explicitly (either in the superclass or in the subclass), is it not just as likely that that someone will be surprised by the fact that the ivar is back to its default value at the end of initializing the object?
Or are people actually familiar with this in Java and would expect that? (in which case it seems strange to even structure the code that way in the first place...)
If you actually want to make that gotcha impossible, then it's, sadly, worse than just "document non-final methods called in constructors", because:1) you have to apply that transitively: also all non-final methods called by final methods called in constructors, and so on2) you also have to apply that to any calls in the constructor to *other* functions (even final ones) outside the current class that take "this" as a parameter, because the other function might well call a non-final method on "this". And then apply *that* transitively as well.I'm not sure that trying to have a policy about constructor behaviour is going to help a lot, really.I don't really have a conclusion here personally; I do understand why we want to save all the bytes we can on android (binary size matters a lot), and I think a proguard optimisation that strips these redundant initialisers is interesting to pursue, and I don't even think it's that likely that it will break anything in practise... but it does make my skin crawl a bit to propose transforms that might affect correctness.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev+unsubscribe@chromium.org.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev+unsubscribe@chromium.org.
Do not disturb, ,
.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev+unsubscribe@chromium.org.
I think that the subtleties discussed in this thread may be orthogonal to whether or not we should initialize default values in Java.I've created a CL (https://codereview.chromium.org/2548013002/) that removes most field initializers in Java, saving us 7.62 kb. We can update the style guide (https://sites.google.com/a/chromium.org/dev/developers/coding-style/java) to reflect this preference - does anyone have any objections to me landing this?
On Fri, Oct 14, 2016 at 10:54 AM, Andrew Grieve <agr...@chromium.org> wrote:
When I first tested this theory out, I did it on a release binary and did see the (uncompressed) dex size shrink a bit. Proguard is a crazy thing though, so I suppose it's possible that there's this much randomness?
On Wed, Oct 12, 2016 at 3:01 PM, Vaibhav Pithadiya <vaibhav....@gmail.com> wrote:
Do not disturb, ,
.
On Thu, 13 Oct 2016 12:23 AM 'Holger' via Chromium-dev, <chromi...@chromium.org> wrote:
Has anyone tested this with an apk that is proguarded?--
I just wanted to see how much this could save in Chrome, and on a proguarded debug build I removed slightly more than 40 default initializers (which set variables to 0 or false)
The result however shows that the apk size actually increased (possibly due to proguard being better at optimizing initialized variables, though that's just a theory)
ls -al
Before: -rw-r--r-- 2 myusername domain^users 64690123 Oct 12 11:21 out/GN-Debug/apks/AlteredChromePublic.apk
After: -rw-r--r-- 2 myusername domain^users 64711075 Oct 12 11:44 out/GN-Debug/apks/AlteredChromePublic.apk
--> The apk size increased by a good 20 kB
--> Has anyone actually tried this on a Release build of Chromium and seen benefits? Are there some gotchas I should be aware of? (E.g. I did this for static and non-static members alike. Was that a bad idea?)
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
---
You received this message because you are subscribed to the Google Groups "Chromium-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev...@chromium.org.
--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
---
You received this message because you are subscribed to the Google Groups "Chromium-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev...@chromium.org.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev+unsubscribe@chromium.org.
Just out of curiosity: 7.62 kb on a proguarded apk or on a multidexed apk?
"Personally, I also find that removing the initialization makes the code lessreadable, since it forces the reader to hunt around for where it is initialized,whereas e.g. explicitly initializing an object to null helpfully implies that itis nullable."
To me it also makes reading the mixed C++/Java codebase easier: if I see a C++ int variable that is not initialized my instinct is it's a mistake. In Java I have to remember that it's initialized and whether the default value of 0 is correct.
I'd support removing the explicit initialization with default values if the binary size wins would justify it.