From 38cebd9f8ffbe27978844981353eb71f4f903764 Mon Sep 17 00:00:00 2001 From: Pierre-Charles David Date: Mon, 24 Jun 2024 10:30:59 +0200 Subject: [PATCH] [19] Avoid costly test during reference serialisation This should help mostly in presence of references with high cardinalities as it avoids an O(n*n) iteration when serialising them. Bug: https://github.com/eclipse-sirius/sirius-emf-json/issues/19 Signed-off-by: Pierre-Charles David --- .../emfjson/utils/GsonEObjectSerializer.java | 47 ++----------------- 1 file changed, 3 insertions(+), 44 deletions(-) diff --git a/bundles/org.eclipse.sirius.emfjson/src/main/java/org/eclipse/sirius/emfjson/utils/GsonEObjectSerializer.java b/bundles/org.eclipse.sirius.emfjson/src/main/java/org/eclipse/sirius/emfjson/utils/GsonEObjectSerializer.java index 1cce4b7..14ef27d 100644 --- a/bundles/org.eclipse.sirius.emfjson/src/main/java/org/eclipse/sirius/emfjson/utils/GsonEObjectSerializer.java +++ b/bundles/org.eclipse.sirius.emfjson/src/main/java/org/eclipse/sirius/emfjson/utils/GsonEObjectSerializer.java @@ -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; @@ -1419,20 +1418,14 @@ private JsonElement serializeMultipleNonContainmentEReference(EObject eObject, E JsonArray jsonArray = new JsonArray(); Object referenceValue = this.helper.getValue(eObject, eReference); for (EObject value : (InternalEList) 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))); } } @@ -1440,40 +1433,6 @@ private JsonElement serializeMultipleNonContainmentEReference(EObject eObject, E 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 internalEList = (InternalEList) this.helper.getValue(eObject, eReference); - - if (internalEList.isEmpty()) { - referenceType = GsonEObjectSerializer.SKIP; - } - - Iterator 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. *