diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2024-01-16 18:09:41 +0100 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2024-01-16 18:09:41 +0100 |
commit | b1c7443922468278a52a6ff3b45dc4f02104b49b (patch) | |
tree | 90f5fa0dacade121419cf715ae0116cc24b2bb6c /document | |
parent | ada510e89058979aecd66598d1853fd09ee0b42d (diff) |
- Only call getKey once.
- Just use a presized array of native ints, instead of LinkedList.
Diffstat (limited to 'document')
-rw-r--r-- | document/src/main/java/com/yahoo/document/datatypes/Struct.java | 22 | ||||
-rw-r--r-- | document/src/main/java/com/yahoo/document/serialization/VespaDocumentSerializer6.java | 30 |
2 files changed, 21 insertions, 31 deletions
diff --git a/document/src/main/java/com/yahoo/document/datatypes/Struct.java b/document/src/main/java/com/yahoo/document/datatypes/Struct.java index bb54b41069b..badcf99b16a 100644 --- a/document/src/main/java/com/yahoo/document/datatypes/Struct.java +++ b/document/src/main/java/com/yahoo/document/datatypes/Struct.java @@ -130,12 +130,8 @@ public class Struct extends StructuredFieldValue { if (myField==null) { throw new IllegalArgumentException("No such field in "+getDataType()+" : "+field.getName()); } - if (!myField - .getDataType().isValueCompatible(value)) { - throw new IllegalArgumentException( - "Incompatible data types. Got " + value.getDataType() - + ", expected " - + myField.getDataType()); + if (!myField.getDataType().isValueCompatible(value)) { + throw new IllegalArgumentException("Incompatible data types. Got " + value.getDataType() + ", expected " + myField.getDataType()); } if (myField.getId() @@ -199,10 +195,9 @@ public class Struct extends StructuredFieldValue { @Override public boolean equals(Object o) { if (this == o) return true; - if (!(o instanceof Struct)) return false; + if (!(o instanceof Struct struct)) return false; if (!super.equals(o)) return false; - Struct struct = (Struct) o; return values.equals(struct.values); } @@ -266,7 +261,7 @@ public class Struct extends StructuredFieldValue { } private class FieldEntry implements Map.Entry<Field, FieldValue> { - private int id; + private final int id; private FieldEntry(int id) { this.id = id; @@ -300,9 +295,8 @@ public class Struct extends StructuredFieldValue { public boolean equals(Object o) { if (this == o) return true; - if (!(o instanceof FieldEntry)) return false; + if (!(o instanceof FieldEntry that)) return false; - FieldEntry that = (FieldEntry) o; return (id == that.id); } @@ -327,7 +321,7 @@ public class Struct extends StructuredFieldValue { private class FieldSetIterator implements Iterator<Map.Entry<Field, FieldValue>> { private int position = 0; - private int [] increasing = getInOrder(); + private final int [] increasing = getInOrder(); public boolean hasNext() { return (position < increasing.length); @@ -337,9 +331,7 @@ public class Struct extends StructuredFieldValue { if (position >= increasing.length) { throw new NoSuchElementException("No more elements in collection"); } - FieldEntry retval = new FieldEntry(increasing[position]); - position++; - return retval; + return new FieldEntry(increasing[position++]); } public void remove() { diff --git a/document/src/main/java/com/yahoo/document/serialization/VespaDocumentSerializer6.java b/document/src/main/java/com/yahoo/document/serialization/VespaDocumentSerializer6.java index 2abaa82d42c..e14d2fea0a2 100644 --- a/document/src/main/java/com/yahoo/document/serialization/VespaDocumentSerializer6.java +++ b/document/src/main/java/com/yahoo/document/serialization/VespaDocumentSerializer6.java @@ -60,7 +60,6 @@ import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; -import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -105,8 +104,7 @@ public class VespaDocumentSerializer6 extends BufferSerializer implements Docume doc.getDataType().serialize(this); if (hasHead) { - StructuredFieldValue asStructured = doc; - write(null, asStructured); + write(null, (StructuredFieldValue) doc); } int finalPos = buf.position(); @@ -327,34 +325,35 @@ public class VespaDocumentSerializer6 extends BufferSerializer implements Docume GrowableByteBuffer buffer = new GrowableByteBuffer(4096, 2.0f); buf = buffer; - List<Integer> fieldIds = new LinkedList<>(); - List<java.lang.Integer> fieldLengths = new LinkedList<>(); + int numFields = s.getFieldCount(); + int [] fieldIds = new int[numFields]; + int [] fieldLengths = new int[numFields]; var iter = s.iterator(); - while (iter.hasNext()) { + for (int i=0; iter.hasNext(); i++) { Map.Entry<Field, FieldValue> value = iter.next(); int startPos = buffer.position(); - value.getValue().serialize(value.getKey(), this); + Field key = value.getKey(); + value.getValue().serialize(key, this); - fieldLengths.add(buffer.position() - startPos); - fieldIds.add(value.getKey().getId()); + fieldLengths[i] = buffer.position() - startPos; + fieldIds[i] = key.getId(); } // Switch buffers again: buffer.flip(); buf = bigBuffer; - int sz = fieldIds.size(); // Actual serialization starts here. int lenPos = buf.position(); putInt(null, 0); // Move back to this after compression is done. buf.put(CompressionType.NONE.getCode()); - buf.putInt1_4Bytes(sz); + buf.putInt1_4Bytes(numFields); - for (int i = 0; i < sz; ++i) { - putInt1_4Bytes(null, fieldIds.get(i)); - putInt2_4_8Bytes(null, fieldLengths.get(i)); + for (int i = 0; i < numFields; ++i) { + putInt1_4Bytes(null, fieldIds[i]); + putInt2_4_8Bytes(null, fieldLengths[i]); } int pos = buf.position(); @@ -374,8 +373,7 @@ public class VespaDocumentSerializer6 extends BufferSerializer implements Docume * @param value - field value */ public void write(FieldBase field, Struct value) { - StructuredFieldValue asStructured = value; - write(field, asStructured); + write(field, (StructuredFieldValue) value); } /** |