Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed 'backed' last cache hit hard references; it seems that that i… #1645

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 5 additions & 23 deletions src/main/java/htsjdk/samtools/cram/ref/ReferenceSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.lang.ref.SoftReference;
import java.net.URL;
import java.nio.file.Path;
import java.util.ArrayList;
Expand All @@ -58,13 +58,7 @@ public class ReferenceSource implements CRAMReferenceSource {
private final ReferenceSequenceFile rsFile;
private int downloadTriesBeforeFailing = 2;

private final Map<String, WeakReference<byte[]>> cacheW = new HashMap<>();

// Optimize for locality of reference by maintaining the backing reference bases for the most recent request.
// Failure to do this will result in the bases being aggressively GC'd when the only other reference to them
// is the weak reference hash map above, resulting in much thrashing.
private byte[] backingReferenceBases;
private int backingContigIndex;
private final Map<String, SoftReference<byte[]>> cache = new HashMap<>();

public ReferenceSource(final File file) {
this(IOUtil.toPath(file));
Expand Down Expand Up @@ -116,7 +110,7 @@ else if (Defaults.USE_CRAM_REF_DOWNLOAD) {
}

private byte[] findInCache(final String name) {
final WeakReference<byte[]> weakReference = cacheW.get(name);
final SoftReference<byte[]> weakReference = cache.get(name);
if (weakReference != null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explicitly check for an empty array here as well, and throw an informative error if you encounter one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Under what circumstances the bases array would be an empty array as supposed to a null (claimed by the GC)?

final byte[] bytes = weakReference.get();
if (bytes != null)
Expand All @@ -133,7 +127,7 @@ private byte[] addToCache(final String sequenceName, final byte[] bases) {
for (int i = 0; i < bases.length; i++) {
bases[i] = StringUtil.toUpperCase(bases[i]);
}
cacheW.put(sequenceName, new WeakReference<>(bases));
cache.put(sequenceName, new SoftReference<>(bases));
return bases;
}

Expand Down Expand Up @@ -196,13 +190,9 @@ public byte[] getReferenceBasesByRegion(

// this implementation maintains the entire reference sequence, and hands out whatever region
// of it is requested
final byte[] bases = getBackingBases(sequenceRecord);
final byte[] bases = getReferenceBases(sequenceRecord, false);

if (bases != null) {
// cache the backing bases to prevent thrashing due to aggressive GC
backingReferenceBases = bases;
backingContigIndex = sequenceRecord.getSequenceIndex();

if (zeroBasedStart >= bases.length) {
throw new IllegalArgumentException(String.format("Requested start %d is beyond the sequence length %s",
zeroBasedStart,
Expand All @@ -213,14 +203,6 @@ public byte[] getReferenceBasesByRegion(
return bases;
}

private byte[] getBackingBases(final SAMSequenceRecord sequenceRecord) {
if (backingReferenceBases != null && backingContigIndex == sequenceRecord.getSequenceIndex()) {
return backingReferenceBases;
} else {
return getReferenceBases(sequenceRecord, false);
}
}

private byte[] findBasesByName(final String name, final boolean tryVariants) {
if (rsFile == null || !rsFile.isIndexed())
return null;
Expand Down