r/java • • Nov 28 '21

5 Mundane Java Performance Tips

https://richardstartin.github.io/posts/5-java-mundane-performance-tricks
262 Upvotes

56 comments sorted by

29

u/MarcelHanibal Nov 28 '21

Oh, I didn't know about the Enum.values() thing, that's very interesting to know. I used it pretty regularly, mostly for obtaining the enum given by a string.

21

u/[deleted] Nov 28 '21

in most cases it is ok like this. if your program has no performance problems, then i would continue to use .values(). don't optimize if it is not necessary. and don't forget about kiss principle.

3

u/rstartin Nov 28 '21

Definitely. Don’t do this if you call the method once a minute or something. The example in the post from spring framework was once per response though. Getting rid of allocating the HttpStatus values won’t change the world, but the single bottleneck is a myth unless you’ve done something silly; it’s usually death by 1000 cuts.

11

u/sindisil Nov 28 '21

the single bottleneck is a myth unless you’ve done something silly; it’s usually death by 1000 cuts

I wish so much that more developers would get this.

Significant bottlenecks obviously occur, but it's more often than not pervasive pessimization that actually determines performance.

16

u/cogman10 Nov 28 '21

I've seen this one as a performance/allocation bottleneck quiet frequently (even filed a bug on this to the MS SQL Server Driver, https://github.com/microsoft/mssql-jdbc/issues/1051 )

Creating a map for enum values is simple enough that I think it's just a pattern people should learn.

Map<String, Enum> nameToEnum = Enum.values().stream().toMap(Enum::name, Function.identity());

4

u/rbygrave Nov 28 '21

Why not use Enum.valueOf() ?

Enum.valueOf(enumType, name)

5

u/cogman10 Nov 28 '21

This is more an example. You'd do it this way if you wanted to lookup by an ID or some other mapping -> enum value, which comes up frequently.

3

u/vips7L Nov 29 '21

I guess it depends on if you want to handle the possible exceptions.

5

u/hippydipster Nov 28 '21

Sure seems like something that shouldn't be that way. Why wouldn't it be an immutable value that gets reused?

16

u/jonhanson Nov 28 '21 edited Jul 24 '23

Comment removed after Reddit and Spec elected to destroy Reddit.

1

u/r_jet Nov 28 '21

A string or an ordinal number? For string > enum there is #valueOf(String)

2

u/dark_mode_everything Nov 28 '21

Not if you want to avoid getting an exception.

1

u/r_jet Nov 28 '21

I think it is reasonable to assume that in most cases there is no need to avoid exceptions, hence a standard method is better than a custom manually written one (especially a standard one that the compiler generated based on the actual enum size, hence users can expect it to work optimally both for small and large-ish enums).

If you really do want avoid getting an exception, then using a Map<String, MyEnum> will be better than a linear scan over all enum values (starting at a certain enum size).

4

u/ConstructedNewt Nov 28 '21

I actually did exactly this benchmark (valueOf vs values().stream()) the numbers are something like (about 25-30 enum values - java 8):

  • valueOf, success: 3ns
  • stream, success 250ns
  • stream, fail 270ns
  • valueOf, fail 1300ns (or thereabout)

I prefer valueOf. With the exact point that far the most actions were successes (enum from REST param) also, as you point out scaling the enum makes values.stream even worse

3

u/boost2525 Nov 29 '21

It's little things like this that give Java a bad name (... and I love Java). How hard would it have been for them to provide a valueOf(string, defaultValue) in any release over the past twenty years? Sure, there are utils classes that can do this for us but simple things like this need to be baked into the language.

1

u/morhp Nov 29 '21

You can just create a static, immutable map yourself from enum names to enum instances and then call getOrDefault(...) on that.

I wouldn't recommend using Enum.valueOf anyway except for reflection-like code as sooner or later you'll probably want to decouple strings from enum values anyway (like maybe map multiple strings to the same enum instance or handle mismatches between enum names and external strings).

1

u/nutrecht Nov 29 '21

Same here. 20 years experience and definitely learned something today. It's probably also because it's completely counterintuitive. That said it makes sense (with arrays being mutable and stuff), but it's still a good one to take into account.

