Skip to content

Commit

Permalink
Reduce garbage on empty queue
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli committed Jan 8, 2016
1 parent 514093f commit dd3c468
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 26 deletions.
Expand Up @@ -238,11 +238,15 @@ public long lastIndex() {
if (lastIndexCount >= 0) {
try {
final VanillaMappedBytes buffer = indexCache.indexFor(cycle, lastIndexCount, false);
final long indices = VanillaIndexCache.countIndices(buffer);
buffer.release();
if(buffer != null) {
final long indices = VanillaIndexCache.countIndices(buffer);
buffer.release();

final long indexEntryNumber = (indices > 0) ? indices - 1 : 0;
return (((long) cycle) << entriesForCycleBits) + (((long) lastIndexCount) << indexBlockLongsBits) + indexEntryNumber;
final long indexEntryNumber = (indices > 0) ? indices - 1 : 0;
return (((long) cycle) << entriesForCycleBits) + (((long) lastIndexCount) << indexBlockLongsBits) + indexEntryNumber;
} else {
return -1;
}
} catch (IOException e) {
throw new AssertionError(e);
}
Expand Down Expand Up @@ -415,6 +419,10 @@ public boolean index(long nextIndex) {
}

indexBytes = indexCache.indexFor(cycle, indexCount, false);
if(indexBytes == null) {
return false;
}

indexFileChange = true;
assert indexBytes.refCount() > 1;
lastCycle = cycle;
Expand Down
Expand Up @@ -34,6 +34,16 @@ public boolean accept(File pathname) {
}
};


/**
*
* @param basePath
* @param cycleStr
* @param name
* @param forAppend
* @return null if !forAppend and file does not exist
* @throws IOException
*/
public static File mkFiles(
String basePath, String cycleStr, String name, boolean forAppend) throws IOException {

Expand All @@ -43,7 +53,7 @@ public static File mkFiles(
if (!forAppend) {
//This test needs to be done before any directories are created.
if (!file.exists()) {
throw new FileNotFoundException(file.getAbsolutePath());
return null;
}
}

Expand All @@ -55,11 +65,12 @@ public static File mkFiles(
return file;
}

public static File fileFor(
public static File indexFileFor(
String basePath, int cycle, int indexCount, VanillaDateCache dateCache) {
return new File(
new File(basePath, dateCache.formatFor(cycle)),
VanillaIndexCache.FILE_NAME_PREFIX + indexCount);
VanillaIndexCache.FILE_NAME_PREFIX + indexCount
);
}

public static List<File> findLeafDirectories(File root) {
Expand Down
Expand Up @@ -29,11 +29,12 @@ class VanillaDateCache {
private static final int SIZE = 32;

private final SimpleDateFormat format;
private final DateValue[] values = new DateValue[SIZE];
private final DateValue[] values;
private final int cycleLength;

public VanillaDateCache(final String formatStr, int cycleLength, TimeZone timeZone) {
this.cycleLength = cycleLength;
this.values = new DateValue[SIZE];

this.format = new SimpleDateFormat(formatStr);
this.format.setTimeZone(timeZone);
Expand Down
Expand Up @@ -29,9 +29,12 @@
import java.io.IOException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import static net.openhft.chronicle.VanillaChronicleUtils.indexFileFor;

public class VanillaIndexCache implements Closeable {
public static final String FILE_NAME_PREFIX = "index-";

Expand All @@ -42,6 +45,7 @@ public class VanillaIndexCache implements Closeable {
private final VanillaDateCache dateCache;
private final VanillaMappedCache<IndexKey> cache;
private final Map<IndexKey, File> cyclePathMap;
private final Map<IndexKey, File> indexFileMap;
private final FileLifecycleListener fileLifecycleListener;

VanillaIndexCache(
Expand All @@ -62,6 +66,12 @@ public class VanillaIndexCache implements Closeable {
);

this.cyclePathMap = new HashMap<>();
this.indexFileMap = new LinkedHashMap<IndexKey, File>(32,1.0f,true) {
@Override
protected boolean removeEldestEntry(Map.Entry<IndexKey, File> eldest) {
return size() >= 32;
}
};
}

public static long append(final VanillaMappedBytes bytes, final long indexValue, final boolean synchronous) {
Expand Down Expand Up @@ -128,19 +138,26 @@ public synchronized VanillaMappedBytes indexFor(int cycle, int indexCount, boole

VanillaMappedBytes vmb = this.cache.get(key);
if(vmb == null) {
File file = this.indexFileMap.get(key);
if(file == null) {
this.indexFileMap.put(
key.clone(),
file = indexFileFor(basePath, cycle, indexCount, dateCache)
);
}

long start = System.nanoTime();
String name = FILE_NAME_PREFIX + indexCount;
vmb = this.cache.put(
key.clone(),
VanillaChronicleUtils.mkFiles(
basePath,
dateCache.formatFor(cycle),
name,
forAppend),
1L << blockBits,
indexCount);

fileLifecycleListener.onEvent(EventType.NEW, new File(name), System.nanoTime() - start);
File parent = file.getParentFile();
if(forAppend && !parent.exists()) {
parent.mkdirs();
}

if(!forAppend && !file.exists()) {
return null;
}

vmb = this.cache.put(key.clone(), file, 1L << blockBits, indexCount);
fileLifecycleListener.onEvent(EventType.NEW, file, System.nanoTime() - start);
}

vmb.reserve();
Expand Down
Expand Up @@ -61,19 +61,19 @@ public void testIndexFor() throws IOException {
int cycle = (int) (System.currentTimeMillis() / 1000);
VanillaMappedBytes vanillaBuffer0 = cache.indexFor(cycle, 0, true);
vanillaBuffer0.writeLong(0, 0x12345678);
File file0 = VanillaChronicleUtils.fileFor(baseDir, cycle, 0, dateCache);
File file0 = VanillaChronicleUtils.indexFileFor(baseDir, cycle, 0, dateCache);
assertEquals(8 << 10, file0.length());
assertEquals(0x12345678L, vanillaBuffer0.readLong(0));
vanillaBuffer0.release();

VanillaMappedBytes vanillaBuffer1 = cache.indexFor(cycle, 1, true);
File file1 = VanillaChronicleUtils.fileFor(baseDir, cycle, 1, dateCache);
File file1 = VanillaChronicleUtils.indexFileFor(baseDir, cycle, 1, dateCache);
assertEquals(8 << 10, file1.length());
vanillaBuffer1.release();
assertNotEquals(file1, file0);

VanillaMappedBytes vanillaBuffer2 = cache.indexFor(cycle, 2, true);
File file2 = VanillaChronicleUtils.fileFor(baseDir, cycle, 2, dateCache);
File file2 = VanillaChronicleUtils.indexFileFor(baseDir, cycle, 2, dateCache);
assertEquals(8 << 10, file2.length());
vanillaBuffer2.release();

Expand Down Expand Up @@ -120,17 +120,17 @@ public void testLastIndexFile() throws IOException {
assertEquals(0, cache.lastIndexFile(cycle));

final VanillaMappedBytes vanillaBuffer0 = cache.indexFor(cycle, 0, true);
assertEquals("index-0", VanillaChronicleUtils.fileFor(baseDir, cycle, 0, dateCache).getName());
assertEquals("index-0", VanillaChronicleUtils.indexFileFor(baseDir, cycle, 0, dateCache).getName());
vanillaBuffer0.release();
assertEquals(0, cache.lastIndexFile(cycle));

final VanillaMappedBytes vanillaBuffer1 = cache.indexFor(cycle, 1, true);
assertEquals("index-1", VanillaChronicleUtils.fileFor(baseDir, cycle, 1, dateCache).getName());
assertEquals("index-1", VanillaChronicleUtils.indexFileFor(baseDir, cycle, 1, dateCache).getName());
vanillaBuffer1.release();
assertEquals(1, cache.lastIndexFile(cycle));

final VanillaMappedBytes vanillaBuffer3 = cache.indexFor(cycle, 3, true);
assertEquals("index-3", VanillaChronicleUtils.fileFor(baseDir, cycle, 3, dateCache).getName());
assertEquals("index-3", VanillaChronicleUtils.indexFileFor(baseDir, cycle, 3, dateCache).getName());
vanillaBuffer3.release();
assertEquals(3, cache.lastIndexFile(cycle));

Expand Down

0 comments on commit dd3c468

Please sign in to comment.