3

I was discussing performance implications of using the Builder pattern within Java with a colleague and while doing research I came across an interesting answer:

https://stackoverflow.com/a/2448624/3049628

But the Java-VM does optimize very strongly, especially the Server-VM (java -server), so the VM may optimize away the builder completely.

Is this true? I tried doing some google searches to support this claim and couldn't find anything. Are some JVMs really smart enough to detect a Builder object and bypass it in cases where it is feasible to do so? If so which JVMs support this?

Community
  • 1
  • 1
Tim B
  • 40,716
  • 16
  • 83
  • 128
  • I'd read and do what is said in the very next sentence in the post in question. – Olivier Grégoire Feb 28 '17 at 09:32
  • @OlivierGrégoire Which gives me a maybe result for a specific VM in the context of a specific test. I'd prefer a more general understanding, thanks all the same. – Tim B Feb 28 '17 at 09:37
  • @AndyTurner Thanks, that's very definitely related. I'm almost tempted to close this as a dupe in fact. – Tim B Feb 28 '17 at 09:37
  • @TimB yeah, I was considering doing so too. – Andy Turner Feb 28 '17 at 09:38
  • While it can, it doesn't mean any particular JVM will. Note: this only happens in the Oracle JVM once the code has been optimised. – Peter Lawrey Feb 28 '17 at 09:59
  • @Peter Lawrey: that’s true, but on the other hand, the performance implication of the allocation of a builder instance is negligible anyway. And the optimizer will usually kick in before the number of allocations can become an issue. – Holger Feb 28 '17 at 11:56
  • @Holger I agree that if it kicks in, it hardly matters as you can encourage this to happen on startup if you care. The only issue is if you really need it to be optimised away, but the JVM for whatever reason doesn't. – Peter Lawrey Feb 28 '17 at 12:18

0 Answers0