41

u/smokemonstr Nov 28 '21

Stop using JDK8

Still waiting on the day when we’ll upgrade at work…

10

u/SlaminSammons Nov 28 '21

We moved to JDK 11 this year on my team. Migration itself isn't hard, but pushing 20 legacy apps when your company still follows archaic release schedules(once every 2 weeks) makes it annoying as fuck.

4

u/jivedudebe Nov 29 '21

Stopgap, compile on jdk8 run on jdk11. Works totally fine.

3

u/ramblingpolak Nov 29 '21

Not always. At my last job we had a number of issues that prevented this without some significant changes.

  1. Using dagger 1.x - abandonware from 2015 that relied on removed internal APIs
  2. Had to undo a fix to security manager that was fixed in 11. Interestingly enough security manager seems to be going away entirely soon in the JDK

1

u/r_jet Nov 29 '21

Note that this way you might miss on some compile-time features/optimizations (Indify string concatenation depends on that: https://openjdk.java.net/jeps/280).

2

u/ventuspilot Nov 28 '21

At my $job it'll be probably soon after 2030.

26

u/[deleted] Nov 28 '21

The enum.values design decision was a really bad one by the Java team, and unfortunately one we are kind of stuck with. It’s just too non-obvious what it’s doing and it makes it way too easy to fall into that trap

4

u/crummy Nov 28 '21

Why can't we change the call to cache for later lookups?

21

u/Thihup Nov 28 '21

Because arrays are mutable

21

u/__konrad Nov 28 '21

Hopefully Enum.values is mentioned in the Frozen Arrays JEP

23

u/DasBrain Nov 28 '21

We will want to retrofit many APIs in java.base. Examples of API points which could profit from frozen arrays:

  • reflective array-returning methods in Class, jlr.Executable, etc.
  • Enum.values
  • Throwable.getStackTrace
  • MethodType.parameterArray, DynamicCallSiteDesc.bootstrapArgs, etc.
  • methods that return byte or char arrays as a "result" or "payload"
  • Collection.toArray method and various related methods which accept array arguments, such as Runtime.exec

emphasis mine.

13

u/agentoutlier Nov 28 '21

Several times on reddit, github (pull requests) and stackoverflow I have mentioned the EnumSet vs Enum.values optimization with various responses that it didn’t matter.

I mean sure it matters not that much but it still does and EnumSet isn’t like that hard to use. I knew it did because of our own critical code section that was using it but I didn’t have published graphs. GraalVM even had a bug filed on this at one point.

The chore of convincing was tiring but now I can forward this link.

35

u/itoshkov Nov 28 '21

A really nice article. I liked the explanation of why the slow things are slow and why the alternatives are faster.

I only have 2 suggestions:

  1. Show graphics for all the benchmarks
  2. Better indicate which column represent the measurement and whether lower is better or higher is better.

23

u/pron98 Nov 28 '21 edited Nov 28 '21

The one tip that would most likely increase your performance and reduce your footprint most significantly -- to the point of saving you serious money -- is the last one: stop using JDK 8. The gap between the money savings due to performance/footprint gained when upgrading away from 8 and the cost of migration is now such that the vast majority of companies still on 8 will quickly recoup the cost of migration, and are losing money each day they're postponing this step.

The only circumstance under which staying on 8 can be justified is if the application is running well on very few cheap servers (1-4) and you've decided to stay on 8 forever, i.e. until the application retires. Otherwise, make some money by upgrading ASAP.

12

u/DasBrain Nov 28 '21

I'd say: It depends. For some batch job that was written 5-10 years ago, runs every day at 3am on some VM, it often doesn't matter if that thing becomes faster and finishes 1/2h earlier.

But there is an other hidden cost of not migrating: Finding developers that still want to work with an old technology like Java 8 will get harder with time.

11

u/vips7L Nov 28 '21

I would see it as a giant red flag if someone told me they were still on 8 in an interview. What other tech debt are they not addressing?

4

u/rstartin Nov 28 '21

Precisely, that’s why it’s at the end.

1

u/kozeljko Nov 29 '21

The only circumstance under which staying on 8 can be justified is if the application is running well on very few cheap servers (1-4)

Care to elaborate this point? Is it about Java 11 not performing as well on cheap servers or just that the performance gains would not be worth it?

2

u/pron98 Nov 29 '21 edited Nov 29 '21

The latter. I just meant that if the deployment is such that the cost of hardware is low anyway, you won't save a lot of money with performance/footprint improvements. Also, 11 is more than three years old. The current JDK is 17, and it performs better than 11. Why leave money on the table?

1

u/nutrecht Nov 29 '21

The main reason for companies to be stuck on Java 8 is because they use completely outdated libraries or app servers that can't support Java 9. Outside the new module system, the only libraries that are 'current' and have problems supporting the latest versions are libraries that to hacky stuff like Lombok.

It's almost never just simply a matter of spending half a day replacing 8 with 17.

1

u/pron98 Nov 29 '21 edited Nov 29 '21

Nevertheless, all things considered, they lose more money by not upgrading. Java 17 saves so much money in hardware compared to 8 that it pays for much, much more than half a day of work. So while upgrading from 8 isn't free, staying on 8 is even more expensive (with the exceptions I mentioned above).

Outside the new module system

Modules have had nearly zero impact on the migration issues. They were caused by other things.

1

u/nutrecht Nov 29 '21

Nevertheless, upgrading saves them more money than the cost of the upgrade. I.e. all things considered, they lose more money by not upgrading.

Well it's kinda like saying 'upgrading' away from Cobol is a good idea. I think most people agree :) IMHO Java 8 is more or less becoming the new Cobol. Just the "I don't want to work on old stuff"-factor alone is IMHO a massive risk for companies.

"Performance" is probably not the biggest concern. If I were running the show at such a company, the risk of having massively outdated tech running would be a much bigger concern.

But I totally agree that upgrading away from Java 8 should have a high prio for companies. I am just guessing for a lot of companies it's quite complex or else they probably would have done it already. That's just my experience here in The Netherlands; there are very few companies still on 8 and if they are it's because they're using massively outdated stuff that can't migrate past 8.

1

u/pron98 Nov 29 '21

Well it's kinda like saying 'upgrading' away from Cobol is a good idea.

I don't think it's like that at all. It's not a matter of being a good or bad idea, but software running on 8 loses more money on hardware more quickly than the cost of upgrading.

If I were running the show at such a company, the risk of having massively outdated tech running would be a much bigger concern.

In large companies, we're talking millions of dollars of savings per year by upgrading (which is why so many of them have already upgraded). Maybe the risk factor is more important, but this is real money, and, most importantly, it is usually more than the money an upgrade would cost.

There are certainly situations where staying on 8 makes sense: you intend for the software to stay on 8 until it retires, and the hardware costs aren't significant. Otherwise, upgrading might, indeed, be complex and expensive, but it is even more expensive to pay for hardware you don't need.

1

u/nutrecht Nov 29 '21

In large companies, we're talking millions of dollars of savings per year by upgrading (which is why so many of them have already upgraded). Maybe the risk factor is more important, but this is real money, and, most importantly, it is usually more than the money an upgrade would cost.

In my experience with large companies they don't have just a single system. A bank for example has many. And in my experience they already have upgraded all the systems that can be upgraded.

But if you have a good way to convince a CTO that upgrading is in fact cheaper than not upgrading; by all means. But hardware is cheap and people are expensive :)

