Skip to content

Commit

Permalink
[19] Avoid costly test during reference serialisation
Browse files Browse the repository at this point in the history
This should help mostly in presence of references with high
cardinalities as it avoids an O(n*n) iteration when serialising them.

Bug: #19
Signed-off-by: Pierre-Charles David <[email protected]>
  • Loading branch information
pcdavid committed Jun 24, 2024
1 parent 6c48ff9 commit 38cebd9
Showing 1 changed file with 3 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -1419,61 +1418,21 @@ private JsonElement serializeMultipleNonContainmentEReference(EObject eObject, E
JsonArray jsonArray = new JsonArray();
Object referenceValue = this.helper.getValue(eObject, eReference);
for (EObject value : (InternalEList<? extends EObject>) referenceValue) {
switch (this.docKindMany(eObject, eReference)) {
case SAME_DOC:
if (!value.eIsProxy() && value.eResource() == this.helper.getResource()) {
String id = this.helper.getIDREF(value);
id = this.removeFragmentSeparator(id);
if (!id.isEmpty()) {
jsonArray.add(new JsonPrimitive(id));
}
break;
case CROSS_DOC:
if (value != null) {
jsonArray.add(new JsonPrimitive(this.saveHref(value, eReference)));
}
break;
default:
} else {
jsonArray.add(new JsonPrimitive(this.saveHref(value, null)));
}
}

jsonElement = jsonArray;
return jsonElement;
}

/**
* Return where given ERference of the given EObject are. If at least one reference is in an other resource, all
* references are treated as references to other resources.
*
* @param eObject
* the given EObject
* @param eReference
* the given ERference
* @return where given ERference of the given EObject are
*/
private int docKindMany(EObject eObject, EReference eReference) {
int referenceType = GsonEObjectSerializer.SAME_DOC;
@SuppressWarnings("unchecked")
InternalEList<? extends InternalEObject> internalEList = (InternalEList<? extends InternalEObject>) this.helper.getValue(eObject, eReference);

if (internalEList.isEmpty()) {
referenceType = GsonEObjectSerializer.SKIP;
}

Iterator<? extends InternalEObject> it = internalEList.iterator();
while (referenceType != GsonEObjectSerializer.SKIP && referenceType != GsonEObjectSerializer.CROSS_DOC && it.hasNext()) {
InternalEObject internalEObject = it.next();
if (internalEObject.eIsProxy()) {
referenceType = GsonEObjectSerializer.CROSS_DOC;
} else {
Resource resource = internalEObject.eResource();
if (resource != this.helper.getResource() && resource != null) {
referenceType = GsonEObjectSerializer.CROSS_DOC;
}
}
}
return referenceType;
}

/**
* Returns a JsonElement representing a single non containment reference value.
*
Expand Down

0 comments on commit 38cebd9

Please sign in to comment.