You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by vo...@apache.org on 2019/01/30 08:42:41 UTC
[ignite] branch master updated: IGNITE-11115: Binary: avoid
expensive ThreadLocal.set() calls on hot serialization/deserialization
path. This closes #5955.
This is an automated email from the ASF dual-hosted git repository.
vozerov pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ignite.git
The following commit(s) were added to refs/heads/master by this push:
new 40977a8 IGNITE-11115: Binary: avoid expensive ThreadLocal.set() calls on hot serialization/deserialization path. This closes #5955.
40977a8 is described below
commit 40977a8b487f71377555ab4fe0052dbc59011eb1
Author: devozerov <vo...@gridgain.com>
AuthorDate: Wed Jan 30 11:42:32 2019 +0300
IGNITE-11115: Binary: avoid expensive ThreadLocal.set() calls on hot serialization/deserialization path. This closes #5955.
---
.../internal/binary/BinaryContextHolder.java | 47 ++++++++++++++++++++++
.../internal/binary/GridBinaryMarshaller.java | 19 +++------
2 files changed, 53 insertions(+), 13 deletions(-)
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryContextHolder.java b/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryContextHolder.java
new file mode 100644
index 0000000..b1e6ced
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryContextHolder.java
@@ -0,0 +1,47 @@
+/*
+ * 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.binary;
+
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Binary context holder. We use to avoid {@code ThreadLocal.clear()} and/or {{}}ThreadLocal.set()}} operations on
+ * every serialization/deserialization, as they may take considerable amount of CPU time (confirmed by benchmarks).
+ */
+public class BinaryContextHolder {
+ /** Context. */
+ private BinaryContext ctx;
+
+ /**
+ * @return Context.
+ */
+ @Nullable public BinaryContext get() {
+ return ctx;
+ }
+
+ /**
+ * @param newCtx New context.
+ * @return Previous context.
+ */
+ @Nullable public BinaryContext set(@Nullable BinaryContext newCtx) {
+ BinaryContext oldCtx = ctx;
+
+ ctx = newCtx;
+
+ return oldCtx;
+ }
+}
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/binary/GridBinaryMarshaller.java b/modules/core/src/main/java/org/apache/ignite/internal/binary/GridBinaryMarshaller.java
index aca48ea..decf369 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/binary/GridBinaryMarshaller.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/binary/GridBinaryMarshaller.java
@@ -33,14 +33,11 @@ import org.jetbrains.annotations.Nullable;
*/
public class GridBinaryMarshaller {
/** */
- public static final ThreadLocal<Boolean> KEEP_BINARIES = new ThreadLocal<Boolean>() {
- @Override protected Boolean initialValue() {
- return true;
- }
- };
+ public static final ThreadLocal<Boolean> KEEP_BINARIES = ThreadLocal.withInitial(() -> true);
/** Binary context in TLS store. */
- private static final ThreadLocal<BinaryContext> BINARY_CTX = new ThreadLocal<>();
+ private static final ThreadLocal<BinaryContextHolder> BINARY_CTX =
+ ThreadLocal.withInitial(BinaryContextHolder::new);
/** */
public static final byte OPTM_MARSH = -2;
@@ -330,11 +327,7 @@ public class GridBinaryMarshaller {
* @return Old binary context.
*/
@Nullable private static BinaryContext pushContext(BinaryContext ctx) {
- BinaryContext old = BINARY_CTX.get();
-
- BINARY_CTX.set(ctx);
-
- return old;
+ return BINARY_CTX.get().set(ctx);
}
/**
@@ -343,7 +336,7 @@ public class GridBinaryMarshaller {
* @param oldCtx Old binary context.
*/
public static void popContext(@Nullable BinaryContext oldCtx) {
- BINARY_CTX.set(oldCtx);
+ BINARY_CTX.get().set(oldCtx);
}
/**
@@ -389,7 +382,7 @@ public class GridBinaryMarshaller {
* @return Thread-bound context.
*/
public static BinaryContext threadLocalContext() {
- BinaryContext ctx = GridBinaryMarshaller.BINARY_CTX.get();
+ BinaryContext ctx = BINARY_CTX.get().get();
if (ctx == null) {
IgniteKernal ignite = IgnitionEx.localIgnite();