JDK-8201650 : Move iteration order randomization of unmodifiable Set and Map to iterators
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.util:collections
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2018-04-17
  • Updated: 2018-05-03
  • Resolved: 2018-04-30
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 11
11 b12Fixed
Related Reports
Relates :  
Relates :  
Description
In the current implementation of Map.of / Set.of, the implementations are using a salt number to randomize insertion in their respective backing arrays when created. When looking up elements in the Set/Map, the same salt needs to be mixed in to get the correct address. This randomization is done to avoid accidental dependency on the iteration order of these collections from creeping into user code.

Moving this randomization step to their respective iterator has a couple of advantages:

- Simplifies the probe operation, which may speed up contains/containsKey and get operations slightly

- Stabilizes the memory layout from one run to another, which makes it straightforward to archive Set.of/Map.of instances using AppCDS, while still leaving iteration order unpredictable. Since such immutable collections are common during bootstrap and make up much of the module graph, experiments show we can reduce startup a few percent by archiving them with CDS.

Main drawback is that performance concerns may limit how random such iterators can be: a fully random iteration order could degrade cache locality etc. However, real randomness isn't strictly necessary: something like randomly choosing a starting position and/or randomly reversing iteration order should be sufficient to achieve the goal of making the iteration order vary from run to run in an unpredictable manner. 
Comments
oops, very true, sorry for the noise.
17-04-2018

List.of(...) has never had any iteration or element order randomization: "The order of elements in the list is the same as the order of the provided arguments, or of the elements in the provided array." https://docs.oracle.com/javase/9/docs/api/java/util/List.html#immutable
17-04-2018

Does it also means that for(int i = 0; i < list.size(); i++) { list.get(i); } is not randomized at all ?
17-04-2018