@@ -1988,7 +1988,7 @@ void free(Page page, Block block, MiByteBuf buf) { |
1988 | 1988 | // If `segment.ownerThread == Thread.currentThread()`, means the current thread owns the `ownerHeap`, |
1989 | 1989 | // so the `ownerHeap` must not have been abandoned, so `ownerHeap` must not be null. |
1990 | 1990 | if (segment.ownerThread == Thread.currentThread()) { |
1991 | | - StampedLock lock = ownerHeap.sharedLock; |
| 1991 | + final StampedLock lock = ownerHeap.sharedLock; |
1992 | 1992 | if (lock == null) { |
1993 | 1993 | // Event loop local free. |
1994 | 1994 | freeLocal(page, block, buf, ownerHeap); |
@@ -2003,12 +2003,24 @@ void free(Page page, Block block, MiByteBuf buf) { |
2003 | 2003 | } |
2004 | 2004 | } |
2005 | 2005 | } else { |
2006 | | - // Use the generic multi-threaded-free path. |
2007 | | - freeBlockMt(page, segment, block); |
2008 | | - // At a very low chance, the `ownerHeap` may already have been abandoned, |
2009 | | - // but it's OK, as in that case, the `ownerHeap.miBufCrossThreadsQueue` will be eventually GC'ed anyway. |
2010 | | - if (buf != null && ownerHeap != null) { |
2011 | | - ownerHeap.miBufCrossThreadsQueue.offer(buf); |
| 2006 | + StampedLock sharedLock; |
| 2007 | + long lockStamp; |
| 2008 | + if (!page.isHuge && ownerHeap != null && (sharedLock = ownerHeap.sharedLock) != null |
| 2009 | + && (lockStamp = sharedLock.tryWriteLock()) != 0) { |
| 2010 | + try { |
| 2011 | + // Successfully acquired the lock, use local free. |
| 2012 | + freeLocal(page, block, buf, ownerHeap); |
| 2013 | + } finally { |
| 2014 | + sharedLock.unlockWrite(lockStamp); |
| 2015 | + } |
| 2016 | + } else { |
| 2017 | + // Use the generic multi-threaded-free path. |
| 2018 | + freeBlockMt(page, segment, block); |
| 2019 | + // At a very low chance, the `ownerHeap` may already have been abandoned, |
| 2020 | + // but it's OK, as in that case, the `ownerHeap.miBufCrossThreadsQueue` will be eventually GC'ed anyway. |
| 2021 | + if (buf != null && ownerHeap != null) { |
| 2022 | + ownerHeap.miBufCrossThreadsQueue.offer(buf); |
| 2023 | + } |
2012 | 2024 | } |
2013 | 2025 | } |
2014 | 2026 | } |
@@ -2118,16 +2130,17 @@ private MiByteBuf allocate(int size, int maxCapacity, MiByteBuf byteBuf, boolean |
2118 | 2130 | long threadId = Thread.currentThread().getId(); |
2119 | 2131 | int currentHeapsScanLength; |
2120 | 2132 | int expansions = 0; |
| 2133 | + boolean ableExpansion; |
2121 | 2134 | do { |
2122 | 2135 | currentHeapsScanLength = this.heapsScanLength.get(); |
2123 | 2136 | int mask = currentHeapsScanLength - 1; |
2124 | 2137 | int index = (int) (threadId & mask); |
2125 | 2138 | SharedHeapWrap sharedHeapWrap; |
2126 | 2139 | StampedLock lock; |
2127 | 2140 | long lockStamp; |
2128 | | - int attempts = currentHeapsScanLength >= MAX_SHARED_HEAP_WRAPS_LENGTH ? |
2129 | | - MAX_SHARED_HEAP_LOCK_SPIN_COUNT : |
2130 | | - Math.min(currentHeapsScanLength << 1, MAX_SHARED_HEAP_LOCK_SPIN_COUNT); |
| 2141 | + ableExpansion = currentHeapsScanLength < MAX_SHARED_HEAP_WRAPS_LENGTH; |
| 2142 | + int attempts = ableExpansion ? Math.min(currentHeapsScanLength << 1, MAX_SHARED_HEAP_LOCK_SPIN_COUNT) : |
| 2143 | + MAX_SHARED_HEAP_LOCK_SPIN_COUNT; |
2131 | 2144 | for (int i = 0; i < attempts; i++) { |
2132 | 2145 | sharedHeapWrap = this.sharedHeapWraps[index + i & mask]; |
2133 | 2146 | lock = sharedHeapWrap.lock; |
@@ -2145,7 +2158,7 @@ private MiByteBuf allocate(int size, int maxCapacity, MiByteBuf byteBuf, boolean |
2145 | 2158 | } |
2146 | 2159 | } |
2147 | 2160 | expansions++; |
2148 | | - } while (expansions <= 3 && tryExpandHeapsScanLength(currentHeapsScanLength)); |
| 2161 | + } while (ableExpansion && expansions <= 3 && tryExpandHeapsScanLength(currentHeapsScanLength)); |
2149 | 2162 | } |
2150 | 2163 | assert this.sharedHeapWraps[0] != null && this.sharedHeapWraps[0].heap != null; |
2151 | 2164 | LocalHeap heap = this.sharedHeapWraps[0].heap; |
|