ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jsr166e/ConcurrentHashMapV8.java
(Generate patch)

Comparing jsr166/src/jsr166e/ConcurrentHashMapV8.java (file contents):
Revision 1.34 by jsr166, Mon Dec 19 19:18:35 2011 UTC vs.
Revision 1.46 by dl, Thu Jul 5 18:05:28 2012 UTC

# Line 20 | Line 20 | import java.util.Enumeration;
20   import java.util.ConcurrentModificationException;
21   import java.util.NoSuchElementException;
22   import java.util.concurrent.ConcurrentMap;
23 + import java.util.concurrent.ThreadLocalRandom;
24   import java.util.concurrent.locks.LockSupport;
25 + import java.util.concurrent.locks.AbstractQueuedSynchronizer;
26   import java.io.Serializable;
27  
28   /**
# Line 103 | Line 105 | public class ConcurrentHashMapV8<K, V>
105       */
106      public static interface MappingFunction<K, V> {
107          /**
108 <         * Returns a non-null value for the given key.
108 >         * Returns a value for the given key, or null if there is no mapping.
109           *
110           * @param key the (non-null) key
111 <         * @return a non-null value
111 >         * @return a value for the key, or null if none
112           */
113          V map(K key);
114      }
# Line 123 | Line 125 | public class ConcurrentHashMapV8<K, V>
125           *
126           * @param key the (non-null) key
127           * @param value the current value, or null if there is no mapping
128 <         * @return a non-null value
128 >         * @return a value for the key, or null if none
129           */
130          V remap(K key, V value);
131      }
132  
133 +    /**
134 +     * A partitionable iterator. A Spliterator can be traversed
135 +     * directly, but can also be partitioned (before traversal) by
136 +     * creating another Spliterator that covers a non-overlapping
137 +     * portion of the elements, and so may be amenable to parallel
138 +     * execution.
139 +     *
140 +     * <p> This interface exports a subset of expected JDK8
141 +     * functionality.
142 +     *
143 +     * <p>Sample usage: Here is one (of the several) ways to compute
144 +     * the sum of the values held in a map using the ForkJoin
145 +     * framework. As illustrated here, Spliterators are well suited to
146 +     * designs in which a task repeatedly splits off half its work
147 +     * into forked subtasks until small enough to process directly,
148 +     * and then joins these subtasks. Variants of this style can also
149 +     * be used in completion-based designs.
150 +     *
151 +     * <pre>
152 +     * {@code ConcurrentHashMapV8<String, Long> m = ...
153 +     * // Uses parallel depth of log2 of size / (parallelism * slack of 8).
154 +     * int depth = 32 - Integer.numberOfLeadingZeros(m.size() / (aForkJoinPool.getParallelism() * 8));
155 +     * long sum = aForkJoinPool.invoke(new SumValues(m.valueSpliterator(), depth, null));
156 +     * // ...
157 +     * static class SumValues extends RecursiveTask<Long> {
158 +     *   final Spliterator<Long> s;
159 +     *   final int depth;             // number of splits before processing
160 +     *   final SumValues nextJoin;    // records forked subtasks to join
161 +     *   SumValues(Spliterator<Long> s, int depth, SumValues nextJoin) {
162 +     *     this.s = s; this.depth = depth; this.nextJoin = nextJoin;
163 +     *   }
164 +     *   public Long compute() {
165 +     *     long sum = 0;
166 +     *     SumValues subtasks = null; // fork subtasks
167 +     *     for (int d = depth - 1; d >= 0; --d)
168 +     *       (subtasks = new SumValues(s.split(), d, subtasks)).fork();
169 +     *     while (s.hasNext())        // directly process remaining elements
170 +     *       sum += s.next();
171 +     *     for (SumValues t = subtasks; t != null; t = t.nextJoin)
172 +     *       sum += t.join();         // collect subtask results
173 +     *     return sum;
174 +     *   }
175 +     * }
176 +     * }</pre>
177 +     */
178 +    public static interface Spliterator<T> extends Iterator<T> {
179 +        /**
180 +         * Returns a Spliterator covering approximately half of the
181 +         * elements, guaranteed not to overlap with those subsequently
182 +         * returned by this Spliterator.  After invoking this method,
183 +         * the current Spliterator will <em>not</em> produce any of
184 +         * the elements of the returned Spliterator, but the two
185 +         * Spliterators together will produce all of the elements that
186 +         * would have been produced by this Spliterator had this
187 +         * method not been called. The exact number of elements
188 +         * produced by the returned Spliterator is not guaranteed, and
189 +         * may be zero (i.e., with {@code hasNext()} reporting {@code
190 +         * false}) if this Spliterator cannot be further split.
191 +         *
192 +         * @return a Spliterator covering approximately half of the
193 +         * elements
194 +         * @throws IllegalStateException if this Spliterator has
195 +         * already commenced traversing elements
196 +         */
197 +        Spliterator<T> split();
198 +    }
199 +
200      /*
201       * Overview:
202       *
# Line 147 | Line 216 | public class ConcurrentHashMapV8<K, V>
216       * supplying null-checks and casts as needed. This also allows
217       * many of the public methods to be factored into a smaller number
218       * of internal methods (although sadly not so for the five
219 <     * sprawling variants of put-related operations).
219 >     * variants of put-related operations). The validation-based
220 >     * approach explained below leads to a lot of code sprawl because
221 >     * retry-control precludes factoring into smaller methods.
222       *
223       * The table is lazily initialized to a power-of-two size upon the
224 <     * first insertion.  Each bin in the table contains a list of
225 <     * Nodes (most often, the list has only zero or one Node).  Table
226 <     * accesses require volatile/atomic reads, writes, and CASes.
227 <     * Because there is no other way to arrange this without adding
228 <     * further indirections, we use intrinsics (sun.misc.Unsafe)
229 <     * operations.  The lists of nodes within bins are always
230 <     * accurately traversable under volatile reads, so long as lookups
231 <     * check hash code and non-nullness of value before checking key
232 <     * equality.
224 >     * first insertion.  Each bin in the table normally contains a
225 >     * list of Nodes (most often, the list has only zero or one Node).
226 >     * Table accesses require volatile/atomic reads, writes, and
227 >     * CASes.  Because there is no other way to arrange this without
228 >     * adding further indirections, we use intrinsics
229 >     * (sun.misc.Unsafe) operations.  The lists of nodes within bins
230 >     * are always accurately traversable under volatile reads, so long
231 >     * as lookups check hash code and non-nullness of value before
232 >     * checking key equality.
233       *
234       * We use the top two bits of Node hash fields for control
235       * purposes -- they are available anyway because of addressing
# Line 170 | Line 241 | public class ConcurrentHashMapV8<K, V>
241       *  10 - Node is a forwarding node
242       *
243       * The lower 30 bits of each Node's hash field contain a
244 <     * transformation (for better randomization -- method "spread") of
245 <     * the key's hash code, except for forwarding nodes, for which the
246 <     * lower bits are zero (and so always have hash field == MOVED).
244 >     * transformation of the key's hash code, except for forwarding
245 >     * nodes, for which the lower bits are zero (and so always have
246 >     * hash field == MOVED).
247       *
248       * Insertion (via put or its variants) of the first node in an
249       * empty bin is performed by just CASing it to the bin.  This is
250 <     * by far the most common case for put operations.  Other update
251 <     * operations (insert, delete, and replace) require locks.  We do
252 <     * not want to waste the space required to associate a distinct
253 <     * lock object with each bin, so instead use the first node of a
254 <     * bin list itself as a lock. Blocking support for these locks
255 <     * relies on the builtin "synchronized" monitors.  However, we
256 <     * also need a tryLock construction, so we overlay these by using
257 <     * bits of the Node hash field for lock control (see above), and
258 <     * so normally use builtin monitors only for blocking and
259 <     * signalling using wait/notifyAll constructions. See
260 <     * Node.tryAwaitLock.
250 >     * by far the most common case for put operations under most
251 >     * key/hash distributions.  Other update operations (insert,
252 >     * delete, and replace) require locks.  We do not want to waste
253 >     * the space required to associate a distinct lock object with
254 >     * each bin, so instead use the first node of a bin list itself as
255 >     * a lock. Blocking support for these locks relies on the builtin
256 >     * "synchronized" monitors.  However, we also need a tryLock
257 >     * construction, so we overlay these by using bits of the Node
258 >     * hash field for lock control (see above), and so normally use
259 >     * builtin monitors only for blocking and signalling using
260 >     * wait/notifyAll constructions. See Node.tryAwaitLock.
261       *
262       * Using the first node of a list as a lock does not by itself
263       * suffice though: When a node is locked, any update must first
# Line 201 | Line 272 | public class ConcurrentHashMapV8<K, V>
272       * The main disadvantage of per-bin locks is that other update
273       * operations on other nodes in a bin list protected by the same
274       * lock can stall, for example when user equals() or mapping
275 <     * functions take a long time.  However, statistically, this is
276 <     * not a common enough problem to outweigh the time/space overhead
277 <     * of alternatives: Under random hash codes, the frequency of
207 <     * nodes in bins follows a Poisson distribution
275 >     * functions take a long time.  However, statistically, under
276 >     * random hash codes, this is not a common problem.  Ideally, the
277 >     * frequency of nodes in bins follows a Poisson distribution
278       * (http://en.wikipedia.org/wiki/Poisson_distribution) with a
279       * parameter of about 0.5 on average, given the resizing threshold
280       * of 0.75, although with a large variance because of resizing
281       * granularity. Ignoring variance, the expected occurrences of
282       * list size k are (exp(-0.5) * pow(0.5, k) / factorial(k)). The
283 <     * first few values are:
283 >     * first values are:
284       *
285 <     * 0:    0.607
286 <     * 1:    0.303
287 <     * 2:    0.076
288 <     * 3:    0.012
289 <     * more: 0.002
285 >     * 0:    0.60653066
286 >     * 1:    0.30326533
287 >     * 2:    0.07581633
288 >     * 3:    0.01263606
289 >     * 4:    0.00157952
290 >     * 5:    0.00015795
291 >     * 6:    0.00001316
292 >     * 7:    0.00000094
293 >     * 8:    0.00000006
294 >     * more: less than 1 in ten million
295       *
296       * Lock contention probability for two threads accessing distinct
297 <     * elements is roughly 1 / (8 * #elements).  Function "spread"
298 <     * performs hashCode randomization that improves the likelihood
299 <     * that these assumptions hold unless users define exactly the
300 <     * same value for too many hashCodes.
297 >     * elements is roughly 1 / (8 * #elements) under random hashes.
298 >     *
299 >     * Actual hash code distributions encountered in practice
300 >     * sometimes deviate significantly from uniform randomness.  This
301 >     * includes the case when N > (1<<30), so some keys MUST collide.
302 >     * Similarly for dumb or hostile usages in which multiple keys are
303 >     * designed to have identical hash codes. Also, although we guard
304 >     * against the worst effects of this (see method spread), sets of
305 >     * hashes may differ only in bits that do not impact their bin
306 >     * index for a given power-of-two mask.  So we use a secondary
307 >     * strategy that applies when the number of nodes in a bin exceeds
308 >     * a threshold, and at least one of the keys implements
309 >     * Comparable.  These TreeBins use a balanced tree to hold nodes
310 >     * (a specialized form of red-black trees), bounding search time
311 >     * to O(log N).  Each search step in a TreeBin is around twice as
312 >     * slow as in a regular list, but given that N cannot exceed
313 >     * (1<<64) (before running out of addresses) this bounds search
314 >     * steps, lock hold times, etc, to reasonable constants (roughly
315 >     * 100 nodes inspected per operation worst case) so long as keys
316 >     * are Comparable (which is very common -- String, Long, etc).
317 >     * TreeBin nodes (TreeNodes) also maintain the same "next"
318 >     * traversal pointers as regular nodes, so can be traversed in
319 >     * iterators in the same way.
320       *
321 <     * The table is resized when occupancy exceeds an occupancy
321 >     * The table is resized when occupancy exceeds a percentage
322       * threshold (nominally, 0.75, but see below).  Only a single
323       * thread performs the resize (using field "sizeCtl", to arrange
324       * exclusion), but the table otherwise remains usable for reads
# Line 245 | Line 339 | public class ConcurrentHashMapV8<K, V>
339       *
340       * Each bin transfer requires its bin lock. However, unlike other
341       * cases, a transfer can skip a bin if it fails to acquire its
342 <     * lock, and revisit it later. Method rebuild maintains a buffer
343 <     * of TRANSFER_BUFFER_SIZE bins that have been skipped because of
344 <     * failure to acquire a lock, and blocks only if none are
345 <     * available (i.e., only very rarely).  The transfer operation
346 <     * must also ensure that all accessible bins in both the old and
347 <     * new table are usable by any traversal.  When there are no lock
348 <     * acquisition failures, this is arranged simply by proceeding
349 <     * from the last bin (table.length - 1) up towards the first.
350 <     * Upon seeing a forwarding node, traversals (see class
351 <     * InternalIterator) arrange to move to the new table without
352 <     * revisiting nodes.  However, when any node is skipped during a
353 <     * transfer, all earlier table bins may have become visible, so
354 <     * are initialized with a reverse-forwarding node back to the old
355 <     * table until the new ones are established. (This sometimes
356 <     * requires transiently locking a forwarding node, which is
357 <     * possible under the above encoding.) These more expensive
342 >     * lock, and revisit it later (unless it is a TreeBin). Method
343 >     * rebuild maintains a buffer of TRANSFER_BUFFER_SIZE bins that
344 >     * have been skipped because of failure to acquire a lock, and
345 >     * blocks only if none are available (i.e., only very rarely).
346 >     * The transfer operation must also ensure that all accessible
347 >     * bins in both the old and new table are usable by any traversal.
348 >     * When there are no lock acquisition failures, this is arranged
349 >     * simply by proceeding from the last bin (table.length - 1) up
350 >     * towards the first.  Upon seeing a forwarding node, traversals
351 >     * (see class InternalIterator) arrange to move to the new table
352 >     * without revisiting nodes.  However, when any node is skipped
353 >     * during a transfer, all earlier table bins may have become
354 >     * visible, so are initialized with a reverse-forwarding node back
355 >     * to the old table until the new ones are established. (This
356 >     * sometimes requires transiently locking a forwarding node, which
357 >     * is possible under the above encoding.) These more expensive
358       * mechanics trigger only when necessary.
359       *
360       * The traversal scheme also applies to partial traversals of
361       * ranges of bins (via an alternate InternalIterator constructor)
362 <     * to support partitioned aggregate operations (that are not
363 <     * otherwise implemented yet).  Also, read-only operations give up
364 <     * if ever forwarded to a null table, which provides support for
365 <     * shutdown-style clearing, which is also not currently
272 <     * implemented.
362 >     * to support partitioned aggregate operations.  Also, read-only
363 >     * operations give up if ever forwarded to a null table, which
364 >     * provides support for shutdown-style clearing, which is also not
365 >     * currently implemented.
366       *
367       * Lazy table initialization minimizes footprint until first use,
368       * and also avoids resizings when the first operation is from a
# Line 347 | Line 440 | public class ConcurrentHashMapV8<K, V>
440       */
441      private static final int TRANSFER_BUFFER_SIZE = 32;
442  
443 +    /**
444 +     * The bin count threshold for using a tree rather than list for a
445 +     * bin.  The value reflects the approximate break-even point for
446 +     * using tree-based operations.
447 +     */
448 +    private static final int TREE_THRESHOLD = 8;
449 +
450      /*
451       * Encodings for special uses of Node hash fields. See above for
452       * explanation.
453       */
454 <    static final int MOVED     = 0x80000000; // hash field for fowarding nodes
454 >    static final int MOVED     = 0x80000000; // hash field for forwarding nodes
455      static final int LOCKED    = 0x40000000; // set/tested only as a bit
456      static final int WAITING   = 0xc0000000; // both bits set/tested together
457      static final int HASH_BITS = 0x3fffffff; // usable bits of normal node hash
# Line 386 | Line 486 | public class ConcurrentHashMapV8<K, V>
486      /** For serialization compatibility. Null unless serialized; see below */
487      private Segment<K,V>[] segments;
488  
489 +    /* ---------------- Table element access -------------- */
490 +
491 +    /*
492 +     * Volatile access methods are used for table elements as well as
493 +     * elements of in-progress next table while resizing.  Uses are
494 +     * null checked by callers, and implicitly bounds-checked, relying
495 +     * on the invariants that tab arrays have non-zero size, and all
496 +     * indices are masked with (tab.length - 1) which is never
497 +     * negative and always less than length. Note that, to be correct
498 +     * wrt arbitrary concurrency errors by users, bounds checks must
499 +     * operate on local variables, which accounts for some odd-looking
500 +     * inline assignments below.
501 +     */
502 +
503 +    static final Node tabAt(Node[] tab, int i) { // used by InternalIterator
504 +        return (Node)UNSAFE.getObjectVolatile(tab, ((long)i<<ASHIFT)+ABASE);
505 +    }
506 +
507 +    private static final boolean casTabAt(Node[] tab, int i, Node c, Node v) {
508 +        return UNSAFE.compareAndSwapObject(tab, ((long)i<<ASHIFT)+ABASE, c, v);
509 +    }
510 +
511 +    private static final void setTabAt(Node[] tab, int i, Node v) {
512 +        UNSAFE.putObjectVolatile(tab, ((long)i<<ASHIFT)+ABASE, v);
513 +    }
514 +
515      /* ---------------- Nodes -------------- */
516  
517      /**
518       * Key-value entry. Note that this is never exported out as a
519 <     * user-visible Map.Entry (see WriteThroughEntry and SnapshotEntry
520 <     * below). Nodes with a hash field of MOVED are special, and do
521 <     * not contain user keys or values.  Otherwise, keys are never
522 <     * null, and null val fields indicate that a node is in the
523 <     * process of being deleted or created. For purposes of read-only
524 <     * access, a key may be read before a val, but can only be used
525 <     * after checking val to be non-null.
519 >     * user-visible Map.Entry (see MapEntry below). Nodes with a hash
520 >     * field of MOVED are special, and do not contain user keys or
521 >     * values.  Otherwise, keys are never null, and null val fields
522 >     * indicate that a node is in the process of being deleted or
523 >     * created. For purposes of read-only access, a key may be read
524 >     * before a val, but can only be used after checking val to be
525 >     * non-null.
526       */
527 <    static final class Node {
527 >    static class Node {
528          volatile int hash;
529          final Object key;
530          volatile Object val;
# Line 435 | Line 561 | public class ConcurrentHashMapV8<K, V>
561           */
562          final void tryAwaitLock(Node[] tab, int i) {
563              if (tab != null && i >= 0 && i < tab.length) { // bounds check
564 +                int r = ThreadLocalRandom.current().nextInt(); // randomize spins
565                  int spins = MAX_SPINS, h;
566                  while (tabAt(tab, i) == this && ((h = hash) & LOCKED) != 0) {
567                      if (spins >= 0) {
568 <                        if (--spins == MAX_SPINS >>> 1)
569 <                            Thread.yield();  // heuristically yield mid-way
568 >                        r ^= r << 1; r ^= r >>> 3; r ^= r << 10; // xorshift
569 >                        if (r >= 0 && --spins == 0)
570 >                            Thread.yield();  // yield before block
571                      }
572                      else if (casHash(h, h | WAITING)) {
573                          synchronized (this) {
# Line 476 | Line 604 | public class ConcurrentHashMapV8<K, V>
604          }
605      }
606  
607 <    /* ---------------- Table element access -------------- */
607 >    /* ---------------- TreeBins -------------- */
608  
609 <    /*
610 <     * Volatile access methods are used for table elements as well as
483 <     * elements of in-progress next table while resizing.  Uses are
484 <     * null checked by callers, and implicitly bounds-checked, relying
485 <     * on the invariants that tab arrays have non-zero size, and all
486 <     * indices are masked with (tab.length - 1) which is never
487 <     * negative and always less than length. Note that, to be correct
488 <     * wrt arbitrary concurrency errors by users, bounds checks must
489 <     * operate on local variables, which accounts for some odd-looking
490 <     * inline assignments below.
609 >    /**
610 >     * Nodes for use in TreeBins
611       */
612 <
613 <    static final Node tabAt(Node[] tab, int i) { // used by InternalIterator
614 <        return (Node)UNSAFE.getObjectVolatile(tab, ((long)i<<ASHIFT)+ABASE);
612 >    static final class TreeNode extends Node {
613 >        TreeNode parent;  // red-black tree links
614 >        TreeNode left;
615 >        TreeNode right;
616 >        TreeNode prev;    // needed to unlink next upon deletion
617 >        boolean red;
618 >
619 >        TreeNode(int hash, Object key, Object val, Node next, TreeNode parent) {
620 >            super(hash, key, val, next);
621 >            this.parent = parent;
622 >        }
623      }
624  
625 <    private static final boolean casTabAt(Node[] tab, int i, Node c, Node v) {
626 <        return UNSAFE.compareAndSwapObject(tab, ((long)i<<ASHIFT)+ABASE, c, v);
627 <    }
625 >    /**
626 >     * A specialized form of red-black tree for use in bins
627 >     * whose size exceeds a threshold.
628 >     *
629 >     * TreeBins use a special form of comparison for search and
630 >     * related operations (which is the main reason we cannot use
631 >     * existing collections such as TreeMaps). TreeBins contain
632 >     * Comparable elements, but may contain others, as well as
633 >     * elements that are Comparable but not necessarily Comparable<T>
634 >     * for the same T, so we cannot invoke compareTo among them. To
635 >     * handle this, the tree is ordered primarily by hash value, then
636 >     * by getClass().getName() order, and then by Comparator order
637 >     * among elements of the same class.  On lookup at a node, if
638 >     * elements are not comparable or compare as 0, both left and
639 >     * right children may need to be searched in the case of tied hash
640 >     * values. (This corresponds to the full list search that would be
641 >     * necessary if all elements were non-Comparable and had tied
642 >     * hashes.)  The red-black balancing code is updated from
643 >     * pre-jdk-collections
644 >     * (http://gee.cs.oswego.edu/dl/classes/collections/RBCell.java)
645 >     * based in turn on Cormen, Leiserson, and Rivest "Introduction to
646 >     * Algorithms" (CLR).
647 >     *
648 >     * TreeBins also maintain a separate locking discipline than
649 >     * regular bins. Because they are forwarded via special MOVED
650 >     * nodes at bin heads (which can never change once established),
651 >     * we cannot use use those nodes as locks. Instead, TreeBin
652 >     * extends AbstractQueuedSynchronizer to support a simple form of
653 >     * read-write lock. For update operations and table validation,
654 >     * the exclusive form of lock behaves in the same way as bin-head
655 >     * locks. However, lookups use shared read-lock mechanics to allow
656 >     * multiple readers in the absence of writers.  Additionally,
657 >     * these lookups do not ever block: While the lock is not
658 >     * available, they proceed along the slow traversal path (via
659 >     * next-pointers) until the lock becomes available or the list is
660 >     * exhausted, whichever comes first. (These cases are not fast,
661 >     * but maximize aggregate expected throughput.)  The AQS mechanics
662 >     * for doing this are straightforward.  The lock state is held as
663 >     * AQS getState().  Read counts are negative; the write count (1)
664 >     * is positive.  There are no signalling preferences among readers
665 >     * and writers. Since we don't need to export full Lock API, we
666 >     * just override the minimal AQS methods and use them directly.
667 >     */
668 >    static final class TreeBin extends AbstractQueuedSynchronizer {
669 >        private static final long serialVersionUID = 2249069246763182397L;
670 >        transient TreeNode root;  // root of tree
671 >        transient TreeNode first; // head of next-pointer list
672  
673 <    private static final void setTabAt(Node[] tab, int i, Node v) {
674 <        UNSAFE.putObjectVolatile(tab, ((long)i<<ASHIFT)+ABASE, v);
675 <    }
673 >        /* AQS overrides */
674 >        public final boolean isHeldExclusively() { return getState() > 0; }
675 >        public final boolean tryAcquire(int ignore) {
676 >            if (compareAndSetState(0, 1)) {
677 >                setExclusiveOwnerThread(Thread.currentThread());
678 >                return true;
679 >            }
680 >            return false;
681 >        }
682 >        public final boolean tryRelease(int ignore) {
683 >            setExclusiveOwnerThread(null);
684 >            setState(0);
685 >            return true;
686 >        }
687 >        public final int tryAcquireShared(int ignore) {
688 >            for (int c;;) {
689 >                if ((c = getState()) > 0)
690 >                    return -1;
691 >                if (compareAndSetState(c, c -1))
692 >                    return 1;
693 >            }
694 >        }
695 >        public final boolean tryReleaseShared(int ignore) {
696 >            int c;
697 >            do {} while (!compareAndSetState(c = getState(), c + 1));
698 >            return c == -1;
699 >        }
700 >
701 >        /** From CLR */
702 >        private void rotateLeft(TreeNode p) {
703 >            if (p != null) {
704 >                TreeNode r = p.right, pp, rl;
705 >                if ((rl = p.right = r.left) != null)
706 >                    rl.parent = p;
707 >                if ((pp = r.parent = p.parent) == null)
708 >                    root = r;
709 >                else if (pp.left == p)
710 >                    pp.left = r;
711 >                else
712 >                    pp.right = r;
713 >                r.left = p;
714 >                p.parent = r;
715 >            }
716 >        }
717  
718 <    /* ---------------- Internal access and update methods -------------- */
718 >        /** From CLR */
719 >        private void rotateRight(TreeNode p) {
720 >            if (p != null) {
721 >                TreeNode l = p.left, pp, lr;
722 >                if ((lr = p.left = l.right) != null)
723 >                    lr.parent = p;
724 >                if ((pp = l.parent = p.parent) == null)
725 >                    root = l;
726 >                else if (pp.right == p)
727 >                    pp.right = l;
728 >                else
729 >                    pp.left = l;
730 >                l.right = p;
731 >                p.parent = l;
732 >            }
733 >        }
734  
735 <    /**
736 <     * Applies a supplemental hash function to a given hashCode, which
737 <     * defends against poor quality hash functions.  The result must
738 <     * be have the top 2 bits clear. For reasonable performance, this
739 <     * function must have good avalanche properties; i.e., that each
740 <     * bit of the argument affects each bit of the result. (Although
741 <     * we don't care about the unused top 2 bits.)
735 >        /**
736 >         * Return the TreeNode (or null if not found) for the given key
737 >         * starting at given root.
738 >         */
739 >        @SuppressWarnings("unchecked") // suppress Comparable cast warning
740 >        final TreeNode getTreeNode(int h, Object k, TreeNode p) {
741 >            Class<?> c = k.getClass();
742 >            while (p != null) {
743 >                int dir, ph;  Object pk; Class<?> pc;
744 >                if ((ph = p.hash) == h) {
745 >                    if ((pk = p.key) == k || k.equals(pk))
746 >                        return p;
747 >                    if (c != (pc = pk.getClass()) ||
748 >                        !(k instanceof Comparable) ||
749 >                        (dir = ((Comparable)k).compareTo((Comparable)pk)) == 0) {
750 >                        dir = (c == pc) ? 0 : c.getName().compareTo(pc.getName());
751 >                        TreeNode r = null, s = null, pl, pr;
752 >                        if (dir >= 0) {
753 >                            if ((pl = p.left) != null && h <= pl.hash)
754 >                                s = pl;
755 >                        }
756 >                        else if ((pr = p.right) != null && h >= pr.hash)
757 >                            s = pr;
758 >                        if (s != null && (r = getTreeNode(h, k, s)) != null)
759 >                            return r;
760 >                    }
761 >                }
762 >                else
763 >                    dir = (h < ph) ? -1 : 1;
764 >                p = (dir > 0) ? p.right : p.left;
765 >            }
766 >            return null;
767 >        }
768 >
769 >        /**
770 >         * Wrapper for getTreeNode used by CHM.get. Tries to obtain
771 >         * read-lock to call getTreeNode, but during failure to get
772 >         * lock, searches along next links.
773 >         */
774 >        final Object getValue(int h, Object k) {
775 >            Node r = null;
776 >            int c = getState(); // Must read lock state first
777 >            for (Node e = first; e != null; e = e.next) {
778 >                if (c <= 0 && compareAndSetState(c, c - 1)) {
779 >                    try {
780 >                        r = getTreeNode(h, k, root);
781 >                    } finally {
782 >                        releaseShared(0);
783 >                    }
784 >                    break;
785 >                }
786 >                else if ((e.hash & HASH_BITS) == h && k.equals(e.key)) {
787 >                    r = e;
788 >                    break;
789 >                }
790 >                else
791 >                    c = getState();
792 >            }
793 >            return r == null ? null : r.val;
794 >        }
795 >
796 >        /**
797 >         * Finds or adds a node.
798 >         * @return null if added
799 >         */
800 >        @SuppressWarnings("unchecked") // suppress Comparable cast warning
801 >        final TreeNode putTreeNode(int h, Object k, Object v) {
802 >            Class<?> c = k.getClass();
803 >            TreeNode pp = root, p = null;
804 >            int dir = 0;
805 >            while (pp != null) { // find existing node or leaf to insert at
806 >                int ph;  Object pk; Class<?> pc;
807 >                p = pp;
808 >                if ((ph = p.hash) == h) {
809 >                    if ((pk = p.key) == k || k.equals(pk))
810 >                        return p;
811 >                    if (c != (pc = pk.getClass()) ||
812 >                        !(k instanceof Comparable) ||
813 >                        (dir = ((Comparable)k).compareTo((Comparable)pk)) == 0) {
814 >                        dir = (c == pc) ? 0 : c.getName().compareTo(pc.getName());
815 >                        TreeNode r = null, s = null, pl, pr;
816 >                        if (dir >= 0) {
817 >                            if ((pl = p.left) != null && h <= pl.hash)
818 >                                s = pl;
819 >                        }
820 >                        else if ((pr = p.right) != null && h >= pr.hash)
821 >                            s = pr;
822 >                        if (s != null && (r = getTreeNode(h, k, s)) != null)
823 >                            return r;
824 >                    }
825 >                }
826 >                else
827 >                    dir = (h < ph) ? -1 : 1;
828 >                pp = (dir > 0) ? p.right : p.left;
829 >            }
830 >
831 >            TreeNode f = first;
832 >            TreeNode x = first = new TreeNode(h, k, v, f, p);
833 >            if (p == null)
834 >                root = x;
835 >            else { // attach and rebalance; adapted from CLR
836 >                TreeNode xp, xpp;
837 >                if (f != null)
838 >                    f.prev = x;
839 >                if (dir <= 0)
840 >                    p.left = x;
841 >                else
842 >                    p.right = x;
843 >                x.red = true;
844 >                while (x != null && (xp = x.parent) != null && xp.red &&
845 >                       (xpp = xp.parent) != null) {
846 >                    TreeNode xppl = xpp.left;
847 >                    if (xp == xppl) {
848 >                        TreeNode y = xpp.right;
849 >                        if (y != null && y.red) {
850 >                            y.red = false;
851 >                            xp.red = false;
852 >                            xpp.red = true;
853 >                            x = xpp;
854 >                        }
855 >                        else {
856 >                            if (x == xp.right) {
857 >                                rotateLeft(x = xp);
858 >                                xpp = (xp = x.parent) == null ? null : xp.parent;
859 >                            }
860 >                            if (xp != null) {
861 >                                xp.red = false;
862 >                                if (xpp != null) {
863 >                                    xpp.red = true;
864 >                                    rotateRight(xpp);
865 >                                }
866 >                            }
867 >                        }
868 >                    }
869 >                    else {
870 >                        TreeNode y = xppl;
871 >                        if (y != null && y.red) {
872 >                            y.red = false;
873 >                            xp.red = false;
874 >                            xpp.red = true;
875 >                            x = xpp;
876 >                        }
877 >                        else {
878 >                            if (x == xp.left) {
879 >                                rotateRight(x = xp);
880 >                                xpp = (xp = x.parent) == null ? null : xp.parent;
881 >                            }
882 >                            if (xp != null) {
883 >                                xp.red = false;
884 >                                if (xpp != null) {
885 >                                    xpp.red = true;
886 >                                    rotateLeft(xpp);
887 >                                }
888 >                            }
889 >                        }
890 >                    }
891 >                }
892 >                TreeNode r = root;
893 >                if (r != null && r.red)
894 >                    r.red = false;
895 >            }
896 >            return null;
897 >        }
898 >
899 >        /**
900 >         * Removes the given node, that must be present before this
901 >         * call.  This is messier than typical red-black deletion code
902 >         * because we cannot swap the contents of an interior node
903 >         * with a leaf successor that is pinned by "next" pointers
904 >         * that are accessible independently of lock. So instead we
905 >         * swap the tree linkages.
906 >         */
907 >        final void deleteTreeNode(TreeNode p) {
908 >            TreeNode next = (TreeNode)p.next; // unlink traversal pointers
909 >            TreeNode pred = p.prev;
910 >            if (pred == null)
911 >                first = next;
912 >            else
913 >                pred.next = next;
914 >            if (next != null)
915 >                next.prev = pred;
916 >            TreeNode replacement;
917 >            TreeNode pl = p.left;
918 >            TreeNode pr = p.right;
919 >            if (pl != null && pr != null) {
920 >                TreeNode s = pr, sl;
921 >                while ((sl = s.left) != null) // find successor
922 >                    s = sl;
923 >                boolean c = s.red; s.red = p.red; p.red = c; // swap colors
924 >                TreeNode sr = s.right;
925 >                TreeNode pp = p.parent;
926 >                if (s == pr) { // p was s's direct parent
927 >                    p.parent = s;
928 >                    s.right = p;
929 >                }
930 >                else {
931 >                    TreeNode sp = s.parent;
932 >                    if ((p.parent = sp) != null) {
933 >                        if (s == sp.left)
934 >                            sp.left = p;
935 >                        else
936 >                            sp.right = p;
937 >                    }
938 >                    if ((s.right = pr) != null)
939 >                        pr.parent = s;
940 >                }
941 >                p.left = null;
942 >                if ((p.right = sr) != null)
943 >                    sr.parent = p;
944 >                if ((s.left = pl) != null)
945 >                    pl.parent = s;
946 >                if ((s.parent = pp) == null)
947 >                    root = s;
948 >                else if (p == pp.left)
949 >                    pp.left = s;
950 >                else
951 >                    pp.right = s;
952 >                replacement = sr;
953 >            }
954 >            else
955 >                replacement = (pl != null) ? pl : pr;
956 >            TreeNode pp = p.parent;
957 >            if (replacement == null) {
958 >                if (pp == null) {
959 >                    root = null;
960 >                    return;
961 >                }
962 >                replacement = p;
963 >            }
964 >            else {
965 >                replacement.parent = pp;
966 >                if (pp == null)
967 >                    root = replacement;
968 >                else if (p == pp.left)
969 >                    pp.left = replacement;
970 >                else
971 >                    pp.right = replacement;
972 >                p.left = p.right = p.parent = null;
973 >            }
974 >            if (!p.red) { // rebalance, from CLR
975 >                TreeNode x = replacement;
976 >                while (x != null) {
977 >                    TreeNode xp, xpl;
978 >                    if (x.red || (xp = x.parent) == null) {
979 >                        x.red = false;
980 >                        break;
981 >                    }
982 >                    if (x == (xpl = xp.left)) {
983 >                        TreeNode sib = xp.right;
984 >                        if (sib != null && sib.red) {
985 >                            sib.red = false;
986 >                            xp.red = true;
987 >                            rotateLeft(xp);
988 >                            sib = (xp = x.parent) == null ? null : xp.right;
989 >                        }
990 >                        if (sib == null)
991 >                            x = xp;
992 >                        else {
993 >                            TreeNode sl = sib.left, sr = sib.right;
994 >                            if ((sr == null || !sr.red) &&
995 >                                (sl == null || !sl.red)) {
996 >                                sib.red = true;
997 >                                x = xp;
998 >                            }
999 >                            else {
1000 >                                if (sr == null || !sr.red) {
1001 >                                    if (sl != null)
1002 >                                        sl.red = false;
1003 >                                    sib.red = true;
1004 >                                    rotateRight(sib);
1005 >                                    sib = (xp = x.parent) == null ? null : xp.right;
1006 >                                }
1007 >                                if (sib != null) {
1008 >                                    sib.red = (xp == null) ? false : xp.red;
1009 >                                    if ((sr = sib.right) != null)
1010 >                                        sr.red = false;
1011 >                                }
1012 >                                if (xp != null) {
1013 >                                    xp.red = false;
1014 >                                    rotateLeft(xp);
1015 >                                }
1016 >                                x = root;
1017 >                            }
1018 >                        }
1019 >                    }
1020 >                    else { // symmetric
1021 >                        TreeNode sib = xpl;
1022 >                        if (sib != null && sib.red) {
1023 >                            sib.red = false;
1024 >                            xp.red = true;
1025 >                            rotateRight(xp);
1026 >                            sib = (xp = x.parent) == null ? null : xp.left;
1027 >                        }
1028 >                        if (sib == null)
1029 >                            x = xp;
1030 >                        else {
1031 >                            TreeNode sl = sib.left, sr = sib.right;
1032 >                            if ((sl == null || !sl.red) &&
1033 >                                (sr == null || !sr.red)) {
1034 >                                sib.red = true;
1035 >                                x = xp;
1036 >                            }
1037 >                            else {
1038 >                                if (sl == null || !sl.red) {
1039 >                                    if (sr != null)
1040 >                                        sr.red = false;
1041 >                                    sib.red = true;
1042 >                                    rotateLeft(sib);
1043 >                                    sib = (xp = x.parent) == null ? null : xp.left;
1044 >                                }
1045 >                                if (sib != null) {
1046 >                                    sib.red = (xp == null) ? false : xp.red;
1047 >                                    if ((sl = sib.left) != null)
1048 >                                        sl.red = false;
1049 >                                }
1050 >                                if (xp != null) {
1051 >                                    xp.red = false;
1052 >                                    rotateRight(xp);
1053 >                                }
1054 >                                x = root;
1055 >                            }
1056 >                        }
1057 >                    }
1058 >                }
1059 >            }
1060 >            if (p == replacement && (pp = p.parent) != null) {
1061 >                if (p == pp.left) // detach pointers
1062 >                    pp.left = null;
1063 >                else if (p == pp.right)
1064 >                    pp.right = null;
1065 >                p.parent = null;
1066 >            }
1067 >        }
1068 >    }
1069 >
1070 >    /* ---------------- Collision reduction methods -------------- */
1071 >
1072 >    /**
1073 >     * Spreads higher bits to lower, and also forces top 2 bits to 0.
1074 >     * Because the table uses power-of-two masking, sets of hashes
1075 >     * that vary only in bits above the current mask will always
1076 >     * collide. (Among known examples are sets of Float keys holding
1077 >     * consecutive whole numbers in small tables.)  To counter this,
1078 >     * we apply a transform that spreads the impact of higher bits
1079 >     * downward. There is a tradeoff between speed, utility, and
1080 >     * quality of bit-spreading. Because many common sets of hashes
1081 >     * are already reasonably distributed across bits (so don't benefit
1082 >     * from spreading), and because we use trees to handle large sets
1083 >     * of collisions in bins, we don't need excessively high quality.
1084       */
1085      private static final int spread(int h) {
1086 <        // Apply base step of MurmurHash; see http://code.google.com/p/smhasher/
1087 <        // Despite two multiplies, this is often faster than others
1088 <        // with comparable bit-spread properties.
1089 <        h ^= h >>> 16;
1090 <        h *= 0x85ebca6b;
1091 <        h ^= h >>> 13;
1092 <        h *= 0xc2b2ae35;
1093 <        return ((h >>> 16) ^ h) & HASH_BITS; // mask out top bits
1086 >        h ^= (h >>> 18) ^ (h >>> 12);
1087 >        return (h ^ (h >>> 10)) & HASH_BITS;
1088 >    }
1089 >
1090 >    /**
1091 >     * Replaces a list bin with a tree bin. Call only when locked.
1092 >     * Fails to replace if the given key is non-comparable or table
1093 >     * is, or needs, resizing.
1094 >     */
1095 >    private final void replaceWithTreeBin(Node[] tab, int index, Object key) {
1096 >        if ((key instanceof Comparable) &&
1097 >            (tab.length >= MAXIMUM_CAPACITY || counter.sum() < (long)sizeCtl)) {
1098 >            TreeBin t = new TreeBin();
1099 >            for (Node e = tabAt(tab, index); e != null; e = e.next)
1100 >                t.putTreeNode(e.hash & HASH_BITS, e.key, e.val);
1101 >            setTabAt(tab, index, new Node(MOVED, t, null, null));
1102 >        }
1103      }
1104  
1105 +    /* ---------------- Internal access and update methods -------------- */
1106 +
1107      /** Implementation for get and containsKey */
1108      private final Object internalGet(Object k) {
1109          int h = spread(k.hashCode());
1110          retry: for (Node[] tab = table; tab != null;) {
1111 <            Node e; Object ek, ev; int eh;    // locals to read fields once
1111 >            Node e, p; Object ek, ev; int eh;      // locals to read fields once
1112              for (e = tabAt(tab, (tab.length - 1) & h); e != null; e = e.next) {
1113                  if ((eh = e.hash) == MOVED) {
1114 <                    tab = (Node[])e.key;      // restart with new table
1115 <                    continue retry;
1114 >                    if ((ek = e.key) instanceof TreeBin)  // search TreeBin
1115 >                        return ((TreeBin)ek).getValue(h, k);
1116 >                    else {                        // restart with new table
1117 >                        tab = (Node[])ek;
1118 >                        continue retry;
1119 >                    }
1120                  }
1121 <                if ((eh & HASH_BITS) == h && (ev = e.val) != null &&
1122 <                    ((ek = e.key) == k || k.equals(ek)))
1121 >                else if ((eh & HASH_BITS) == h && (ev = e.val) != null &&
1122 >                         ((ek = e.key) == k || k.equals(ek)))
1123                      return ev;
1124              }
1125              break;
# Line 551 | Line 1136 | public class ConcurrentHashMapV8<K, V>
1136          int h = spread(k.hashCode());
1137          Object oldVal = null;
1138          for (Node[] tab = table;;) {
1139 <            Node f; int i, fh;
1139 >            Node f; int i, fh; Object fk;
1140              if (tab == null ||
1141                  (f = tabAt(tab, i = (tab.length - 1) & h)) == null)
1142                  break;
1143 <            else if ((fh = f.hash) == MOVED)
1144 <                tab = (Node[])f.key;
1143 >            else if ((fh = f.hash) == MOVED) {
1144 >                if ((fk = f.key) instanceof TreeBin) {
1145 >                    TreeBin t = (TreeBin)fk;
1146 >                    boolean validated = false;
1147 >                    boolean deleted = false;
1148 >                    t.acquire(0);
1149 >                    try {
1150 >                        if (tabAt(tab, i) == f) {
1151 >                            validated = true;
1152 >                            TreeNode p = t.getTreeNode(h, k, t.root);
1153 >                            if (p != null) {
1154 >                                Object pv = p.val;
1155 >                                if (cv == null || cv == pv || cv.equals(pv)) {
1156 >                                    oldVal = pv;
1157 >                                    if ((p.val = v) == null) {
1158 >                                        deleted = true;
1159 >                                        t.deleteTreeNode(p);
1160 >                                    }
1161 >                                }
1162 >                            }
1163 >                        }
1164 >                    } finally {
1165 >                        t.release(0);
1166 >                    }
1167 >                    if (validated) {
1168 >                        if (deleted)
1169 >                            counter.add(-1L);
1170 >                        break;
1171 >                    }
1172 >                }
1173 >                else
1174 >                    tab = (Node[])fk;
1175 >            }
1176              else if ((fh & HASH_BITS) != h && f.next == null) // precheck
1177                  break;                          // rules out possible existence
1178              else if ((fh & LOCKED) != 0) {
# Line 615 | Line 1231 | public class ConcurrentHashMapV8<K, V>
1231       *  1. If table uninitialized, create
1232       *  2. If bin empty, try to CAS new node
1233       *  3. If bin stale, use new table
1234 <     *  4. Lock and validate; if valid, scan and add or update
1234 >     *  4. if bin converted to TreeBin, validate and relay to TreeBin methods
1235 >     *  5. Lock and validate; if valid, scan and add or update
1236       *
1237       * The others interweave other checks and/or alternative actions:
1238       *  * Plain put checks for and performs resize after insertion.
# Line 636 | Line 1253 | public class ConcurrentHashMapV8<K, V>
1253      /** Implementation for put */
1254      private final Object internalPut(Object k, Object v) {
1255          int h = spread(k.hashCode());
1256 <        boolean checkSize = false;
1256 >        int count = 0;
1257          for (Node[] tab = table;;) {
1258 <            int i; Node f; int fh;
1258 >            int i; Node f; int fh; Object fk;
1259              if (tab == null)
1260                  tab = initTable();
1261              else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1262                  if (casTabAt(tab, i, null, new Node(h, k, v, null)))
1263                      break;                   // no lock when adding to empty bin
1264              }
1265 <            else if ((fh = f.hash) == MOVED)
1266 <                tab = (Node[])f.key;
1265 >            else if ((fh = f.hash) == MOVED) {
1266 >                if ((fk = f.key) instanceof TreeBin) {
1267 >                    TreeBin t = (TreeBin)fk;
1268 >                    Object oldVal = null;
1269 >                    t.acquire(0);
1270 >                    try {
1271 >                        if (tabAt(tab, i) == f) {
1272 >                            count = 2;
1273 >                            TreeNode p = t.putTreeNode(h, k, v);
1274 >                            if (p != null) {
1275 >                                oldVal = p.val;
1276 >                                p.val = v;
1277 >                            }
1278 >                        }
1279 >                    } finally {
1280 >                        t.release(0);
1281 >                    }
1282 >                    if (count != 0) {
1283 >                        if (oldVal != null)
1284 >                            return oldVal;
1285 >                        break;
1286 >                    }
1287 >                }
1288 >                else
1289 >                    tab = (Node[])fk;
1290 >            }
1291              else if ((fh & LOCKED) != 0) {
1292                  checkForResize();
1293                  f.tryAwaitLock(tab, i);
1294              }
1295              else if (f.casHash(fh, fh | LOCKED)) {
1296                  Object oldVal = null;
656                boolean validated = false;
1297                  try {                        // needed in case equals() throws
1298                      if (tabAt(tab, i) == f) {
1299 <                        validated = true;    // retry if 1st already deleted
1300 <                        for (Node e = f;;) {
1299 >                        count = 1;
1300 >                        for (Node e = f;; ++count) {
1301                              Object ek, ev;
1302                              if ((e.hash & HASH_BITS) == h &&
1303                                  (ev = e.val) != null &&
# Line 669 | Line 1309 | public class ConcurrentHashMapV8<K, V>
1309                              Node last = e;
1310                              if ((e = e.next) == null) {
1311                                  last.next = new Node(h, k, v, null);
1312 <                                if (last != f || tab.length <= 64)
1313 <                                    checkSize = true;
1312 >                                if (count >= TREE_THRESHOLD)
1313 >                                    replaceWithTreeBin(tab, i, k);
1314                                  break;
1315                              }
1316                          }
# Line 681 | Line 1321 | public class ConcurrentHashMapV8<K, V>
1321                          synchronized (f) { f.notifyAll(); };
1322                      }
1323                  }
1324 <                if (validated) {
1324 >                if (count != 0) {
1325                      if (oldVal != null)
1326                          return oldVal;
1327 +                    if (tab.length <= 64)
1328 +                        count = 2;
1329                      break;
1330                  }
1331              }
1332          }
1333          counter.add(1L);
1334 <        if (checkSize)
1334 >        if (count > 1)
1335              checkForResize();
1336          return null;
1337      }
# Line 697 | Line 1339 | public class ConcurrentHashMapV8<K, V>
1339      /** Implementation for putIfAbsent */
1340      private final Object internalPutIfAbsent(Object k, Object v) {
1341          int h = spread(k.hashCode());
1342 +        int count = 0;
1343          for (Node[] tab = table;;) {
1344              int i; Node f; int fh; Object fk, fv;
1345              if (tab == null)
# Line 705 | Line 1348 | public class ConcurrentHashMapV8<K, V>
1348                  if (casTabAt(tab, i, null, new Node(h, k, v, null)))
1349                      break;
1350              }
1351 <            else if ((fh = f.hash) == MOVED)
1352 <                tab = (Node[])f.key;
1351 >            else if ((fh = f.hash) == MOVED) {
1352 >                if ((fk = f.key) instanceof TreeBin) {
1353 >                    TreeBin t = (TreeBin)fk;
1354 >                    Object oldVal = null;
1355 >                    t.acquire(0);
1356 >                    try {
1357 >                        if (tabAt(tab, i) == f) {
1358 >                            count = 2;
1359 >                            TreeNode p = t.putTreeNode(h, k, v);
1360 >                            if (p != null)
1361 >                                oldVal = p.val;
1362 >                        }
1363 >                    } finally {
1364 >                        t.release(0);
1365 >                    }
1366 >                    if (count != 0) {
1367 >                        if (oldVal != null)
1368 >                            return oldVal;
1369 >                        break;
1370 >                    }
1371 >                }
1372 >                else
1373 >                    tab = (Node[])fk;
1374 >            }
1375              else if ((fh & HASH_BITS) == h && (fv = f.val) != null &&
1376                       ((fk = f.key) == k || k.equals(fk)))
1377                  return fv;
# Line 730 | Line 1395 | public class ConcurrentHashMapV8<K, V>
1395                  }
1396                  else if (tabAt(tab, i) == f && f.casHash(fh, fh | LOCKED)) {
1397                      Object oldVal = null;
733                    boolean validated = false;
1398                      try {
1399                          if (tabAt(tab, i) == f) {
1400 <                            validated = true;
1401 <                            for (Node e = f;;) {
1400 >                            count = 1;
1401 >                            for (Node e = f;; ++count) {
1402                                  Object ek, ev;
1403                                  if ((e.hash & HASH_BITS) == h &&
1404                                      (ev = e.val) != null &&
# Line 745 | Line 1409 | public class ConcurrentHashMapV8<K, V>
1409                                  Node last = e;
1410                                  if ((e = e.next) == null) {
1411                                      last.next = new Node(h, k, v, null);
1412 +                                    if (count >= TREE_THRESHOLD)
1413 +                                        replaceWithTreeBin(tab, i, k);
1414                                      break;
1415                                  }
1416                              }
# Line 755 | Line 1421 | public class ConcurrentHashMapV8<K, V>
1421                              synchronized (f) { f.notifyAll(); };
1422                          }
1423                      }
1424 <                    if (validated) {
1424 >                    if (count != 0) {
1425                          if (oldVal != null)
1426                              return oldVal;
1427 +                        if (tab.length <= 64)
1428 +                            count = 2;
1429                          break;
1430                      }
1431                  }
1432              }
1433          }
1434          counter.add(1L);
1435 +        if (count > 1)
1436 +            checkForResize();
1437          return null;
1438      }
1439  
# Line 772 | Line 1442 | public class ConcurrentHashMapV8<K, V>
1442                                                   MappingFunction<? super K, ?> mf) {
1443          int h = spread(k.hashCode());
1444          Object val = null;
1445 +        int count = 0;
1446          for (Node[] tab = table;;) {
1447              Node f; int i, fh; Object fk, fv;
1448              if (tab == null)
1449                  tab = initTable();
1450              else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1451                  Node node = new Node(fh = h | LOCKED, k, null, null);
781                boolean validated = false;
1452                  if (casTabAt(tab, i, null, node)) {
1453 <                    validated = true;
1453 >                    count = 1;
1454                      try {
1455                          if ((val = mf.map(k)) != null)
1456                              node.val = val;
# Line 793 | Line 1463 | public class ConcurrentHashMapV8<K, V>
1463                          }
1464                      }
1465                  }
1466 <                if (validated)
1466 >                if (count != 0)
1467                      break;
1468              }
1469 <            else if ((fh = f.hash) == MOVED)
1470 <                tab = (Node[])f.key;
1469 >            else if ((fh = f.hash) == MOVED) {
1470 >                if ((fk = f.key) instanceof TreeBin) {
1471 >                    TreeBin t = (TreeBin)fk;
1472 >                    boolean added = false;
1473 >                    t.acquire(0);
1474 >                    try {
1475 >                        if (tabAt(tab, i) == f) {
1476 >                            count = 1;
1477 >                            TreeNode p = t.getTreeNode(h, k, t.root);
1478 >                            if (p != null)
1479 >                                val = p.val;
1480 >                            else if ((val = mf.map(k)) != null) {
1481 >                                added = true;
1482 >                                count = 2;
1483 >                                t.putTreeNode(h, k, val);
1484 >                            }
1485 >                        }
1486 >                    } finally {
1487 >                        t.release(0);
1488 >                    }
1489 >                    if (count != 0) {
1490 >                        if (!added)
1491 >                            return val;
1492 >                        break;
1493 >                    }
1494 >                }
1495 >                else
1496 >                    tab = (Node[])fk;
1497 >            }
1498              else if ((fh & HASH_BITS) == h && (fv = f.val) != null &&
1499                       ((fk = f.key) == k || k.equals(fk)))
1500                  return fv;
# Line 820 | Line 1517 | public class ConcurrentHashMapV8<K, V>
1517                      f.tryAwaitLock(tab, i);
1518                  }
1519                  else if (tabAt(tab, i) == f && f.casHash(fh, fh | LOCKED)) {
1520 <                    boolean validated = false;
1520 >                    boolean added = false;
1521                      try {
1522                          if (tabAt(tab, i) == f) {
1523 <                            validated = true;
1524 <                            for (Node e = f;;) {
1523 >                            count = 1;
1524 >                            for (Node e = f;; ++count) {
1525                                  Object ek, ev;
1526                                  if ((e.hash & HASH_BITS) == h &&
1527                                      (ev = e.val) != null &&
# Line 834 | Line 1531 | public class ConcurrentHashMapV8<K, V>
1531                                  }
1532                                  Node last = e;
1533                                  if ((e = e.next) == null) {
1534 <                                    if ((val = mf.map(k)) != null)
1534 >                                    if ((val = mf.map(k)) != null) {
1535 >                                        added = true;
1536                                          last.next = new Node(h, k, val, null);
1537 +                                        if (count >= TREE_THRESHOLD)
1538 +                                            replaceWithTreeBin(tab, i, k);
1539 +                                    }
1540                                      break;
1541                                  }
1542                              }
# Line 846 | Line 1547 | public class ConcurrentHashMapV8<K, V>
1547                              synchronized (f) { f.notifyAll(); };
1548                          }
1549                      }
1550 <                    if (validated)
1550 >                    if (count != 0) {
1551 >                        if (!added)
1552 >                            return val;
1553 >                        if (tab.length <= 64)
1554 >                            count = 2;
1555                          break;
1556 +                    }
1557                  }
1558              }
1559          }
1560 <        if (val == null)
1561 <            throw new NullPointerException();
1562 <        counter.add(1L);
1560 >        if (val != null) {
1561 >            counter.add(1L);
1562 >            if (count > 1)
1563 >                checkForResize();
1564 >        }
1565          return val;
1566      }
1567  
# Line 863 | Line 1571 | public class ConcurrentHashMapV8<K, V>
1571                                           RemappingFunction<? super K, V> mf) {
1572          int h = spread(k.hashCode());
1573          Object val = null;
1574 <        boolean added = false;
1575 <        boolean checkSize = false;
1574 >        int delta = 0;
1575 >        int count = 0;
1576          for (Node[] tab = table;;) {
1577 <            Node f; int i, fh;
1577 >            Node f; int i, fh; Object fk;
1578              if (tab == null)
1579                  tab = initTable();
1580              else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1581                  Node node = new Node(fh = h | LOCKED, k, null, null);
874                boolean validated = false;
1582                  if (casTabAt(tab, i, null, node)) {
876                    validated = true;
1583                      try {
1584 +                        count = 1;
1585                          if ((val = mf.remap(k, null)) != null) {
1586                              node.val = val;
1587 <                            added = true;
1587 >                            delta = 1;
1588                          }
1589                      } finally {
1590 <                        if (!added)
1590 >                        if (delta == 0)
1591                              setTabAt(tab, i, null);
1592                          if (!node.casHash(fh, h)) {
1593                              node.hash = h;
# Line 888 | Line 1595 | public class ConcurrentHashMapV8<K, V>
1595                          }
1596                      }
1597                  }
1598 <                if (validated)
1598 >                if (count != 0)
1599                      break;
1600              }
1601 <            else if ((fh = f.hash) == MOVED)
1602 <                tab = (Node[])f.key;
1601 >            else if ((fh = f.hash) == MOVED) {
1602 >                if ((fk = f.key) instanceof TreeBin) {
1603 >                    TreeBin t = (TreeBin)fk;
1604 >                    t.acquire(0);
1605 >                    try {
1606 >                        if (tabAt(tab, i) == f) {
1607 >                            count = 1;
1608 >                            TreeNode p = t.getTreeNode(h, k, t.root);
1609 >                            Object pv = (p == null) ? null : p.val;
1610 >                            if ((val = mf.remap(k, (V)pv)) != null) {
1611 >                                if (p != null)
1612 >                                    p.val = val;
1613 >                                else {
1614 >                                    count = 2;
1615 >                                    delta = 1;
1616 >                                    t.putTreeNode(h, k, val);
1617 >                                }
1618 >                            }
1619 >                            else if (p != null) {
1620 >                                delta = -1;
1621 >                                t.deleteTreeNode(p);
1622 >                            }
1623 >                        }
1624 >                    } finally {
1625 >                        t.release(0);
1626 >                    }
1627 >                    if (count != 0)
1628 >                        break;
1629 >                }
1630 >                else
1631 >                    tab = (Node[])fk;
1632 >            }
1633              else if ((fh & LOCKED) != 0) {
1634                  checkForResize();
1635                  f.tryAwaitLock(tab, i);
1636              }
1637              else if (f.casHash(fh, fh | LOCKED)) {
901                boolean validated = false;
1638                  try {
1639                      if (tabAt(tab, i) == f) {
1640 <                        validated = true;
1641 <                        for (Node e = f;;) {
1640 >                        count = 1;
1641 >                        for (Node e = f, pred = null;; ++count) {
1642                              Object ek, ev;
1643                              if ((e.hash & HASH_BITS) == h &&
1644                                  (ev = e.val) != null &&
# Line 910 | Line 1646 | public class ConcurrentHashMapV8<K, V>
1646                                  val = mf.remap(k, (V)ev);
1647                                  if (val != null)
1648                                      e.val = val;
1649 +                                else {
1650 +                                    delta = -1;
1651 +                                    Node en = e.next;
1652 +                                    if (pred != null)
1653 +                                        pred.next = en;
1654 +                                    else
1655 +                                        setTabAt(tab, i, en);
1656 +                                }
1657                                  break;
1658                              }
1659 <                            Node last = e;
1659 >                            pred = e;
1660                              if ((e = e.next) == null) {
1661                                  if ((val = mf.remap(k, null)) != null) {
1662 <                                    last.next = new Node(h, k, val, null);
1663 <                                    added = true;
1664 <                                    if (last != f || tab.length <= 64)
1665 <                                        checkSize = true;
1662 >                                    pred.next = new Node(h, k, val, null);
1663 >                                    delta = 1;
1664 >                                    if (count >= TREE_THRESHOLD)
1665 >                                        replaceWithTreeBin(tab, i, k);
1666                                  }
1667                                  break;
1668                              }
# Line 930 | Line 1674 | public class ConcurrentHashMapV8<K, V>
1674                          synchronized (f) { f.notifyAll(); };
1675                      }
1676                  }
1677 <                if (validated)
1677 >                if (count != 0) {
1678 >                    if (tab.length <= 64)
1679 >                        count = 2;
1680                      break;
1681 +                }
1682              }
1683          }
1684 <        if (val == null)
1685 <            throw new NullPointerException();
1686 <        if (added) {
940 <            counter.add(1L);
941 <            if (checkSize)
1684 >        if (delta != 0) {
1685 >            counter.add((long)delta);
1686 >            if (count > 1)
1687                  checkForResize();
1688          }
1689          return val;
# Line 959 | Line 1704 | public class ConcurrentHashMapV8<K, V>
1704                  }
1705                  int h = spread(k.hashCode());
1706                  for (Node[] tab = table;;) {
1707 <                    int i; Node f; int fh;
1707 >                    int i; Node f; int fh; Object fk;
1708                      if (tab == null)
1709                          tab = initTable();
1710                      else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null){
# Line 968 | Line 1713 | public class ConcurrentHashMapV8<K, V>
1713                              break;
1714                          }
1715                      }
1716 <                    else if ((fh = f.hash) == MOVED)
1717 <                        tab = (Node[])f.key;
1716 >                    else if ((fh = f.hash) == MOVED) {
1717 >                        if ((fk = f.key) instanceof TreeBin) {
1718 >                            TreeBin t = (TreeBin)fk;
1719 >                            boolean validated = false;
1720 >                            t.acquire(0);
1721 >                            try {
1722 >                                if (tabAt(tab, i) == f) {
1723 >                                    validated = true;
1724 >                                    TreeNode p = t.getTreeNode(h, k, t.root);
1725 >                                    if (p != null)
1726 >                                        p.val = v;
1727 >                                    else {
1728 >                                        t.putTreeNode(h, k, v);
1729 >                                        ++delta;
1730 >                                    }
1731 >                                }
1732 >                            } finally {
1733 >                                t.release(0);
1734 >                            }
1735 >                            if (validated)
1736 >                                break;
1737 >                        }
1738 >                        else
1739 >                            tab = (Node[])fk;
1740 >                    }
1741                      else if ((fh & LOCKED) != 0) {
1742                          counter.add(delta);
1743                          delta = 0L;
# Line 977 | Line 1745 | public class ConcurrentHashMapV8<K, V>
1745                          f.tryAwaitLock(tab, i);
1746                      }
1747                      else if (f.casHash(fh, fh | LOCKED)) {
1748 <                        boolean validated = false;
981 <                        boolean tooLong = false;
1748 >                        int count = 0;
1749                          try {
1750                              if (tabAt(tab, i) == f) {
1751 <                                validated = true;
1752 <                                for (Node e = f;;) {
1751 >                                count = 1;
1752 >                                for (Node e = f;; ++count) {
1753                                      Object ek, ev;
1754                                      if ((e.hash & HASH_BITS) == h &&
1755                                          (ev = e.val) != null &&
# Line 994 | Line 1761 | public class ConcurrentHashMapV8<K, V>
1761                                      if ((e = e.next) == null) {
1762                                          ++delta;
1763                                          last.next = new Node(h, k, v, null);
1764 +                                        if (count >= TREE_THRESHOLD)
1765 +                                            replaceWithTreeBin(tab, i, k);
1766                                          break;
1767                                      }
999                                    tooLong = true;
1768                                  }
1769                              }
1770                          } finally {
# Line 1005 | Line 1773 | public class ConcurrentHashMapV8<K, V>
1773                                  synchronized (f) { f.notifyAll(); };
1774                              }
1775                          }
1776 <                        if (validated) {
1777 <                            if (tooLong) {
1776 >                        if (count != 0) {
1777 >                            if (count > 1) {
1778                                  counter.add(delta);
1779                                  delta = 0L;
1780                                  checkForResize();
# Line 1162 | Line 1930 | public class ConcurrentHashMapV8<K, V>
1930                      }
1931                  }
1932              }
1933 <            else if (((fh = f.hash) & LOCKED) == 0 && f.casHash(fh, fh|LOCKED)) {
1933 >            else if ((fh = f.hash) == MOVED) {
1934 >                Object fk = f.key;
1935 >                if (fk instanceof TreeBin) {
1936 >                    TreeBin t = (TreeBin)fk;
1937 >                    boolean validated = false;
1938 >                    t.acquire(0);
1939 >                    try {
1940 >                        if (tabAt(tab, i) == f) {
1941 >                            validated = true;
1942 >                            splitTreeBin(nextTab, i, t);
1943 >                            setTabAt(tab, i, fwd);
1944 >                        }
1945 >                    } finally {
1946 >                        t.release(0);
1947 >                    }
1948 >                    if (!validated)
1949 >                        continue;
1950 >                }
1951 >            }
1952 >            else if ((fh & LOCKED) == 0 && f.casHash(fh, fh|LOCKED)) {
1953                  boolean validated = false;
1954                  try {              // split to lo and hi lists; copying as needed
1955                      if (tabAt(tab, i) == f) {
1956                          validated = true;
1957 <                        Node e = f, lastRun = f;
1171 <                        Node lo = null, hi = null;
1172 <                        int runBit = e.hash & n;
1173 <                        for (Node p = e.next; p != null; p = p.next) {
1174 <                            int b = p.hash & n;
1175 <                            if (b != runBit) {
1176 <                                runBit = b;
1177 <                                lastRun = p;
1178 <                            }
1179 <                        }
1180 <                        if (runBit == 0)
1181 <                            lo = lastRun;
1182 <                        else
1183 <                            hi = lastRun;
1184 <                        for (Node p = e; p != lastRun; p = p.next) {
1185 <                            int ph = p.hash & HASH_BITS;
1186 <                            Object pk = p.key, pv = p.val;
1187 <                            if ((ph & n) == 0)
1188 <                                lo = new Node(ph, pk, pv, lo);
1189 <                            else
1190 <                                hi = new Node(ph, pk, pv, hi);
1191 <                        }
1192 <                        setTabAt(nextTab, i, lo);
1193 <                        setTabAt(nextTab, i + n, hi);
1957 >                        splitBin(nextTab, i, f);
1958                          setTabAt(tab, i, fwd);
1959                      }
1960                  } finally {
# Line 1236 | Line 2000 | public class ConcurrentHashMapV8<K, V>
2000      }
2001  
2002      /**
2003 +     * Splits a normal bin with list headed by e into lo and hi parts;
2004 +     * installs in given table.
2005 +     */
2006 +    private static void splitBin(Node[] nextTab, int i, Node e) {
2007 +        int bit = nextTab.length >>> 1; // bit to split on
2008 +        int runBit = e.hash & bit;
2009 +        Node lastRun = e, lo = null, hi = null;
2010 +        for (Node p = e.next; p != null; p = p.next) {
2011 +            int b = p.hash & bit;
2012 +            if (b != runBit) {
2013 +                runBit = b;
2014 +                lastRun = p;
2015 +            }
2016 +        }
2017 +        if (runBit == 0)
2018 +            lo = lastRun;
2019 +        else
2020 +            hi = lastRun;
2021 +        for (Node p = e; p != lastRun; p = p.next) {
2022 +            int ph = p.hash & HASH_BITS;
2023 +            Object pk = p.key, pv = p.val;
2024 +            if ((ph & bit) == 0)
2025 +                lo = new Node(ph, pk, pv, lo);
2026 +            else
2027 +                hi = new Node(ph, pk, pv, hi);
2028 +        }
2029 +        setTabAt(nextTab, i, lo);
2030 +        setTabAt(nextTab, i + bit, hi);
2031 +    }
2032 +
2033 +    /**
2034 +     * Splits a tree bin into lo and hi parts; installs in given table.
2035 +     */
2036 +    private static void splitTreeBin(Node[] nextTab, int i, TreeBin t) {
2037 +        int bit = nextTab.length >>> 1;
2038 +        TreeBin lt = new TreeBin();
2039 +        TreeBin ht = new TreeBin();
2040 +        int lc = 0, hc = 0;
2041 +        for (Node e = t.first; e != null; e = e.next) {
2042 +            int h = e.hash & HASH_BITS;
2043 +            Object k = e.key, v = e.val;
2044 +            if ((h & bit) == 0) {
2045 +                ++lc;
2046 +                lt.putTreeNode(h, k, v);
2047 +            }
2048 +            else {
2049 +                ++hc;
2050 +                ht.putTreeNode(h, k, v);
2051 +            }
2052 +        }
2053 +        Node ln, hn; // throw away trees if too small
2054 +        if (lc <= (TREE_THRESHOLD >>> 1)) {
2055 +            ln = null;
2056 +            for (Node p = lt.first; p != null; p = p.next)
2057 +                ln = new Node(p.hash, p.key, p.val, ln);
2058 +        }
2059 +        else
2060 +            ln = new Node(MOVED, lt, null, null);
2061 +        setTabAt(nextTab, i, ln);
2062 +        if (hc <= (TREE_THRESHOLD >>> 1)) {
2063 +            hn = null;
2064 +            for (Node p = ht.first; p != null; p = p.next)
2065 +                hn = new Node(p.hash, p.key, p.val, hn);
2066 +        }
2067 +        else
2068 +            hn = new Node(MOVED, ht, null, null);
2069 +        setTabAt(nextTab, i + bit, hn);
2070 +    }
2071 +
2072 +    /**
2073       * Implementation for clear. Steps through each bin, removing all
2074       * nodes.
2075       */
# Line 1244 | Line 2078 | public class ConcurrentHashMapV8<K, V>
2078          int i = 0;
2079          Node[] tab = table;
2080          while (tab != null && i < tab.length) {
2081 <            int fh;
2081 >            int fh; Object fk;
2082              Node f = tabAt(tab, i);
2083              if (f == null)
2084                  ++i;
2085 <            else if ((fh = f.hash) == MOVED)
2086 <                tab = (Node[])f.key;
2085 >            else if ((fh = f.hash) == MOVED) {
2086 >                if ((fk = f.key) instanceof TreeBin) {
2087 >                    TreeBin t = (TreeBin)fk;
2088 >                    t.acquire(0);
2089 >                    try {
2090 >                        if (tabAt(tab, i) == f) {
2091 >                            for (Node p = t.first; p != null; p = p.next) {
2092 >                                p.val = null;
2093 >                                --delta;
2094 >                            }
2095 >                            t.first = null;
2096 >                            t.root = null;
2097 >                            ++i;
2098 >                        }
2099 >                    } finally {
2100 >                        t.release(0);
2101 >                    }
2102 >                }
2103 >                else
2104 >                    tab = (Node[])fk;
2105 >            }
2106              else if ((fh & LOCKED) != 0) {
2107                  counter.add(delta); // opportunistically update count
2108                  delta = 0L;
2109                  f.tryAwaitLock(tab, i);
2110              }
2111              else if (f.casHash(fh, fh | LOCKED)) {
1259                boolean validated = false;
2112                  try {
2113                      if (tabAt(tab, i) == f) {
1262                        validated = true;
2114                          for (Node e = f; e != null; e = e.next) {
2115 <                            if (e.val != null) { // currently always true
2116 <                                e.val = null;
1266 <                                --delta;
1267 <                            }
2115 >                            e.val = null;
2116 >                            --delta;
2117                          }
2118                          setTabAt(tab, i, null);
2119 +                        ++i;
2120                      }
2121                  } finally {
2122                      if (!f.casHash(fh | LOCKED, fh)) {
# Line 1274 | Line 2124 | public class ConcurrentHashMapV8<K, V>
2124                          synchronized (f) { f.notifyAll(); };
2125                      }
2126                  }
1277                if (validated)
1278                    ++i;
2127              }
2128          }
2129          if (delta != 0)
2130              counter.add(delta);
2131      }
2132  
1285
2133      /* ----------------Table Traversal -------------- */
2134  
2135      /**
# Line 1291 | Line 2138 | public class ConcurrentHashMapV8<K, V>
2138       *
2139       * At each step, the iterator snapshots the key ("nextKey") and
2140       * value ("nextVal") of a valid node (i.e., one that, at point of
2141 <     * snapshot, has a nonnull user value). Because val fields can
2141 >     * snapshot, has a non-null user value). Because val fields can
2142       * change (including to null, indicating deletion), field nextVal
2143       * might not be accurate at point of use, but still maintains the
2144       * weak consistency property of holding a value that was once
2145       * valid.
2146       *
2147       * Internal traversals directly access these fields, as in:
2148 <     * {@code while (it.next != null) { process(it.nextKey); it.advance(); }}
2148 >     * {@code while (it.advance() != null) { process(it.nextKey); }}
2149       *
2150 <     * Exported iterators (subclasses of ViewIterator) extract key,
2151 <     * value, or key-value pairs as return values of Iterator.next(),
2152 <     * and encapsulate the it.next check as hasNext();
2150 >     * Exported iterators must track whether the iterator has advanced
2151 >     * (in hasNext vs next) (by setting/checking/nulling field
2152 >     * nextVal), and then extract key, value, or key-value pairs as
2153 >     * return values of next().
2154       *
2155       * The iterator visits once each still-valid node that was
2156       * reachable upon iterator construction. It might miss some that
# Line 1320 | Line 2168 | public class ConcurrentHashMapV8<K, V>
2168       * paranoically cope with potential sharing by users of iterators
2169       * across threads, iteration terminates if a bounds checks fails
2170       * for a table read.
1323     *
1324     * The range-based constructor enables creation of parallel
1325     * range-splitting traversals. (Not yet implemented.)
2171       */
2172 <    static class InternalIterator {
2172 >    static class InternalIterator<K,V> {
2173 >        final ConcurrentHashMapV8<K, V> map;
2174          Node next;           // the next entry to use
2175          Node last;           // the last entry used
2176          Object nextKey;      // cached key field of next
# Line 1332 | Line 2178 | public class ConcurrentHashMapV8<K, V>
2178          Node[] tab;          // current table; updated if resized
2179          int index;           // index of bin to use next
2180          int baseIndex;       // current index of initial table
2181 <        final int baseLimit; // index bound for initial table
2181 >        int baseLimit;       // index bound for initial table
2182          final int baseSize;  // initial table size
2183  
2184          /** Creates iterator for all entries in the table. */
2185 <        InternalIterator(Node[] tab) {
2186 <            this.tab = tab;
2185 >        InternalIterator(ConcurrentHashMapV8<K, V> map) {
2186 >            this.tab = (this.map = map).table;
2187              baseLimit = baseSize = (tab == null) ? 0 : tab.length;
1342            index = baseIndex = 0;
1343            next = null;
1344            advance();
1345        }
1346
1347        /** Creates iterator for the given range of the table */
1348        InternalIterator(Node[] tab, int lo, int hi) {
1349            this.tab = tab;
1350            baseSize = (tab == null) ? 0 : tab.length;
1351            baseLimit = (hi <= baseSize) ? hi : baseSize;
1352            index = baseIndex = (lo >= 0) ? lo : 0;
1353            next = null;
1354            advance();
2188          }
2189  
2190 <        /** Advances next. See above for explanation. */
2191 <        final void advance() {
2190 >        /** Creates iterator for clone() and split() methods */
2191 >        InternalIterator(InternalIterator<K,V> it, boolean split) {
2192 >            this.map = it.map;
2193 >            this.tab = it.tab;
2194 >            this.baseSize = it.baseSize;
2195 >            int lo = it.baseIndex;
2196 >            int hi = this.baseLimit = it.baseLimit;
2197 >            this.index = this.baseIndex =
2198 >                (split) ? (it.baseLimit = (lo + hi + 1) >>> 1) : lo;
2199 >        }
2200 >
2201 >        /**
2202 >         * Advances next; returns nextVal or null if terminated
2203 >         * See above for explanation.
2204 >         */
2205 >        final Object advance() {
2206              Node e = last = next;
2207 +            Object ev = null;
2208              outer: do {
2209                  if (e != null)                  // advance past used/skipped node
2210                      e = e.next;
2211                  while (e == null) {             // get to next non-null bin
2212 <                    Node[] t; int b, i, n;      // checks must use locals
2212 >                    Node[] t; int b, i, n; Object ek; // checks must use locals
2213                      if ((b = baseIndex) >= baseLimit || (i = index) < 0 ||
2214                          (t = tab) == null || i >= (n = t.length))
2215                          break outer;
2216 <                    else if ((e = tabAt(t, i)) != null && e.hash == MOVED)
2217 <                        tab = (Node[])e.key;    // restarts due to null val
2218 <                    else                        // visit upper slots if present
2219 <                        index = (i += baseSize) < n ? i : (baseIndex = b + 1);
2216 >                    else if ((e = tabAt(t, i)) != null && e.hash == MOVED) {
2217 >                        if ((ek = e.key) instanceof TreeBin)
2218 >                            e = ((TreeBin)ek).first;
2219 >                        else {
2220 >                            tab = (Node[])ek;
2221 >                            continue;           // restarts due to null val
2222 >                        }
2223 >                    }                           // visit upper slots if present
2224 >                    index = (i += baseSize) < n ? i : (baseIndex = b + 1);
2225                  }
2226                  nextKey = e.key;
2227 <            } while ((nextVal = e.val) == null);// skip deleted or special nodes
2227 >            } while ((ev = e.val) == null);    // skip deleted or special nodes
2228              next = e;
2229 +            return nextVal = ev;
2230          }
2231 +
2232 +        public final void remove() {
2233 +            if (nextVal == null)
2234 +                advance();
2235 +            Node e = last;
2236 +            if (e == null)
2237 +                throw new IllegalStateException();
2238 +            last = null;
2239 +            map.remove(e.key);
2240 +        }
2241 +
2242 +        public final boolean hasNext() {
2243 +            return nextVal != null || advance() != null;
2244 +        }
2245 +
2246 +        public final boolean hasMoreElements() { return hasNext(); }
2247      }
2248  
2249      /* ---------------- Public operations -------------- */
# Line 1535 | Line 2405 | public class ConcurrentHashMapV8<K, V>
2405          if (value == null)
2406              throw new NullPointerException();
2407          Object v;
2408 <        InternalIterator it = new InternalIterator(table);
2409 <        while (it.next != null) {
2410 <            if ((v = it.nextVal) == value || value.equals(v))
2408 >        InternalIterator<K,V> it = new InternalIterator<K,V>(this);
2409 >        while ((v = it.advance()) != null) {
2410 >            if (v == value || value.equals(v))
2411                  return true;
1542            it.advance();
2412          }
2413          return false;
2414      }
# Line 1610 | Line 2479 | public class ConcurrentHashMapV8<K, V>
2479  
2480      /**
2481       * If the specified key is not already associated with a value,
2482 <     * computes its value using the given mappingFunction and
2483 <     * enters it into the map.  This is equivalent to
2482 >     * computes its value using the given mappingFunction and enters
2483 >     * it into the map unless null.  This is equivalent to
2484       * <pre> {@code
2485       * if (map.containsKey(key))
2486       *   return map.get(key);
2487       * value = mappingFunction.map(key);
2488 <     * map.put(key, value);
2488 >     * if (value != null)
2489 >     *   map.put(key, value);
2490       * return value;}</pre>
2491       *
2492       * except that the action is performed atomically.  If the
2493 <     * function returns {@code null} (in which case a {@code
2494 <     * NullPointerException} is thrown), or the function itself throws
2495 <     * an (unchecked) exception, the exception is rethrown to its
2496 <     * caller, and no mapping is recorded.  Some attempted update
2497 <     * operations on this map by other threads may be blocked while
2498 <     * computation is in progress, so the computation should be short
2499 <     * and simple, and must not attempt to update any other mappings
2500 <     * of this Map. The most appropriate usage is to construct a new
2501 <     * object serving as an initial mapped value, or memoized result,
1632 <     * as in:
2493 >     * function returns {@code null} no mapping is recorded. If the
2494 >     * function itself throws an (unchecked) exception, the exception
2495 >     * is rethrown to its caller, and no mapping is recorded.  Some
2496 >     * attempted update operations on this map by other threads may be
2497 >     * blocked while computation is in progress, so the computation
2498 >     * should be short and simple, and must not attempt to update any
2499 >     * other mappings of this Map. The most appropriate usage is to
2500 >     * construct a new object serving as an initial mapped value, or
2501 >     * memoized result, as in:
2502       *
2503       *  <pre> {@code
2504       * map.computeIfAbsent(key, new MappingFunction<K, V>() {
# Line 1638 | Line 2507 | public class ConcurrentHashMapV8<K, V>
2507       * @param key key with which the specified value is to be associated
2508       * @param mappingFunction the function to compute a value
2509       * @return the current (existing or computed) value associated with
2510 <     *         the specified key.
2511 <     * @throws NullPointerException if the specified key, mappingFunction,
2512 <     *         or computed value is null
2510 >     *         the specified key, or null if the computed value is null.
2511 >     * @throws NullPointerException if the specified key or mappingFunction
2512 >     *         is null
2513       * @throws IllegalStateException if the computation detectably
2514       *         attempts a recursive update to this map that would
2515       *         otherwise never complete
# Line 1655 | Line 2524 | public class ConcurrentHashMapV8<K, V>
2524      }
2525  
2526      /**
2527 <     * Computes and enters a new mapping value given a key and
2527 >     * Computes a new mapping value given a key and
2528       * its current mapped value (or {@code null} if there is no current
2529       * mapping). This is equivalent to
2530       *  <pre> {@code
2531 <     *  map.put(key, remappingFunction.remap(key, map.get(key));
2531 >     *   value = remappingFunction.remap(key, map.get(key));
2532 >     *   if (value != null)
2533 >     *     map.put(key, value);
2534 >     *   else
2535 >     *     map.remove(key);
2536       * }</pre>
2537       *
2538       * except that the action is performed atomically.  If the
2539 <     * function returns {@code null} (in which case a {@code
2540 <     * NullPointerException} is thrown), or the function itself throws
2541 <     * an (unchecked) exception, the exception is rethrown to its
2542 <     * caller, and current mapping is left unchanged.  Some attempted
2543 <     * update operations on this map by other threads may be blocked
2544 <     * while computation is in progress, so the computation should be
2545 <     * short and simple, and must not attempt to update any other
2546 <     * mappings of this Map. For example, to either create or
1674 <     * append new messages to a value mapping:
2539 >     * function returns {@code null}, the mapping is removed.  If the
2540 >     * function itself throws an (unchecked) exception, the exception
2541 >     * is rethrown to its caller, and the current mapping is left
2542 >     * unchanged.  Some attempted update operations on this map by
2543 >     * other threads may be blocked while computation is in progress,
2544 >     * so the computation should be short and simple, and must not
2545 >     * attempt to update any other mappings of this Map. For example,
2546 >     * to either create or append new messages to a value mapping:
2547       *
2548       * <pre> {@code
2549       * Map<Key, String> map = ...;
# Line 1683 | Line 2555 | public class ConcurrentHashMapV8<K, V>
2555       * @param key key with which the specified value is to be associated
2556       * @param remappingFunction the function to compute a value
2557       * @return the new value associated with
2558 <     *         the specified key.
2558 >     *         the specified key, or null if none.
2559       * @throws NullPointerException if the specified key or remappingFunction
2560 <     *         or computed value is null
2560 >     *         is null
2561       * @throws IllegalStateException if the computation detectably
2562       *         attempts a recursive update to this map that would
2563       *         otherwise never complete
# Line 1844 | Line 2716 | public class ConcurrentHashMapV8<K, V>
2716      }
2717  
2718      /**
2719 +     * Returns a partionable iterator of the keys in this map.
2720 +     *
2721 +     * @return a partionable iterator of the keys in this map
2722 +     */
2723 +    public Spliterator<K> keySpliterator() {
2724 +        return new KeyIterator<K,V>(this);
2725 +    }
2726 +
2727 +    /**
2728 +     * Returns a partionable iterator of the values in this map.
2729 +     *
2730 +     * @return a partionable iterator of the values in this map
2731 +     */
2732 +    public Spliterator<V> valueSpliterator() {
2733 +        return new ValueIterator<K,V>(this);
2734 +    }
2735 +
2736 +    /**
2737 +     * Returns a partionable iterator of the entries in this map.
2738 +     *
2739 +     * @return a partionable iterator of the entries in this map
2740 +     */
2741 +    public Spliterator<Map.Entry<K,V>> entrySpliterator() {
2742 +        return new EntryIterator<K,V>(this);
2743 +    }
2744 +
2745 +    /**
2746       * Returns the hash code value for this {@link Map}, i.e.,
2747       * the sum of, for each key-value pair in the map,
2748       * {@code key.hashCode() ^ value.hashCode()}.
# Line 1852 | Line 2751 | public class ConcurrentHashMapV8<K, V>
2751       */
2752      public int hashCode() {
2753          int h = 0;
2754 <        InternalIterator it = new InternalIterator(table);
2755 <        while (it.next != null) {
2756 <            h += it.nextKey.hashCode() ^ it.nextVal.hashCode();
2757 <            it.advance();
2754 >        InternalIterator<K,V> it = new InternalIterator<K,V>(this);
2755 >        Object v;
2756 >        while ((v = it.advance()) != null) {
2757 >            h += it.nextKey.hashCode() ^ v.hashCode();
2758          }
2759          return h;
2760      }
# Line 1872 | Line 2771 | public class ConcurrentHashMapV8<K, V>
2771       * @return a string representation of this map
2772       */
2773      public String toString() {
2774 <        InternalIterator it = new InternalIterator(table);
2774 >        InternalIterator<K,V> it = new InternalIterator<K,V>(this);
2775          StringBuilder sb = new StringBuilder();
2776          sb.append('{');
2777 <        if (it.next != null) {
2777 >        Object v;
2778 >        if ((v = it.advance()) != null) {
2779              for (;;) {
2780 <                Object k = it.nextKey, v = it.nextVal;
2780 >                Object k = it.nextKey;
2781                  sb.append(k == this ? "(this Map)" : k);
2782                  sb.append('=');
2783                  sb.append(v == this ? "(this Map)" : v);
2784 <                it.advance();
1885 <                if (it.next == null)
2784 >                if ((v = it.advance()) == null)
2785                      break;
2786                  sb.append(',').append(' ');
2787              }
# Line 1905 | Line 2804 | public class ConcurrentHashMapV8<K, V>
2804              if (!(o instanceof Map))
2805                  return false;
2806              Map<?,?> m = (Map<?,?>) o;
2807 <            InternalIterator it = new InternalIterator(table);
2808 <            while (it.next != null) {
2809 <                Object val = it.nextVal;
2807 >            InternalIterator<K,V> it = new InternalIterator<K,V>(this);
2808 >            Object val;
2809 >            while ((val = it.advance()) != null) {
2810                  Object v = m.get(it.nextKey);
2811                  if (v == null || (v != val && !v.equals(val)))
2812                      return false;
1914                it.advance();
2813              }
2814              for (Map.Entry<?,?> e : m.entrySet()) {
2815                  Object mk, mv, v;
# Line 1927 | Line 2825 | public class ConcurrentHashMapV8<K, V>
2825  
2826      /* ----------------Iterators -------------- */
2827  
2828 <    /**
2829 <     * Base class for key, value, and entry iterators.  Adds a map
2830 <     * reference to InternalIterator to support Iterator.remove.
2831 <     */
2832 <    static abstract class ViewIterator<K,V> extends InternalIterator {
1935 <        final ConcurrentHashMapV8<K, V> map;
1936 <        ViewIterator(ConcurrentHashMapV8<K, V> map) {
1937 <            super(map.table);
1938 <            this.map = map;
2828 >    static final class KeyIterator<K,V> extends InternalIterator<K,V>
2829 >        implements Spliterator<K>, Enumeration<K> {
2830 >        KeyIterator(ConcurrentHashMapV8<K, V> map) { super(map); }
2831 >        KeyIterator(InternalIterator<K,V> it, boolean split) {
2832 >            super(it, split);
2833          }
2834 <
2835 <        public final void remove() {
1942 <            if (last == null)
2834 >        public KeyIterator<K,V> split() {
2835 >            if (last != null || (next != null && nextVal == null))
2836                  throw new IllegalStateException();
2837 <            map.remove(last.key);
2838 <            last = null;
2837 >            return new KeyIterator<K,V>(this, true);
2838 >        }
2839 >        public KeyIterator<K,V> clone() {
2840 >            if (last != null || (next != null && nextVal == null))
2841 >                throw new IllegalStateException();
2842 >            return new KeyIterator<K,V>(this, false);
2843          }
1947
1948        public final boolean hasNext()         { return next != null; }
1949        public final boolean hasMoreElements() { return next != null; }
1950    }
1951
1952    static final class KeyIterator<K,V> extends ViewIterator<K,V>
1953        implements Iterator<K>, Enumeration<K> {
1954        KeyIterator(ConcurrentHashMapV8<K, V> map) { super(map); }
2844  
2845          @SuppressWarnings("unchecked")
2846          public final K next() {
2847 <            if (next == null)
2847 >            if (nextVal == null && advance() == null)
2848                  throw new NoSuchElementException();
2849              Object k = nextKey;
2850 <            advance();
2851 <            return (K)k;
2850 >            nextVal = null;
2851 >            return (K) k;
2852          }
2853  
2854          public final K nextElement() { return next(); }
2855      }
2856  
2857 <    static final class ValueIterator<K,V> extends ViewIterator<K,V>
2858 <        implements Iterator<V>, Enumeration<V> {
2857 >    static final class ValueIterator<K,V> extends InternalIterator<K,V>
2858 >        implements Spliterator<V>, Enumeration<V> {
2859          ValueIterator(ConcurrentHashMapV8<K, V> map) { super(map); }
2860 +        ValueIterator(InternalIterator<K,V> it, boolean split) {
2861 +            super(it, split);
2862 +        }
2863 +        public ValueIterator<K,V> split() {
2864 +            if (last != null || (next != null && nextVal == null))
2865 +                throw new IllegalStateException();
2866 +            return new ValueIterator<K,V>(this, true);
2867 +        }
2868 +
2869 +        public ValueIterator<K,V> clone() {
2870 +            if (last != null || (next != null && nextVal == null))
2871 +                throw new IllegalStateException();
2872 +            return new ValueIterator<K,V>(this, false);
2873 +        }
2874  
2875          @SuppressWarnings("unchecked")
2876          public final V next() {
2877 <            if (next == null)
2877 >            Object v;
2878 >            if ((v = nextVal) == null && (v = advance()) == null)
2879                  throw new NoSuchElementException();
2880 <            Object v = nextVal;
2881 <            advance();
1978 <            return (V)v;
2880 >            nextVal = null;
2881 >            return (V) v;
2882          }
2883  
2884          public final V nextElement() { return next(); }
2885      }
2886  
2887 <    static final class EntryIterator<K,V> extends ViewIterator<K,V>
2888 <        implements Iterator<Map.Entry<K,V>> {
2887 >    static final class EntryIterator<K,V> extends InternalIterator<K,V>
2888 >        implements Spliterator<Map.Entry<K,V>> {
2889          EntryIterator(ConcurrentHashMapV8<K, V> map) { super(map); }
2890 <
2891 <        @SuppressWarnings("unchecked")
2892 <        public final Map.Entry<K,V> next() {
2893 <            if (next == null)
2894 <                throw new NoSuchElementException();
2895 <            Object k = nextKey;
2896 <            Object v = nextVal;
2897 <            advance();
2898 <            return new WriteThroughEntry<K,V>((K)k, (V)v, map);
2890 >        EntryIterator(InternalIterator<K,V> it, boolean split) {
2891 >            super(it, split);
2892 >        }
2893 >        public EntryIterator<K,V> split() {
2894 >            if (last != null || (next != null && nextVal == null))
2895 >                throw new IllegalStateException();
2896 >            return new EntryIterator<K,V>(this, true);
2897 >        }
2898 >        public EntryIterator<K,V> clone() {
2899 >            if (last != null || (next != null && nextVal == null))
2900 >                throw new IllegalStateException();
2901 >            return new EntryIterator<K,V>(this, false);
2902          }
1997    }
1998
1999    static final class SnapshotEntryIterator<K,V> extends ViewIterator<K,V>
2000        implements Iterator<Map.Entry<K,V>> {
2001        SnapshotEntryIterator(ConcurrentHashMapV8<K, V> map) { super(map); }
2903  
2904          @SuppressWarnings("unchecked")
2905          public final Map.Entry<K,V> next() {
2906 <            if (next == null)
2906 >            Object v;
2907 >            if ((v = nextVal) == null && (v = advance()) == null)
2908                  throw new NoSuchElementException();
2909              Object k = nextKey;
2910 <            Object v = nextVal;
2911 <            advance();
2010 <            return new SnapshotEntry<K,V>((K)k, (V)v);
2910 >            nextVal = null;
2911 >            return new MapEntry<K,V>((K)k, (V)v, map);
2912          }
2913      }
2914  
2915      /**
2916 <     * Base of writeThrough and Snapshot entry classes
2916 >     * Exported Entry for iterators
2917       */
2918 <    static abstract class MapEntry<K,V> implements Map.Entry<K, V> {
2918 >    static final class MapEntry<K,V> implements Map.Entry<K, V> {
2919          final K key; // non-null
2920          V val;       // non-null
2921 <        MapEntry(K key, V val)        { this.key = key; this.val = val; }
2921 >        final ConcurrentHashMapV8<K, V> map;
2922 >        MapEntry(K key, V val, ConcurrentHashMapV8<K, V> map) {
2923 >            this.key = key;
2924 >            this.val = val;
2925 >            this.map = map;
2926 >        }
2927          public final K getKey()       { return key; }
2928          public final V getValue()     { return val; }
2929          public final int hashCode()   { return key.hashCode() ^ val.hashCode(); }
# Line 2032 | Line 2938 | public class ConcurrentHashMapV8<K, V>
2938                      (v == val || v.equals(val)));
2939          }
2940  
2035        public abstract V setValue(V value);
2036    }
2037
2038    /**
2039     * Entry used by EntryIterator.next(), that relays setValue
2040     * changes to the underlying map.
2041     */
2042    static final class WriteThroughEntry<K,V> extends MapEntry<K,V>
2043        implements Map.Entry<K, V> {
2044        final ConcurrentHashMapV8<K, V> map;
2045        WriteThroughEntry(K key, V val, ConcurrentHashMapV8<K, V> map) {
2046            super(key, val);
2047            this.map = map;
2048        }
2049
2941          /**
2942           * Sets our entry's value and writes through to the map. The
2943 <         * value to return is somewhat arbitrary here. Since a
2944 <         * WriteThroughEntry does not necessarily track asynchronous
2945 <         * changes, the most recent "previous" value could be
2946 <         * different from what we return (or could even have been
2947 <         * removed in which case the put will re-establish). We do not
2057 <         * and cannot guarantee more.
2943 >         * value to return is somewhat arbitrary here. Since a we do
2944 >         * not necessarily track asynchronous changes, the most recent
2945 >         * "previous" value could be different from what we return (or
2946 >         * could even have been removed in which case the put will
2947 >         * re-establish). We do not and cannot guarantee more.
2948           */
2949          public final V setValue(V value) {
2950              if (value == null) throw new NullPointerException();
# Line 2065 | Line 2955 | public class ConcurrentHashMapV8<K, V>
2955          }
2956      }
2957  
2068    /**
2069     * Internal version of entry, that doesn't write though changes
2070     */
2071    static final class SnapshotEntry<K,V> extends MapEntry<K,V>
2072        implements Map.Entry<K, V> {
2073        SnapshotEntry(K key, V val) { super(key, val); }
2074        public final V setValue(V value) { // only locally update
2075            if (value == null) throw new NullPointerException();
2076            V v = val;
2077            val = value;
2078            return v;
2079        }
2080    }
2081
2958      /* ----------------Views -------------- */
2959  
2960      /**
2961 <     * Base class for views. This is done mainly to allow adding
2086 <     * customized parallel traversals (not yet implemented.)
2961 >     * Base class for views.
2962       */
2963      static abstract class MapView<K, V> {
2964          final ConcurrentHashMapV8<K, V> map;
# Line 2093 | Line 2968 | public class ConcurrentHashMapV8<K, V>
2968          public final void clear()               { map.clear(); }
2969  
2970          // implementations below rely on concrete classes supplying these
2971 <        abstract Iterator<?> iter();
2971 >        abstract public Iterator<?> iterator();
2972          abstract public boolean contains(Object o);
2973          abstract public boolean remove(Object o);
2974  
# Line 2106 | Line 2981 | public class ConcurrentHashMapV8<K, V>
2981              int n = (int)sz;
2982              Object[] r = new Object[n];
2983              int i = 0;
2984 <            Iterator<?> it = iter();
2984 >            Iterator<?> it = iterator();
2985              while (it.hasNext()) {
2986                  if (i == n) {
2987                      if (n >= MAX_ARRAY_SIZE)
# Line 2133 | Line 3008 | public class ConcurrentHashMapV8<K, V>
3008                  .newInstance(a.getClass().getComponentType(), m);
3009              int n = r.length;
3010              int i = 0;
3011 <            Iterator<?> it = iter();
3011 >            Iterator<?> it = iterator();
3012              while (it.hasNext()) {
3013                  if (i == n) {
3014                      if (n >= MAX_ARRAY_SIZE)
# Line 2155 | Line 3030 | public class ConcurrentHashMapV8<K, V>
3030  
3031          public final int hashCode() {
3032              int h = 0;
3033 <            for (Iterator<?> it = iter(); it.hasNext();)
3033 >            for (Iterator<?> it = iterator(); it.hasNext();)
3034                  h += it.next().hashCode();
3035              return h;
3036          }
# Line 2163 | Line 3038 | public class ConcurrentHashMapV8<K, V>
3038          public final String toString() {
3039              StringBuilder sb = new StringBuilder();
3040              sb.append('[');
3041 <            Iterator<?> it = iter();
3041 >            Iterator<?> it = iterator();
3042              if (it.hasNext()) {
3043                  for (;;) {
3044                      Object e = it.next();
# Line 2189 | Line 3064 | public class ConcurrentHashMapV8<K, V>
3064  
3065          public final boolean removeAll(Collection<?> c) {
3066              boolean modified = false;
3067 <            for (Iterator<?> it = iter(); it.hasNext();) {
3067 >            for (Iterator<?> it = iterator(); it.hasNext();) {
3068                  if (c.contains(it.next())) {
3069                      it.remove();
3070                      modified = true;
# Line 2200 | Line 3075 | public class ConcurrentHashMapV8<K, V>
3075  
3076          public final boolean retainAll(Collection<?> c) {
3077              boolean modified = false;
3078 <            for (Iterator<?> it = iter(); it.hasNext();) {
3078 >            for (Iterator<?> it = iterator(); it.hasNext();) {
3079                  if (!c.contains(it.next())) {
3080                      it.remove();
3081                      modified = true;
# Line 2215 | Line 3090 | public class ConcurrentHashMapV8<K, V>
3090          KeySet(ConcurrentHashMapV8<K, V> map)   { super(map); }
3091          public final boolean contains(Object o) { return map.containsKey(o); }
3092          public final boolean remove(Object o)   { return map.remove(o) != null; }
2218
3093          public final Iterator<K> iterator() {
3094              return new KeyIterator<K,V>(map);
3095          }
2222        final Iterator<?> iter() {
2223            return new KeyIterator<K,V>(map);
2224        }
3096          public final boolean add(K e) {
3097              throw new UnsupportedOperationException();
3098          }
# Line 2240 | Line 3111 | public class ConcurrentHashMapV8<K, V>
3111          implements Collection<V> {
3112          Values(ConcurrentHashMapV8<K, V> map)   { super(map); }
3113          public final boolean contains(Object o) { return map.containsValue(o); }
2243
3114          public final boolean remove(Object o) {
3115              if (o != null) {
3116                  Iterator<V> it = new ValueIterator<K,V>(map);
# Line 2256 | Line 3126 | public class ConcurrentHashMapV8<K, V>
3126          public final Iterator<V> iterator() {
3127              return new ValueIterator<K,V>(map);
3128          }
2259        final Iterator<?> iter() {
2260            return new ValueIterator<K,V>(map);
2261        }
3129          public final boolean add(V e) {
3130              throw new UnsupportedOperationException();
3131          }
# Line 2270 | Line 3137 | public class ConcurrentHashMapV8<K, V>
3137      static final class EntrySet<K,V> extends MapView<K,V>
3138          implements Set<Map.Entry<K,V>> {
3139          EntrySet(ConcurrentHashMapV8<K, V> map) { super(map); }
2273
3140          public final boolean contains(Object o) {
3141              Object k, v, r; Map.Entry<?,?> e;
3142              return ((o instanceof Map.Entry) &&
# Line 2279 | Line 3145 | public class ConcurrentHashMapV8<K, V>
3145                      (v = e.getValue()) != null &&
3146                      (v == r || v.equals(r)));
3147          }
2282
3148          public final boolean remove(Object o) {
3149              Object k, v; Map.Entry<?,?> e;
3150              return ((o instanceof Map.Entry) &&
# Line 2287 | Line 3152 | public class ConcurrentHashMapV8<K, V>
3152                      (v = e.getValue()) != null &&
3153                      map.remove(k, v));
3154          }
2290
3155          public final Iterator<Map.Entry<K,V>> iterator() {
3156              return new EntryIterator<K,V>(map);
3157          }
2294        final Iterator<?> iter() {
2295            return new SnapshotEntryIterator<K,V>(map);
2296        }
3158          public final boolean add(Entry<K,V> e) {
3159              throw new UnsupportedOperationException();
3160          }
# Line 2339 | Line 3200 | public class ConcurrentHashMapV8<K, V>
3200                  segments[i] = new Segment<K,V>(LOAD_FACTOR);
3201          }
3202          s.defaultWriteObject();
3203 <        InternalIterator it = new InternalIterator(table);
3204 <        while (it.next != null) {
3203 >        InternalIterator<K,V> it = new InternalIterator<K,V>(this);
3204 >        Object v;
3205 >        while ((v = it.advance()) != null) {
3206              s.writeObject(it.nextKey);
3207 <            s.writeObject(it.nextVal);
2346 <            it.advance();
3207 >            s.writeObject(v);
3208          }
3209          s.writeObject(null);
3210          s.writeObject(null);
# Line 2369 | Line 3230 | public class ConcurrentHashMapV8<K, V>
3230              K k = (K) s.readObject();
3231              V v = (V) s.readObject();
3232              if (k != null && v != null) {
3233 <                p = new Node(spread(k.hashCode()), k, v, p);
3233 >                int h = spread(k.hashCode());
3234 >                p = new Node(h, k, v, p);
3235                  ++size;
3236              }
3237              else
# Line 2385 | Line 3247 | public class ConcurrentHashMapV8<K, V>
3247                  n = tableSizeFor(sz + (sz >>> 1) + 1);
3248              }
3249              int sc = sizeCtl;
3250 +            boolean collide = false;
3251              if (n > sc &&
3252                  UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
3253                  try {
# Line 2395 | Line 3258 | public class ConcurrentHashMapV8<K, V>
3258                          while (p != null) {
3259                              int j = p.hash & mask;
3260                              Node next = p.next;
3261 <                            p.next = tabAt(tab, j);
3261 >                            Node q = p.next = tabAt(tab, j);
3262                              setTabAt(tab, j, p);
3263 +                            if (!collide && q != null && q.hash == p.hash)
3264 +                                collide = true;
3265                              p = next;
3266                          }
3267                          table = tab;
# Line 2406 | Line 3271 | public class ConcurrentHashMapV8<K, V>
3271                  } finally {
3272                      sizeCtl = sc;
3273                  }
3274 +                if (collide) { // rescan and convert to TreeBins
3275 +                    Node[] tab = table;
3276 +                    for (int i = 0; i < tab.length; ++i) {
3277 +                        int c = 0;
3278 +                        for (Node e = tabAt(tab, i); e != null; e = e.next) {
3279 +                            if (++c > TREE_THRESHOLD &&
3280 +                                (e.key instanceof Comparable)) {
3281 +                                replaceWithTreeBin(tab, i, e.key);
3282 +                                break;
3283 +                            }
3284 +                        }
3285 +                    }
3286 +                }
3287              }
3288              if (!init) { // Can only happen if unsafely published.
3289                  while (p != null) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines