Source:
public synchronized void forEach(EntryConsumer consumer) { int entriesToSort = (int) index.size(); int arrayLen = entriesToSort * 4; if (sortedEntries == null || sortedEntries.length < arrayLen) { sortedEntries = new long[(int) (arrayLen * 2)]; } long startTime = System.nanoTime(); sortedEntriesIdx = 0; index.forEach((ledgerId, entryId, offset, length) -> { if (deletedLedgers.contains(ledgerId)) { return; } sortedEntries[sortedEntriesIdx] = ledgerId; sortedEntries[sortedEntriesIdx + 1] = entryId; sortedEntries[sortedEntriesIdx + 2] = offset; sortedEntries[sortedEntriesIdx + 3] = length; sortedEntriesIdx += 4; }); if (log.isDebugEnabled()) { log.debug("iteration took ms", (System.nanoTime() - startTime) / 1e6); } startTime = System.nanoTime(); groupSorter.sort(sortedEntries, 0, sortedEntriesIdx); if (log.isDebugEnabled()) { log.debug("sorting ms", (System.nanoTime() - startTime) / 1e6); } startTime = System.nanoTime(); ByteBuf[] entrySegments = new ByteBuf[segmentsCount]; for (int i = 0; i < segmentsCount; i++) { entrySegments[i] = cacheSegments[i].slice(0, cacheSegments[i].capacity()); } for (int i = 0; i < sortedEntriesIdx; i += 4) { long ledgerId = sortedEntries[i]; long entryId = sortedEntries[i + 1]; long offset = sortedEntries[i + 2]; long length = sortedEntries[i + 3]; int localOffset = (int) (offset & SegmentOffsetMask); int segmentIdx = (int) (offset / MaxSegmentSize); ByteBuf entry = entrySegments[segmentIdx]; entry.setIndex(localOffset, localOffset + (int) length); consumer.accept(ledgerId, entryId, entry); } if (log.isDebugEnabled()) { log.debug("entry log adding ms", (System.nanoTime() - startTime) / 1e6); } }

Target:
public void forEach(EntryConsumer consumer) { sortedEntriesLock.lock(); try { int entriesToSort = (int) index.size(); int arrayLen = entriesToSort * 4; if (sortedEntries == null || sortedEntries.length < arrayLen) { sortedEntries = new long[(int) (arrayLen * 2)]; } long startTime = System.nanoTime(); sortedEntriesIdx = 0; index.forEach((ledgerId, entryId, offset, length) -> { if (deletedLedgers.contains(ledgerId)) { return; } sortedEntries[sortedEntriesIdx] = ledgerId; sortedEntries[sortedEntriesIdx + 1] = entryId; sortedEntries[sortedEntriesIdx + 2] = offset; sortedEntries[sortedEntriesIdx + 3] = length; sortedEntriesIdx += 4; }); if (log.isDebugEnabled()) { log.debug("iteration took ms", (System.nanoTime() - startTime) / 1e6); } startTime = System.nanoTime(); groupSorter.sort(sortedEntries, 0, sortedEntriesIdx); if (log.isDebugEnabled()) { log.debug("sorting ms", (System.nanoTime() - startTime) / 1e6); } startTime = System.nanoTime(); ByteBuf[] entrySegments = new ByteBuf[segmentsCount]; for (int i = 0; i < segmentsCount; i++) { entrySegments[i] = cacheSegments[i].slice(0, cacheSegments[i].capacity()); } for (int i = 0; i < sortedEntriesIdx; i += 4) { long ledgerId = sortedEntries[i]; long entryId = sortedEntries[i + 1]; long offset = sortedEntries[i + 2]; long length = sortedEntries[i + 3]; int localOffset = (int) (offset & SegmentOffsetMask); int segmentIdx = (int) (offset / MaxSegmentSize); ByteBuf entry = entrySegments[segmentIdx]; entry.setIndex(localOffset, localOffset + (int) length); consumer.accept(ledgerId, entryId, entry); } if (log.isDebugEnabled()) { log.debug("entry log adding ms", (System.nanoTime() - startTime) / 1e6); } } finally { sortedEntriesLock.unlock(); } }