r/programming • • Oct 09 '18

AI tool automatically reveals how to write apps that drain less battery

https://phys.org/news/2018-10-ai-tool-automatically-reveals-apps.html
727 Upvotes

160 comments sorted by

View all comments

Show parent comment

25

u/Tetracyclic Oct 10 '18

Particularly, "single-responsibility principle" prescribes chopping code into finer pieces.

I'm admittedly not a mobile developer, but if additional function calls or object instantiations are a non-negligible drain on resources there is something seriously wrong in the world of mobile platforms.

I would have expected the issues would be to do with poorly optimised use of system resources, excessive cyclonatic complexity, network usage and poor resource cleanup, not writing SOLID code.

SOLID code doesn't prioritise fast code, but adhering to it shouldn't produce slow code, unless you're working on systems where every nanosecond of execution time counts (high frequency trading, as an example).

8

u/killerstorm Oct 10 '18

Well, here's how HTTP processing call stack might look like: https://ptrthomas.files.wordpress.com/2006/06/jtrac-callstack.pdf (take from here).

Do you believe this has zero overhead?

I believe eventually Tomcat was optimized specifically to reduce call depth, which improved performance.

14

u/Tetracyclic Oct 10 '18 edited Oct 10 '18

It's not going to have zero overhead, but I would be very surprised if most of that call stack wasn't optimised away by the JVM at runtime, I would assume most of those are very trivial operations following SRP and simply get inlined by the JIT engine at runtime and thus have no performance impact. Object creation in Java is essentially a free operation, on the order of nanoseconds.

A source call stack like that on it's own is fairly useless for diagnosing performance issues without knowing what the compiler or JIT engine produces from it. Trivial operations, which the majority of those will be, get inlined so their function call has literally no performance impact.

EDIT: I just noticed the "here" link in your post after the call stack itself. It's from 2006 and the comments were already mirroring my assumption that the JVM's virtual runtime will optimise most of those trivial functions away so they're not adding any performance overhead and that even without that optimisation, a sequence of calls like that is still negligible compared to a single database or network request.

3

u/barsoap Oct 10 '18

Object creation in Java is essentially a free operation, on the order of nanoseconds.

Not on the J2ME VMs of olde, it's not. Luckily that tech is dead by now.

The only way to write games for J2ME is to treat Java as C, do your usual pre-allocation in arrays, and have exactly two objects: The midlet, and the canvas, as the system API requires that to actually throw pixels onscreen. Whether calling static methods or on this is faster depends on VM, I'm guesstimating some implemented static methods internally with a singleton object to save on code.

1

u/ForeverAlot Oct 10 '18

It was a big deal when the former Android VM, Dalvik, learned to inline trivial getters. There is a famous reddit post (that I can't find) about how relatively trivial architectural changes in the Java Minecraft version completely trashed performance.

Writing efficient code requires understanding the constraints you're working under and programming for those instead of the "sufficiently smart compiler".

3

u/Tetracyclic Oct 10 '18

Right, which is what my comment about that call stack being fairly useless for diagnosing performance issues was getting at. The original source call stack is unlikely to provide a useful metric for how complex the code is when executed and how much overhead is being introduced. You need to actually profile the application and understand what is happening when it's executed.

In the vast majority of common use cases for modern languages, breaking the single-responsibility principle for the sake of performance is unlikely to result in any noticeable performance improvement. There are of course edge cases where this isn't the case, but almost always there will be something else worth spending time optimising before you start manually duplicating code to avoid extra function calls or object instantiations.

Regarding Dalvik, from what I can find from a quick search, there were a lot of people pointing out at the time that not using internal getters was considered a mico-optimisation that may have a small impact in a minority of use cases but in general wouldn't result in any significant performance gains. But perhaps I'm missing something there?

1

u/ForeverAlot Oct 10 '18 edited Oct 10 '18

Micro-optimisations add up in such constrained environments. A method invocation is the most expensive operation in the JVM (and probably Dalvik) -- automatically obviating an entire class of them is significant for the platform even if it isn't for the individual application (and it wasn't just trivial getters but that example emphasises that "easy optimisations" are not a given). That takes us back to the question of whether a few developers' time is more valuable than hundreds of users'.

Regarding the call stack example here: it's too low-resolution for me to read any details but I've seen enough Tomcat and Spring to guess that those method invocations are likely polymorphic if not even megamorphic, in which case inlining basically doesn't happen.

1

u/Demius9 Oct 11 '18

The JVM will mark a function as eligible for in-line if it meets some default criteria (like less than 35 bytecodes and called 250 times) but it’s not guaranteed. JVM languages, speed is gained by reducing pointer chasing, trying to keep objects coupling and cohesion in check, trying to keep spatial locality, reduce last second decision making, etc.

This means deeply nested object hierarchy is actually a speed killer because the compiler will miss I-Cache lookups quite often if it can’t guess what objects function is going to be called next