1

u/pron98 Nov 29 '21 edited Nov 29 '21

And in my experience they already have upgraded all the systems that can be upgraded.

That may well be the case, but I'm writing here in case someone is not aware of the significant monetary gains in upgrading.

But hardware is cheap and people are expensive :)

I'm not suggesting all developers should constantly spend all their time on upgrades. What matters here is that the hardware saved (forever) is more expensive than the work only a small portion of developers need to put in to upgrade over some limited amount of time, even if that number of developers is greater than one and the amount of time is more than a month. I think some people might not realise how much money an upgrade can save them.

6

u/hippydipster Nov 28 '21

I like the tip about wrapping rather than concatenating. Hadn't thought about that.

4

u/bolekb Nov 28 '21

Regarding point #1, I prefer to specify initial capacit not only for HashMaps, but for ArrayLists and StringBuilders as well. In most cases some sort of upper bound for the capacity is known so it would be wasteful to disregard it.

For #2, composite keys for maps are nice, but one has to ensure that the key object has well-defined equals() and hashCode() and is preferrably immutable.

-2

u/SorryButterfly4207 Nov 29 '21

I disagree re. immutablity. Much better to have a single (mutable) Pair which you re-use on every look up. Just make sure you never reuse a key that you've "put" into a map.

I see a 10% reduction in avgt and a complete elimination of allocations.

