Use addAndGet(...) as a replacement for compareAndSet(...) when tracking the direct memory usage. (#8596)
Motivation: We can change from using compareAndSet to addAndGet, which emits a different CPU instruction on x86 (CMPXCHG to XADD) when count direct memory usage. This instruction is cheaper in general and so produce less overhead on the "happy path". If we detect too much memory usage we just rollback the change before throwing the Error. Modifications: Replace compareAndSet(...) with addAndGet(...) Result: Less overhead when tracking direct memory.
This commit is contained in:
parent
48c7ca8edd
commit
2e0922d773
@ -649,16 +649,12 @@ public final class PlatformDependent {
|
|||||||
|
|
||||||
private static void incrementMemoryCounter(int capacity) {
|
private static void incrementMemoryCounter(int capacity) {
|
||||||
if (DIRECT_MEMORY_COUNTER != null) {
|
if (DIRECT_MEMORY_COUNTER != null) {
|
||||||
for (;;) {
|
long newUsedMemory = DIRECT_MEMORY_COUNTER.addAndGet(capacity);
|
||||||
long usedMemory = DIRECT_MEMORY_COUNTER.get();
|
if (newUsedMemory > DIRECT_MEMORY_LIMIT) {
|
||||||
long newUsedMemory = usedMemory + capacity;
|
DIRECT_MEMORY_COUNTER.addAndGet(-capacity);
|
||||||
if (newUsedMemory > DIRECT_MEMORY_LIMIT) {
|
throw new OutOfDirectMemoryError("failed to allocate " + capacity
|
||||||
throw new OutOfDirectMemoryError("failed to allocate " + capacity
|
+ " byte(s) of direct memory (used: " + (newUsedMemory - capacity)
|
||||||
+ " byte(s) of direct memory (used: " + usedMemory + ", max: " + DIRECT_MEMORY_LIMIT + ')');
|
+ ", max: " + DIRECT_MEMORY_LIMIT + ')');
|
||||||
}
|
|
||||||
if (DIRECT_MEMORY_COUNTER.compareAndSet(usedMemory, newUsedMemory)) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user