IGNITE-1917: Optimized handles usage during write.
Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/1bfae544 Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/1bfae544 Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/1bfae544 Branch: refs/heads/ignite-1917 Commit: 1bfae544000038c53d2eb51334bc134567c46b01 Parents: ebd612a Author: vozerov-gridgain <[email protected]> Authored: Mon Nov 16 11:51:34 2015 +0300 Committer: vozerov-gridgain <[email protected]> Committed: Mon Nov 16 11:51:34 2015 +0300 ---------------------------------------------------------------------- .../internal/portable/BinaryWriterExImpl.java | 35 ++++--- .../internal/portable/BinaryWriterHandles.java | 101 +++++++++++++++++++ .../portable/PortableClassDescriptor.java | 14 +-- .../internal/portable/PortableContext.java | 7 +- 4 files changed, 134 insertions(+), 23 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/1bfae544/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryWriterExImpl.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryWriterExImpl.java b/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryWriterExImpl.java index a23ccb3..9bd1a50 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryWriterExImpl.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryWriterExImpl.java @@ -37,7 +37,6 @@ import java.math.BigInteger; import java.sql.Timestamp; import java.util.Collection; import java.util.Date; -import java.util.IdentityHashMap; import java.util.Map; import java.util.UUID; @@ -116,7 +115,7 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje private int rawOffPos; /** Handles. */ - private Map<Object, Integer> handles; + private BinaryWriterHandles handles; /** Output stream. */ private PortableOutputStream out; @@ -153,7 +152,6 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje this.ctx = ctx; this.out = new PortableHeapOutputStream(INIT_CAP, tlsCtx.chunk); - this.handles = new IdentityHashMap<>(); start = out.position(); schema = tlsCtx.schema; @@ -176,7 +174,6 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje BinaryWriterExImpl(PortableContext ctx, PortableOutputStream out) { this.ctx = ctx; this.out = out; - this.handles = new IdentityHashMap<>(); start = out.position(); @@ -189,7 +186,7 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje * @param handles Handles. */ private BinaryWriterExImpl(PortableContext ctx, PortableOutputStream out, SchemaHolder schema, - Map<Object, Integer> handles) { + BinaryWriterHandles handles) { this.ctx = ctx; this.out = out; this.schema = schema; @@ -550,7 +547,7 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje if (obj == null) doWriteByte(NULL); else { - BinaryWriterExImpl writer = new BinaryWriterExImpl(ctx, out, schema, handles); + BinaryWriterExImpl writer = new BinaryWriterExImpl(ctx, out, schema, handles()); writer.marshal(obj); } @@ -1456,8 +1453,7 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje if (obj == null) doWriteByte(NULL); else { - BinaryWriterExImpl writer = - new BinaryWriterExImpl(ctx, out, schema, new IdentityHashMap<Object, Integer>()); + BinaryWriterExImpl writer = new BinaryWriterExImpl(ctx, out, schema, null); writer.marshal(obj); } @@ -1797,6 +1793,19 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje } /** + * Get current handles. If they are {@code null}, then we should create them. Otherwise we will not see updates + * performed by child writers. + * + * @return Handles. + */ + private BinaryWriterHandles handles() { + if (handles == null) + handles = new BinaryWriterHandles(); + + return handles; + } + + /** * Attempts to write the object as a handle. * * @param obj Object to write. @@ -1807,13 +1816,13 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje int pos = out.position(); - Integer old = handles.put(obj, pos); + BinaryWriterHandles handles0 = handles(); - if (old == null) + int old = handles0.put(obj, pos); + + if (old == BinaryWriterHandles.POS_NULL) return false; else { - handles.put(obj, old); - doWriteByte(GridPortableMarshaller.HANDLE); doWriteInt(pos - old); @@ -1828,7 +1837,7 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje * @return New writer. */ public BinaryWriterExImpl newWriter(int typeId) { - BinaryWriterExImpl res = new BinaryWriterExImpl(ctx, out, schema, handles); + BinaryWriterExImpl res = new BinaryWriterExImpl(ctx, out, schema, handles()); res.typeId = typeId; http://git-wip-us.apache.org/repos/asf/ignite/blob/1bfae544/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryWriterHandles.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryWriterHandles.java b/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryWriterHandles.java new file mode 100644 index 0000000..2a47a2b --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryWriterHandles.java @@ -0,0 +1,101 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.internal.portable; + +import java.util.IdentityHashMap; + +/** + * Writer handles. Aimed to delay hash map allocation for some time until it is clearly evident that it is needed. + */ +public class BinaryWriterHandles { + /** Value denoting null position. */ + public static final int POS_NULL = -1; + + /** Mode: empty. */ + private static final int MODE_EMPTY = 0; + + /** Mode: single object. */ + private static final int MODE_SINGLE = 1; + + /** Mode: multiple objects. */ + private static final int MODE_MULTIPLE = 2; + + /** Data. This is either an object or a map. */ + private Object data; + + /** Position. */ + private int singlePos; + + /** Mode. */ + private int mode = MODE_EMPTY; + + /** + * Put object to registry and return previous position (if any). + * + * @param obj Object. + * @param pos Position. + * @return Old position. + */ + @SuppressWarnings("unchecked") + public int put(Object obj, int pos) { + assert obj != null; + assert pos >= 0; + + switch (mode) { + case MODE_EMPTY: + this.data = obj; + this.singlePos = pos; + this.mode = MODE_SINGLE; + + return POS_NULL; + + case MODE_SINGLE: + if (this.data == obj) + return singlePos; + else { + IdentityHashMap<Object, Integer> newData = new IdentityHashMap<>(2); + + newData.put(data, singlePos); + newData.put(obj, pos); + + this.data = newData; + this.singlePos = -1; + this.mode = MODE_MULTIPLE; + + return POS_NULL; + } + + default: + assert mode == MODE_MULTIPLE; + + IdentityHashMap<Object, Integer> data0 = (IdentityHashMap<Object, Integer>)data; + + Integer oldPos = data0.put(obj, pos); + + if (oldPos != null) { + // Restore initial position and return it. + data0.put(obj, oldPos); + + return oldPos; + } + else + return POS_NULL; + + } + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/1bfae544/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableClassDescriptor.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableClassDescriptor.java b/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableClassDescriptor.java index 6d6381b..0946fa7 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableClassDescriptor.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableClassDescriptor.java @@ -99,7 +99,7 @@ public class PortableClassDescriptor { private final Map<String, Integer> stableFieldsMeta; /** Object schemas. Initialized only for serializable classes and contains only 1 entry. */ - private final Collection<PortableSchema> stableSchemas; + private final PortableSchema stableSchema; /** Schema registry. */ private final PortableSchemaRegistry schemaReg; @@ -210,7 +210,7 @@ public class PortableClassDescriptor { ctor = null; fields = null; stableFieldsMeta = null; - stableSchemas = null; + stableSchema = null; break; @@ -219,7 +219,7 @@ public class PortableClassDescriptor { ctor = constructor(cls); fields = null; stableFieldsMeta = null; - stableSchemas = null; + stableSchema = null; break; @@ -264,7 +264,7 @@ public class PortableClassDescriptor { fields = fields0.toArray(new BinaryFieldAccessor[fields0.size()]); - stableSchemas = Collections.singleton(schemaBuilder.build()); + stableSchema = schemaBuilder.build(); break; @@ -312,10 +312,10 @@ public class PortableClassDescriptor { } /** - * @return Schemas. + * @return Schema. */ - Collection<PortableSchema> schemas() { - return stableSchemas; + PortableSchema schema() { + return stableSchema; } /** http://git-wip-us.apache.org/repos/asf/ignite/blob/1bfae544/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableContext.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableContext.java b/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableContext.java index afc23e1..193e368 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableContext.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableContext.java @@ -31,6 +31,7 @@ import java.net.URLClassLoader; import java.sql.Timestamp; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.Date; import java.util.Enumeration; import java.util.HashMap; @@ -573,8 +574,8 @@ public class PortableContext implements Externalizable { mappers.putIfAbsent(typeId, idMapper); - metaHnd.addMeta(typeId, - new BinaryMetadata(typeId, typeName, desc.fieldsMeta(), null, desc.schemas()).wrap(this)); + metaHnd.addMeta(typeId, new BinaryMetadata(typeId, typeName, desc.fieldsMeta(), null, + Collections.singleton(desc.schema())).wrap(this)); return desc; } @@ -782,7 +783,7 @@ public class PortableContext implements Externalizable { ); fieldsMeta = desc.fieldsMeta(); - schemas = desc.schemas(); + schemas = Collections.singleton(desc.schema()); if (IgniteUtils.detectClassLoader(cls).equals(dfltLdr)) userTypes.put(id, desc);
