Skip to content

Commit

Permalink
Fix caching in OpenIE; fix build (I think)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabor Angeli authored and Stanford NLP committed Feb 28, 2016
1 parent 7640535 commit b820afd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 25 deletions.
38 changes: 14 additions & 24 deletions src/edu/stanford/nlp/simple/Document.java
Expand Up @@ -20,6 +20,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.ref.SoftReference;
import java.util.*;
import java.util.function.Function;
import java.util.function.Supplier;
Expand Down Expand Up @@ -255,16 +256,7 @@ private synchronized static Annotator getOrCreate(AnnotatorFactory factory) {
* There is an attempt to mitigate this in {@link edu.stanford.nlp.simple.Document#finalize()}}, but one
* should never rely on the finalize method.
*/
private static final IdentityHashMap<Document, Annotation> annotationPool = new IdentityHashMap<>();
/**
* The keys for documents we've annotated, to determine which elements of the cache to delete first.
*/
private static final LinkedList<Document> annotationPoolKeys = new LinkedList<>();
/**
* The maximum number of Annotation objects to keep in the cache in {@link Document#annotationPool}.
*/
private static final int MAX_ANNOTATION_POOL_SIZE = 64;

private static final IdentityHashMap<Document, SoftReference<Annotation>> annotationPool = new IdentityHashMap<>();

/** The protocol buffer representing this document */
private final CoreNLPProtos.Document.Builder impl;
Expand Down Expand Up @@ -522,9 +514,6 @@ public List<Sentence> sentences(Properties props) {
synchronized (annotationPool) {
annotationPool.remove(this);
}
synchronized (annotationPoolKeys) {
annotationPoolKeys.remove(this);
}

return sentences;
}
Expand Down Expand Up @@ -832,19 +821,23 @@ protected Document runKBP(Properties props) {
*/
public Annotation asAnnotation() {
synchronized (annotationPool) {
Annotation candidate = annotationPool.get(this);
Annotation candidate = annotationPool.get(this).get();
if (candidate == null) {
// Redo cache
synchronized (serializer) {
candidate = serializer.fromProto(serialize());
}
annotationPool.put(this, candidate);
synchronized (annotationPoolKeys) {
annotationPoolKeys.addLast(this);
// Manage the cache's size
while (annotationPoolKeys.size() > MAX_ANNOTATION_POOL_SIZE) {
annotationPool.remove(annotationPoolKeys.removeFirst());
}
annotationPool.put(this, new SoftReference<>(candidate));
// Clean up garbage collected annotations
if (annotationPool.size() > 64) {
new HashSet<>(annotationPool.keySet()).stream()
.filter(key -> annotationPool.get(key).get() == null)
.forEach(annotationPool::remove);
}
// Limit the size of the pool in general, if still too large
// This shouldn't happen often...
while (annotationPool.size() > 64) {
annotationPool.remove(annotationPool.keySet().iterator().next());
}
}
return candidate;
Expand Down Expand Up @@ -940,9 +933,6 @@ protected void finalize() throws Throwable {
synchronized (annotationPool) {
annotationPool.remove(this);
}
synchronized (annotationPoolKeys) {
annotationPoolKeys.remove(this);
}
}

}
20 changes: 20 additions & 0 deletions src/edu/stanford/nlp/util/StringUtils.java
Expand Up @@ -2643,4 +2643,24 @@ public static String expandEnvironmentVariables(String raw) {
return expandEnvironmentVariables(raw, System.getenv());
}


/**
* Logs the command line arguments to Redwood on the given channels.
* The logger should be a RedwoodChannels of a single channel: the main class.
*
* @param log The redwood logger to log to.
* @param args The command-line arguments to log.
*/
public static void logInvocationString(Redwood.RedwoodChannels log, String[] args) {
if (log.channelNames.length > 0) {
if (log.channelNames[0] instanceof Class) {
log.info(toInvocationString(((Class) log.channelNames[0]).getSimpleName(), args));
} else {
log.info(toInvocationString(log.channelNames[0].toString(), args));

}
} else {
log.info(toInvocationString("CoreNLP", args));
}
}
}
2 changes: 1 addition & 1 deletion src/edu/stanford/nlp/util/logging/Redwood.java
Expand Up @@ -1195,7 +1195,7 @@ public static void printChannels(int width){
*/
@SuppressWarnings("unused")
public static class RedwoodChannels {
private final Object[] channelNames;
public final Object[] channelNames;

public RedwoodChannels(Object... channelNames) {
this.channelNames = channelNames;
Expand Down

0 comments on commit b820afd

Please sign in to comment.