Ben Caradoc-Davies ha scritto: > [This affects GeoServer too, but I'd rather avoid a cross-list post.] > > Synopsis: > - HashMap has platform-dependent iteration order. > - Use LinkedHashMap to make iteration order consistent across platforms. > - Same rule applies for HashSet: replace with LinkedHashSet. > > HashMap uses key hashCodes to put entries into bins. Because hashCodes > are typically based on the underlying machine pointer address, they > differ across platform and configuration (Unix/Windows, 32-bit/64-bit, > JVM implementation, GC options, ...). Iteration over a HashMap is > performed by walking the bins in order, which relies on hashCodes, and > thus varies across platforms. > > In a nutshell: HashMap iteration order == chaos (across platforms).
Developer depending on hashmap order -> chaos It's not the fault of poor hashmap that developers use it in a number of places where it's unnecessary if not even harmful. I see a tendency in our code to use Set whenever, when a plain list would be more efficient, more compact, and without surprises. Yes, in some cases you would have to ensure it contains no duplicates, but for collections have have a short build time and a long read only usage time, in which you can control what the user puts into them, and whose primary access it iteration, lists are a winner any day (yes, that's a lot of ifs, but a common situation too). > It is my experience that many developers (including myself) infer > software behaviour by observation, as a workaround for incomplete > documentation, lack of explicit design-by-contract, and sheer software > complexity. One example of this, and I am as much as fault as anyone, > are unit tests that assume the behaviour of software on my platform is > the correct one. The results are unjustified assumptions encoded into > unit tests, which then fail on other platforms. > > Platform iteration chaos is also exposed to end-users. For example, have > a look at GeoServer WFS namespace order, which varies across platforms. > There are many other examples in the code base. > > I propose that wherever we might be tempted to use a HashMap in a manner > that will expose its iteration order, we instead use a LinkedHashMap. > Furthermore, I also propose that we retrofit all existing uses of > HashMap that expose iteration order with LinkedHashMap. I agree with this, but would also urge developers to stop and think for a moment if they really need a Set to start with. Would a list suffice? If so please spare everybody the hefty overhead HashSet impose (LinkedHashSet is just worse memory consumption wise). Memory issues are not problematic if they are used in places where you don't get to build millions of them though (and do so lazily, e.g., making the feature user map be built lazily provided a good performance boost in the past). Cheers Andrea -- Andrea Aime OpenGeo - http://opengeo.org Expert service straight from the developers. ------------------------------------------------------------------------------ ThinkGeek and WIRED's GeekDad team up for the Ultimate GeekDad Father's Day Giveaway. ONE MASSIVE PRIZE to the lucky parental unit. See the prize list and enter to win: http://p.sf.net/sfu/thinkgeek-promo _______________________________________________ Geotools-devel mailing list Geotools-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/geotools-devel