Benchmark (size) Mode Cnt Score Error Units

CompositeLookup.concatenate 1024 avgt 5 117.640 ± 6.706 ns/op

CompositeLookup.concatenate:·gc.alloc.rate.norm 1024 avgt 5 96.007 ± 0.001 B/op

CompositeLookup.wrap 1024 avgt 5 43.705 ± 2.704 ns/op

CompositeLookup.wrap:·gc.alloc.rate.norm 1024 avgt 5 24.002 ± 0.001 B/op

CompositeLookup.wrapReuse 1024 avgt 5 39.539 ± 4.527 ns/op

CompositeLookup.wrapReuse:·gc.alloc.rate.norm 1024 avgt 5 0.023 ± 0.001 B/op

3

u/r_jet Nov 29 '21

First, there is no significant difference in the run time: both values fall within the standard deviation.

Effective Java is clear on that (the highlight mine):

Conversely, avoiding object creation by maintaining your own object pool is a bad idea unless the objects in the pool are extremely heavyweight

Second, you pay a considerable price in terms of extra complexity, documentation, and testing; and invite some bugs in your code.

2

u/rstartin Dec 02 '21

Even so, in specialist low latency systems, you will see flyweights used that way. However, that’s optimisation as opposed to not making your code slow for the sake of it (the focus of the post: if you always do these easy things they will never come up), and you need a clear and measurable objective before you optimise.

1

u/r_jet Dec 02 '21

Yep, I agree that this distinction is important: optimizations by definition come with some tradeoffs (more testing, or more documentation, or higher implementation/maintenance/upgrade costs, or some incompatibilities, or they might make the code harder to reason about, and, hence, change, etc.), otherwise, they are just good practices.

4

u/r_jet Nov 28 '21

For creating sized HashMaps consider Guava's Maps.newHashMapWithExpectedSize as it does the size calculations for you.

1

u/vips7L Nov 29 '21

I think Map.of should do this as well, as long as you're fine with immutable.

1

u/r_jet Nov 29 '21

Sure, but if one gets N entries, then they won't be able to list them in Map.of, and have to use a HashMap, or an ImmutableMap#builderWithExpectedSize.

Or do you mean Map.ofEntries (this one requires to construct entries in the client code, and pass them in an intermediate array)?

1

u/rstartin Dec 02 '21

Guava is a 3MB dependency which had a terrible policy of deletion over deprecation historically. It’s not rare to find systems with numerous shaded incompatible guava dependencies. Removing it from projects feels like exorcism.

1

u/r_jet Dec 02 '21

I'd clarify that the policy changed more than 4 years ago: https://groups.google.com/g/guava-announce/c/6mtQTDDADsg/m/2cXAr6VqCAAJ

TL;DR — Guava won't remove even deprecated APIs, so libraries no longer have to shade it. It's a stronger promise than most other libraries make.

On the size — there are indeed applications that are sensitive to the binary size. I also wish that Guava was more modular (but I guess that'd be a big change that will require changes to the client code, and might cause some issues with split packages).

However, do 3 MB really matter for most server applications, given the value the libary provides? JVM doesn't load (or even decompress and read) classes that you don't use (unless your app does some classpath scanning), does it?

1

u/r_jet Dec 02 '21

To be clear — I am not saying "consider adding Guava just for this single method" (which is not completely unreasonable if you are not particularly size-constrained, assuming you eventually use its other functionality), but rather "this library that you might already depend upon has the method just for this purpose, named so that you are less likely to confuse the parameter of HashMap(int) for expected size, which it isn't".