GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Use try-lock on shared release path; add ableExpansion flag to shorcu… · neoionet/netty-allocator@4638c51

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • mimalloc/src/main/java/io/github/neoionet/netty/mimalloc
    • MiMallocByteBufAllocator.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎mimalloc/src/main/java/io/github/neoionet/netty/mimalloc/MiMallocByteBufAllocator.java‎

Copy file name to clipboardExpand all lines: mimalloc/src/main/java/io/github/neoionet/netty/mimalloc/MiMallocByteBufAllocator.java
+24-11Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1988,7 +1988,7 @@ void free(Page page, Block block, MiByteBuf buf) {
19881988
// If `segment.ownerThread == Thread.currentThread()`, means the current thread owns the `ownerHeap`,
19891989
// so the `ownerHeap` must not have been abandoned, so `ownerHeap` must not be null.
19901990
if (segment.ownerThread == Thread.currentThread()) {
1991-
StampedLock lock = ownerHeap.sharedLock;
1991+
final StampedLock lock = ownerHeap.sharedLock;
19921992
if (lock == null) {
19931993
// Event loop local free.
19941994
freeLocal(page, block, buf, ownerHeap);
@@ -2003,12 +2003,24 @@ void free(Page page, Block block, MiByteBuf buf) {
20032003
}
20042004
}
20052005
} 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+
}
20122024
}
20132025
}
20142026
}
@@ -2118,16 +2130,17 @@ private MiByteBuf allocate(int size, int maxCapacity, MiByteBuf byteBuf, boolean
21182130
long threadId = Thread.currentThread().getId();
21192131
int currentHeapsScanLength;
21202132
int expansions = 0;
2133+
boolean ableExpansion;
21212134
do {
21222135
currentHeapsScanLength = this.heapsScanLength.get();
21232136
int mask = currentHeapsScanLength - 1;
21242137
int index = (int) (threadId & mask);
21252138
SharedHeapWrap sharedHeapWrap;
21262139
StampedLock lock;
21272140
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;
21312144
for (int i = 0; i < attempts; i++) {
21322145
sharedHeapWrap = this.sharedHeapWraps[index + i & mask];
21332146
lock = sharedHeapWrap.lock;
@@ -2145,7 +2158,7 @@ private MiByteBuf allocate(int size, int maxCapacity, MiByteBuf byteBuf, boolean
21452158
}
21462159
}
21472160
expansions++;
2148-
} while (expansions <= 3 && tryExpandHeapsScanLength(currentHeapsScanLength));
2161+
} while (ableExpansion && expansions <= 3 && tryExpandHeapsScanLength(currentHeapsScanLength));
21492162
}
21502163
assert this.sharedHeapWraps[0] != null && this.sharedHeapWraps[0].heap != null;
21512164
LocalHeap heap = this.sharedHeapWraps[0].heap;

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.