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

Reduce space used by OBO Parser #429

Merged
merged 5 commits into from
Aug 9, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add freeze methods to oboformat Clause and Frame. These methods
(a) replace ArrayLists of size 0 with emptyList()
(b) replace ArrayLists of size 1 with singletonList()
(c) call trimToSize on ArrayLists of size >1.
Modifications automatically the relevant objects.

Add freezeFrames method to OBODoc, which will call freeze on all held frames.
  • Loading branch information
sesuncedu committed Aug 8, 2015
commit 4b253bb326a227a14cb9598a3337780fbc0218e6
147 changes: 134 additions & 13 deletions oboformat/src/main/java/org/obolibrary/oboformat/model/Clause.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import org.obolibrary.oboformat.parser.OBOFormatConstants.OboFormatTag;

/** Clause */
public class Clause {

protected String tag;
@Nonnull
protected final List<Object> values = new ArrayList<>();
protected List<Object> values = new ArrayList<>();
@Nonnull
protected final Collection<Xref> xrefs = new ArrayList<>();
protected Collection<Xref> xrefs = new ArrayList<>();
@Nonnull
protected final Collection<QualifierValue> qualifierValues = new ArrayList<>();
protected Collection<QualifierValue> qualifierValues = new ArrayList<>();

/**
* @param tag
Expand Down Expand Up @@ -66,6 +65,64 @@ public Clause(@Nonnull OboFormatTag tag, String value) {
@Deprecated
public Clause() {}

/**
* freezing a clause signals that the clause has become quiescent, and that data structures can be adjusted to
* increase performance, or reduce memory consumption.
*/
void freeze() {
freezeValues();
freezeXrefs();
freezeQualifiers();
}

void freezeValues() {
switch (values.size()) {
case 0:
values = Collections.emptyList();
break;
case 1:
Object o = values.iterator().next();
values = Collections.singletonList(o);
break;
default:
((ArrayList) values).trimToSize();
}
}

void freezeXrefs() {
switch (xrefs.size()) {
case 0:
xrefs = Collections.emptyList();
break;
case 1:
Xref xref = xrefs.iterator().next();
xrefs = Collections.singletonList(xref);
break;
default:
if (values instanceof ArrayList) {
((ArrayList) values).trimToSize();
}
}
}

void freezeQualifiers() {
switch (qualifierValues.size()) {
case 0:
qualifierValues = Collections.emptyList();
break;
case 1:
QualifierValue qualifierValue = qualifierValues.iterator().next();
qualifierValues = Collections.singletonList(qualifierValue);
break;
default:
if (qualifierValues instanceof ArrayList) {
((ArrayList) qualifierValues).trimToSize();
}
}
}



/**
* @return tag
*/
Expand Down Expand Up @@ -94,24 +151,47 @@ public Collection<Object> getValues() {
* values
*/
public void setValues(@Nonnull Collection<Object> values) {
this.values.clear();
this.values.addAll(values);
if (!(this.values instanceof ArrayList)) {
switch (values.size()) {
case 0:
this.values = Collections.emptyList();
break;
case 1:
Object o = values.iterator().next();
this.values = Collections.singletonList(o);
break;
default:
this.values = new ArrayList<>(values);
}
} else {
this.values.clear();
this.values.addAll(values);
}
}

/**
* @param v
* v
*/
public void setValue(Object v) {
values.clear();
values.add(v);
if(values instanceof ArrayList) {
values.clear(); // TODO: Remove this line after profile gathering
values.add(v);
} else {
this.values = Collections.singletonList(v);
}
}

/**
* @param v
* v
*/
public void addValue(Object v) {
if (!(this.values instanceof ArrayList)) {
List<Object> newValues = new ArrayList<>(values.size() + 1);
newValues.addAll(this.values);
this.values = newValues;
}
values.add(v);
}

Expand Down Expand Up @@ -184,15 +264,34 @@ public Collection<Xref> getXrefs() {
* xrefs
*/
public void setXrefs(@Nonnull Collection<Xref> xrefs) {
this.xrefs.clear();
this.xrefs.addAll(xrefs);
if (!(this.xrefs instanceof ArrayList)) {
switch (xrefs.size()) {
case 0:
this.xrefs = Collections.emptyList();
break;
case 1:
Xref xref = xrefs.iterator().next();
this.xrefs = Collections.singletonList(xref);
break;
default:
this.xrefs = new ArrayList<>(xrefs);
}
} else {
this.xrefs.clear();
this.xrefs.addAll(xrefs);
}
}

/**
* @param xref
* xref
*/
public void addXref(Xref xref) {
if (!(xrefs instanceof ArrayList)) {
List<Xref> newXrefs = new ArrayList<>(xrefs.size() + 1);
newXrefs.addAll(xrefs);
this.xrefs = newXrefs;
}
xrefs.add(xref);
}

Expand All @@ -209,15 +308,36 @@ public Collection<QualifierValue> getQualifierValues() {
* qualifierValues
*/
public void setQualifierValues(@Nonnull Collection<QualifierValue> qualifierValues) {
this.qualifierValues.clear();
this.qualifierValues.addAll(qualifierValues);
if (!(this.qualifierValues instanceof ArrayList)) {
switch (qualifierValues.size()) {
case 0:
this.qualifierValues = Collections.emptyList();
break;
case 1:
QualifierValue qualifierValue = qualifierValues.iterator().next();
this.qualifierValues = Collections.singletonList(qualifierValue);
break;
default:
this.qualifierValues = new ArrayList<>(qualifierValues);
}
} else {
this.qualifierValues.clear();
this.qualifierValues.addAll(qualifierValues);
}
}


/**
* @param qv
* qv
*/
public void addQualifierValue(QualifierValue qv) {
if (!(qualifierValues instanceof ArrayList)) {
List<QualifierValue> newQualifierValues = new ArrayList<>(qualifierValues.size() + 1);
newQualifierValues.addAll(qualifierValues);
this.qualifierValues = newQualifierValues;
}

qualifierValues.add(qv);
}

Expand Down Expand Up @@ -327,4 +447,5 @@ public boolean equals(Object obj) {
*/
return collectionsEquals(qualifierValues, other.getQualifierValues());
}

}
33 changes: 31 additions & 2 deletions oboformat/src/main/java/org/obolibrary/oboformat/model/Frame.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import org.obolibrary.obo2owl.OboInOwlCardinalityTools;
import org.obolibrary.oboformat.parser.OBOFormatConstants.OboFormatTag;

Expand Down Expand Up @@ -52,6 +51,33 @@ protected final void init() {
clauses = new ArrayList<>();
}

/**
* freezing a frame signals that a frame has become quiescent, and that data structures can be adjusted to
* increase performance or reduce memory consumption. If a frozen frame is subsequently modified it will be thawed
* as necessary.
*/
public void freeze() {
if (clauses.isEmpty()) {
clauses = Collections.emptyList();
return;
}

for (Clause clause : clauses) {
clause.freeze();
}

if (clauses.size() == 1) {
clauses = Collections.singletonList(clauses.iterator().next());
return;
}

if (clauses instanceof ArrayList<?>) {
ArrayList<?> arrayList = (ArrayList<?>) clauses;
arrayList.trimToSize();
}

}

/**
* @return the type
*/
Expand Down Expand Up @@ -154,6 +180,9 @@ public void setClauses(Collection<Clause> clauses) {
* the clause
*/
public void addClause(Clause cl) {
if (!(clauses instanceof ArrayList)) {
clauses = new ArrayList<>(clauses.size()+1);
}
clauses.add(cl);
}

Expand Down
22 changes: 22 additions & 0 deletions oboformat/src/main/java/org/obolibrary/oboformat/model/OBODoc.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,28 @@ public Collection<Frame> getInstanceFrames() {
return instanceFrameMap.values();
}

/**
* Freezing an OBODoc signals that the document has become quiescent, and that the system may optimize data
* structures for performance or space.
*/

public void freezeFrames() {
headerFrame.freeze();
freezeFrameMap(termFrameMap);
freezeFrameMap(typedefFrameMap);
freezeFrameMap(instanceFrameMap);
for (Frame frame : annotationFrames) {
frame.freeze();
}

}

private static void freezeFrameMap(Map<String, Frame> frameMap) {
for (Frame frame : frameMap.values()) {
frame.freeze();
}
}

/**
* @param id
* the id
Expand Down