From 5556d31d0d0194cad7c4983fc3bc4191038b4256 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20=C4=8Cernock=C3=BD?= Date: Tue, 28 Jul 2015 11:14:45 +0200 Subject: [PATCH] storing fields in MessageFields as indexed List instead of multiple Maps - speed and space optimization --- .../message/context/MessageContext.java | 31 +++++++- .../internal/message/field/MessageFields.java | 79 ++++++++----------- 2 files changed, 64 insertions(+), 46 deletions(-) diff --git a/message_generation/src/main/java/org/ros/internal/message/context/MessageContext.java b/message_generation/src/main/java/org/ros/internal/message/context/MessageContext.java index 11f4a2b4..4db3b75f 100644 --- a/message_generation/src/main/java/org/ros/internal/message/context/MessageContext.java +++ b/message_generation/src/main/java/org/ros/internal/message/context/MessageContext.java @@ -44,6 +44,10 @@ public class MessageContext { private final Map fieldSetterNames; private final List fieldNames; + private final Map fieldIndexes; + private final Map fieldGetterIndexes; + private final Map fieldSetterIndexes; + public MessageContext(MessageDeclaration messageDeclaration, MessageFactory messageFactory) { this.messageDeclaration = messageDeclaration; this.messageFactory = messageFactory; @@ -51,6 +55,9 @@ public MessageContext(MessageDeclaration messageDeclaration, MessageFactory mess this.fieldGetterNames = Maps.newHashMap(); this.fieldSetterNames = Maps.newHashMap(); this.fieldNames = Lists.newArrayList(); + this.fieldIndexes = Maps.newHashMap(); + this.fieldGetterIndexes = Maps.newHashMap(); + this.fieldSetterIndexes = Maps.newHashMap(); } public MessageFactory getMessageFactory() { @@ -79,9 +86,16 @@ public String getDefinition() { public void addFieldFactory(String name, FieldFactory fieldFactory) { fieldFactories.put(name, fieldFactory); - fieldGetterNames.put(name, "get" + getJavaName(name)); - fieldSetterNames.put(name, "set" + getJavaName(name)); + String javaName = getJavaName(name); + String getter = "get" + javaName; + String setter = "set" + javaName; + fieldGetterNames.put(name, getter); + fieldSetterNames.put(name, setter); fieldNames.add(name); + int index = fieldIndexes.size(); + fieldIndexes.put(name, index); + fieldGetterIndexes.put(getter, index); + fieldSetterIndexes.put(setter, index); } private String getJavaName(String name) { @@ -110,6 +124,18 @@ public FieldFactory getFieldFactory(String name) { return fieldFactories.get(name); } + public Integer getFieldIndexByName(String name) { + return fieldIndexes.get(name); + } + + public Integer getFieldIndexByGetterName(String name) { + return fieldGetterIndexes.get(name); + } + + public Integer getFieldIndexBySetterName(String name) { + return fieldSetterIndexes.get(name); + } + /** * @return a {@link List} of field names in the order they were added */ @@ -141,4 +167,5 @@ public boolean equals(Object obj) { return false; return true; } + } diff --git a/message_generation/src/main/java/org/ros/internal/message/field/MessageFields.java b/message_generation/src/main/java/org/ros/internal/message/field/MessageFields.java index c79f3f3c..65f7e2e3 100644 --- a/message_generation/src/main/java/org/ros/internal/message/field/MessageFields.java +++ b/message_generation/src/main/java/org/ros/internal/message/field/MessageFields.java @@ -16,58 +16,54 @@ package org.ros.internal.message.field; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; - import org.ros.exception.RosMessageRuntimeException; import org.ros.internal.message.context.MessageContext; +import java.util.Arrays; import java.util.Collections; import java.util.List; -import java.util.Map; /** * @author damonkohler@google.com (Damon Kohler) */ public class MessageFields { - private final Map fields; - private final Map setters; - private final Map getters; - private final List orderedFields; + private final MessageContext messageContext; + private final List fields; public MessageFields(MessageContext messageContext) { - fields = Maps.newHashMap(); - setters = Maps.newHashMap(); - getters = Maps.newHashMap(); - orderedFields = Lists.newArrayList(); - for (String name : messageContext.getFieldNames()) { - Field field = messageContext.getFieldFactory(name).create(); - fields.put(name, field); - getters.put(messageContext.getFieldGetterName(name), field); - setters.put(messageContext.getFieldSetterName(name), field); - orderedFields.add(field); + this.messageContext = messageContext; + List fieldNames = messageContext.getFieldNames(); + Field[] fieldsArr = new Field[fieldNames.size()]; + for (int i = 0; i < fieldsArr.length; i++) { + String fieldName = fieldNames.get(i); + fieldsArr[i] = messageContext.getFieldFactory(fieldName).create(); } + fields = Collections.unmodifiableList(Arrays.asList(fieldsArr)); } public Field getField(String name) { - return fields.get(name); + return getField(messageContext.getFieldIndexByName(name)); } public Field getSetterField(String name) { - return setters.get(name); + return getField(messageContext.getFieldIndexBySetterName(name)); } public Field getGetterField(String name) { - return getters.get(name); + return getField(messageContext.getFieldIndexByGetterName(name)); + } + + private Field getField(Integer index) { + return index != null ? fields.get(index) : null; } public List getFields() { - return Collections.unmodifiableList(orderedFields); + return fields; } public Object getFieldValue(String name) { - Field field = fields.get(name); + Field field = getField(name); if (field != null) { return field.getValue(); } @@ -75,7 +71,7 @@ public Object getFieldValue(String name) { } public void setFieldValue(String name, Object value) { - Field field = fields.get(name); + Field field = getField(name); if (field != null) { field.setValue(value); } else { @@ -85,32 +81,27 @@ public void setFieldValue(String name, Object value) { @Override public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((fields == null) ? 0 : fields.hashCode()); - result = prime * result + ((orderedFields == null) ? 0 : orderedFields.hashCode()); + int result = messageContext.hashCode(); + result = 31 * result + fields.hashCode(); return result; } @Override - public boolean equals(Object obj) { - if (this == obj) + public boolean equals(Object o) { + if (this == o) { return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - MessageFields other = (MessageFields) obj; - if (fields == null) { - if (other.fields != null) - return false; - } else if (!fields.equals(other.fields)) + } + if (!(o instanceof MessageFields)) { return false; - if (orderedFields == null) { - if (other.orderedFields != null) - return false; - } else if (!orderedFields.equals(other.orderedFields)) + } + + MessageFields that = (MessageFields) o; + + if (!messageContext.equals(that.messageContext)) { return false; - return true; + } + return fields.equals(that.fields); + } + }