2

I absolutely love the Java 8 features, because of readability, but I am concerned about performance degrading that they might cause. While it probably doesn't affect the application speed in the long run due to JIT, it might affect memory usage, which is one of my primary concerns. IntelliJ IDEA provides the option to convert Stream API usages to loops (for single usage). Is there a way to inline all these features every time I build my project?

For example, if I use find("my-record").ifPresent(records::add), I would expect these changes: signature of Optional<Record> find(String) to be converted to (Nullable) Record find(String) and the method call to be converted to a null-check.

Note: I know that there are tons of subtle differences between, say, streams and loops, but I design my code not to rely on details of implementation.

PhilipRoman
  • 1,136
  • 10
  • 20
  • 2
    "probably [...] it might affect memory usage" do you have any indication that this really applies (in a significant way) to you concrete situation? – reto Jan 15 '18 at 07:12
  • 2
    Streams tend to create quite a few objects, loops and 'if' create none, right? Besides, there are several benchmarks (which seem believable) where small-sized streams can be >5 times slower. – PhilipRoman Jan 15 '18 at 07:13
  • 1
    That would mean this feature would adjust both the return type of the called method from `Optional` to `Record` and all calls to it. That's a questionable approach since it relies on all dependencies of the callee to be built using that feature. – daniu Jan 15 '18 at 07:15
  • I see... if such feature would be possible, about how much time would it add to the building process? I don't see any similar issues with Streams though. – PhilipRoman Jan 15 '18 at 07:16
  • 3
    Do you actually have a performance problem? Looks like a case of premature optimization. – Andreas Jan 15 '18 at 07:24
  • It might be premature, but I've noticed in my past projects that memory usage and GC pauses are the two limitations (one causing the other) which force me to rent better hardware. – PhilipRoman Jan 15 '18 at 07:30
  • 3
    I would worry about what that `find` method actually does rather than how it returns the result. You’re surely looking at the wrong end. – Holger Jan 15 '18 at 07:46
  • This is not actual code – PhilipRoman Jan 15 '18 at 07:50
  • There are several similar questions: https://stackoverflow.com/questions/24923040, https://stackoverflow.com/questions/2096361, https://stackoverflow.com/questions/7772864, https://stackoverflow.com/questions/35601841, https://stackoverflow.com/questions/3228141 – tkruse Jan 16 '18 at 02:39

1 Answers1

1

As explained in all the similar questions I linked to from my comment, the Java8 SDK itself does not offer ways to add such optimizations during compile time.

The Java 9 "Ahead-of-time" compiler may inline more aggressively.

The optimization techniques envisioned here include, but are not limited to: Fast lookup of both JDK and application classes; early bytecode verification; aggressive inlining of, e.g., lambda expressions, and other standard compiler optimizations; [...]

Also ProGuard does some inlining:

In the optimization step, ProGuard further optimizes the code. Among other optimizations, classes and methods that are not entry points can be made private, static, or final, unused parameters can be removed, and some methods may be inlined.

tkruse
  • 10,222
  • 7
  • 53
  • 80
  • Thank you for the answer, I was more interested in _object inlining_ as opposed to simple function inlining. But the AOT definitely looks promising, at least for some cases (I'm aware of it's shortsomings). And thanks for ProGuard - never used it before, but seems like a useful tool. – PhilipRoman Jan 16 '18 at 17:27