24

I suggested returning Collections.unmodifiableList() instead of directly returning a member variable, and my colleague is concerned that there would be a performance hit. Of course the best answer is to measure it, and we may do that - but I'd like to know your experiences and any references, pro or con.

Carl Manaster
  • 39,912
  • 17
  • 102
  • 155
  • 2
    This is the kind of unsubstantiated, pre-mature micro-optimization that drives me crazy. Be sure to point your colleague to these responses and the source code itself. – duffymo Jul 07 '11 at 00:46
  • Thanks, Duffy; I'll do it. I'd rather take the time to measure (and still might), but it's good to have clear answers here, not just for me, and not just for this once. I expected I would find a question along these lines already asked & well answered here. When I didn't - well, I asked. – Carl Manaster Jul 07 '11 at 00:49
  • 1
    @duffymo agreed entirely. @Carl Manaster the question you need to ask your colleague is 'performance hit compared to what?' You can get the wrong answer in zero time if that's what you want. It isn't. – user207421 Jul 07 '11 at 02:07
  • @EJP Indeed, when somebody jeopardizes the efficiency of our holy patterns, we must label the plain approaches as microapptimizations and disregard them. Kidding and trolling can stop even the most passionate dumb asses, like that collegue. No dumb ass is allowed to ask dangerous questions jeopardizing our holy programming patterns. That is why, nobody can determine what is the asked reference when one asks to compare a pattern wrapper against the original dumb collection. Trolling and absurd is our absolute weapon. It destroys any sense. – Val Aug 31 '13 at 11:20

3 Answers3

26

No. At least, the OpenJDK implementation literally "replaces" the modification methods with UnsupportedOperationExceptions, the rest adds one level of indirection, which should just get optimized away by the compiler VM (and even so, one level of indirection wouldn't be costly).

If you wish to return a list that cannot be modified, any performance impact would pale in comparison to the loss in correctness, I wouldn't avoid it for performance alone, and I certainly wouldn't avoid it if it's what you need.

Mark Elliot
  • 75,278
  • 22
  • 140
  • 160
7

If you look at the implementation you'll see that Collections.unmodifiable is just a thin wrapper around the real collection that throws an exception for all remove/add methods instead of forwarding it. So no there's no performance hit (the forwarding call will be inlined by the JIT).

So yes you absolutely should return an unmodifiable collection instead of the original most of the times - much better coding practice.

Voo
  • 29,040
  • 11
  • 82
  • 156
3

If the JIT inlines the functions, no. If it doesn't, then yes, a slight performance hit will happen, but you will likely not be able to notice it unless you're having a very tight loop.

It likely will inline the function, unless maybe you compile for debugging.

user541686
  • 205,094
  • 128
  • 528
  • 886