You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by dk...@apache.org on 2020/05/26 15:01:39 UTC
[avro] branch master updated: AVRO-2068: Improved static class
constructor performance
This is an automated email from the ASF dual-hosted git repository.
dkulp pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/avro.git
The following commit(s) were added to refs/heads/master by this push:
new 1e77a40 AVRO-2068: Improved static class constructor performance
1e77a40 is described below
commit 1e77a401b951a481b8175e9e5f99afeebd9acf75
Author: David Mollitor <dm...@apache.org>
AuthorDate: Tue Jan 21 22:39:22 2020 -0500
AVRO-2068: Improved static class constructor performance
---
.../avro/src/main/java/org/apache/avro/Schema.java | 30 ++++++++++++++--------
1 file changed, 19 insertions(+), 11 deletions(-)
diff --git a/lang/java/avro/src/main/java/org/apache/avro/Schema.java b/lang/java/avro/src/main/java/org/apache/avro/Schema.java
index 6a0f7da..89e99f8 100644
--- a/lang/java/avro/src/main/java/org/apache/avro/Schema.java
+++ b/lang/java/avro/src/main/java/org/apache/avro/Schema.java
@@ -900,11 +900,12 @@ public abstract class Schema extends JsonProperties implements Serializable {
throw new AvroRuntimeException("Fields are already set");
}
int i = 0;
- fieldMap = new HashMap<>();
- LockableArrayList ff = new LockableArrayList();
+ fieldMap = new HashMap<>(Math.multiplyExact(2, fields.size()));
+ LockableArrayList<Field> ff = new LockableArrayList<>(fields.size());
for (Field f : fields) {
- if (f.position != -1)
+ if (f.position != -1) {
throw new AvroRuntimeException("Field already used: " + f);
+ }
f.position = i++;
final Field existingField = fieldMap.put(f.name(), f);
if (existingField != null) {
@@ -1020,15 +1021,18 @@ public abstract class Schema extends JsonProperties implements Serializable {
public EnumSchema(Name name, String doc, LockableArrayList<String> symbols, String enumDefault) {
super(Type.ENUM, name, doc);
this.symbols = symbols.lock();
- this.ordinals = new HashMap<>();
+ this.ordinals = new HashMap<>(Math.multiplyExact(2, symbols.size()));
this.enumDefault = enumDefault;
int i = 0;
- for (String symbol : symbols)
- if (ordinals.put(validateName(symbol), i++) != null)
+ for (String symbol : symbols) {
+ if (ordinals.put(validateName(symbol), i++) != null) {
throw new SchemaParseException("Duplicate enum symbol: " + symbol);
- if (enumDefault != null && !symbols.contains(enumDefault))
+ }
+ }
+ if (enumDefault != null && !symbols.contains(enumDefault)) {
throw new SchemaParseException(
"The Enum Default: " + enumDefault + " is not in the enum symbol set: " + symbols);
+ }
}
@Override
@@ -1167,20 +1171,24 @@ public abstract class Schema extends JsonProperties implements Serializable {
private static class UnionSchema extends Schema {
private final List<Schema> types;
- private final Map<String, Integer> indexByName = new HashMap<>();
+ private final Map<String, Integer> indexByName;
public UnionSchema(LockableArrayList<Schema> types) {
super(Type.UNION);
+ this.indexByName = new HashMap<>(Math.multiplyExact(2, types.size()));
this.types = types.lock();
int index = 0;
for (Schema type : types) {
- if (type.getType() == Type.UNION)
+ if (type.getType() == Type.UNION) {
throw new AvroRuntimeException("Nested union: " + this);
+ }
String name = type.getFullName();
- if (name == null)
+ if (name == null) {
throw new AvroRuntimeException("Nameless in union:" + this);
- if (indexByName.put(name, index++) != null)
+ }
+ if (indexByName.put(name, index++) != null) {
throw new AvroRuntimeException("Duplicate in union:" + name);
+ }
}
}