<div dir="ltr"><div><div>Matt and I have been looking into performance bottlenecks. One that we found was in the way that the Actor of SegmentCacheManagerImpl implements the ResponseQueue. Because it synchronized the call to ResponseQueue.take, it was possible for a long running command to prevent other threads from obtaining the response that they needed out of the queue. <br>

<br>Another problem was that it relied on a WeakHashMap and the GC was responsible of cleaning it up periodically.<br><br></div>We came up with the following replacement for the ResponseQueue and were curious if someone can spot a potential problem with this new brew.<br>

<br></div>Cheers!<br><br>--------------------------------------------------------------------------------<br>    private static class ResponseQueue&lt;K, V&gt; {<br>        private final ConcurrentHashMap&lt;K, SlotFuture&lt;V&gt;&gt; queue;<br>

<br>        /**<br>         * Creates a ResponseQueue with given capacity.<br>         *<br>         * @param capacity Capacity<br>         */<br>        public ResponseQueue(int capacity) {<br>            queue = new ConcurrentHashMap&lt;K, SlotFuture&lt;V&gt;&gt;(capacity);<br>

        }<br><br>        /**<br>         * Places a (request, response) pair onto the queue.<br>         *<br>         * @param k Request<br>         * @param v Response<br>         * @throws InterruptedException if interrupted while waiting<br>

         */<br>        public void put(K k, V v) throws InterruptedException {<br>            queue.putIfAbsent(k, new SlotFuture&lt;V&gt;());<br>            queue.get(k).put(v);<br>        }<br><br>        /**<br>         * Retrieves the response from the queue matching the given key,<br>

         * blocking until it is received.<br>         *<br>         * @param k Response<br>         * @return Response<br>         * @throws InterruptedException if interrupted while waiting<br>         */<br>        public V take(K k) throws InterruptedException {<br>

            queue.putIfAbsent(k, new SlotFuture&lt;V&gt;());<br>            V v = Util.safeGet(queue.get(k), &quot;&quot;);<br>            queue.remove(k);<br>            return v;<br>        }<br>    }<br>--------------------------------------------------------------------------------</div>