ImmutableTable.get results in copyOf with actual copy being invoked
See original GitHub issueI faced a strange performance situation with ImmutableTable
.
Here is a stacktrace of what’s happening (it’s a profiling trace from async-profiler):
Basically, it seems that calling ImmutableTable.get()
results in ImmutableMap.copyOf()
being called, and even though there is a comment in rowMap()
saying that copyOf
is called only for casting (https://github.com/google/guava/blob/master/guava/src/com/google/common/collect/SparseImmutableTable.java#L97), in practice, there is an actual copy via toArray()
.
I believe this is not meant to happen, is it?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:8 (2 by maintainers)
Top Results From Across the Web
com.google.common.collect.ImmutableTable.copyOf java code ...
Returns an immutable copy of the provided table. The Table#cellSet() iteration order of the provided table determines the iteration ordering of all views...
Read more >Is it better to return an ImmutableMap or a Map? - Stack Overflow
If a mutative operation is invoked it creates a copy of the underlying map and all further invocations will be delegated to this...
Read more >ImmutableMap (Guava: Google Core Libraries for Java 16.0 API)
ImmutableMap is convenient for public static final maps ("constant maps") and also lets you easily make a "defensive copy" of a map provided...
Read more >ImmutableMap (Guava: Google Core Libraries for Java 31.1 ...
Returns true if this map maps one or more keys to the specified value. static <K,V> ImmutableMap<K,V>, copyOf(Iterable<?
Read more >Java - Coding Rules
When the call to a function doesn't have any side effects, what is the point of ... You should have received a copy...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
For what it’s worth, I’m confident that the code would behave that way if we were seeing that kind of interaction between multiple classloaders. What I’m not at all confident in is that such an interaction was happening for the original poster.
If you’re curious, it’s definitely possible to test such things, but it’s definitely a pain 😃 You can see some examples custom classloaders in our tests, such as in this test: https://github.com/google/guava/blob/081c486173032e6096c912e3c297c1d74eddcf93/guava-tests/test/com/google/common/util/concurrent/AbstractFutureInnocuousThreadTest.java#L48
This does seem very strange, and it pops into my head from time to time.
The only even remotely plausible theory I have is that the
ImmutableTable
is an instance of the type from one classloader and theImmutableMap
call is using the type from another.[edit: Actually, I don’t think I buy this: The two references to
ImmutableMap
should refer to the same type, since they are both source references from `ImmutableTable. I think?]