This is an automated email from the ASF dual-hosted git repository.

nizhikov 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 250e1b9edb2 IGNITE-25012 Extract GridToStringBuilder Unsafe code to 
interface (#11982)
250e1b9edb2 is described below

commit 250e1b9edb2e70d36c19b6a0b62af7368f7ab0ad
Author: Nikolay <[email protected]>
AuthorDate: Mon Apr 7 14:11:32 2025 +0300

    IGNITE-25012 Extract GridToStringBuilder Unsafe code to interface (#11982)
---
 .../org/apache/ignite/internal/IgniteKernal.java   |  12 ++
 .../util/tostring/GridToStringBuilder.java         |  24 ++--
 .../util/tostring/GridToStringFieldDescriptor.java |  63 +++++++++-
 .../ReflectionToStringFieldDescriptor.java         | 135 +++++++++++++++++++++
 .../tostring/UnsafeToStringFieldDescriptor.java    |  83 +++++++++++++
 5 files changed, 304 insertions(+), 13 deletions(-)

diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/IgniteKernal.java 
b/modules/core/src/main/java/org/apache/ignite/internal/IgniteKernal.java
index 45d5098eeb2..92d8c3b587d 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/IgniteKernal.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/IgniteKernal.java
@@ -182,7 +182,9 @@ import 
org.apache.ignite.internal.util.future.GridFinishedFuture;
 import org.apache.ignite.internal.util.future.GridFutureAdapter;
 import org.apache.ignite.internal.util.future.IgniteFutureImpl;
 import org.apache.ignite.internal.util.lang.GridAbsClosure;
+import org.apache.ignite.internal.util.tostring.GridToStringBuilder;
 import org.apache.ignite.internal.util.tostring.GridToStringExclude;
+import org.apache.ignite.internal.util.tostring.UnsafeToStringFieldDescriptor;
 import org.apache.ignite.internal.util.typedef.C1;
 import org.apache.ignite.internal.util.typedef.CI1;
 import org.apache.ignite.internal.util.typedef.F;
@@ -862,6 +864,8 @@ public class IgniteKernal implements IgniteEx, 
Externalizable {
         Thread.UncaughtExceptionHandler hnd,
         TimeBag startTimer
     ) throws IgniteCheckedException {
+        initializeToStringBuilder();
+
         gw.compareAndSet(null, new 
GridKernalGatewayImpl(cfg.getIgniteInstanceName()));
 
         GridKernalGateway gw = this.gw.get();
@@ -1502,6 +1506,14 @@ public class IgniteKernal implements IgniteEx, 
Externalizable {
         MarshallerUtils.setNodeName(marsh, ctx.igniteInstanceName());
     }
 
+    /** */
+    private void initializeToStringBuilder() {
+        if (!BinaryMarshaller.available())
+            return;
+
+        GridToStringBuilder.fldDescFactory = 
UnsafeToStringFieldDescriptor::new;
+    }
+
     /**
      * @param cfg Ignite configuration to check for possible performance 
issues.
      */
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/util/tostring/GridToStringBuilder.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/util/tostring/GridToStringBuilder.java
index d38c0f8711e..9b13ede9557 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/util/tostring/GridToStringBuilder.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/util/tostring/GridToStringBuilder.java
@@ -43,7 +43,6 @@ import java.util.function.Function;
 import java.util.function.Supplier;
 import org.apache.ignite.IgniteException;
 import org.apache.ignite.IgniteSystemProperties;
-import org.apache.ignite.internal.util.GridUnsafe;
 import org.apache.ignite.internal.util.typedef.F;
 import org.apache.ignite.internal.util.typedef.internal.SB;
 import org.jetbrains.annotations.NotNull;
@@ -99,6 +98,9 @@ public class GridToStringBuilder {
     /** */
     private static final Map<String, GridToStringClassDescriptor> classCache = 
new ConcurrentHashMap<>();
 
+    /** */
+    public static volatile Function<Field, GridToStringFieldDescriptor> 
fldDescFactory = ReflectionToStringFieldDescriptor::new;
+
     /** @see IgniteSystemProperties#IGNITE_TO_STRING_MAX_LENGTH */
     public static final int DFLT_TO_STRING_MAX_LENGTH = 10_000;
 
@@ -1118,39 +1120,39 @@ public class GridToStringBuilder {
 
                 switch (fd.type()) {
                     case GridToStringFieldDescriptor.FIELD_TYPE_OBJECT:
-                        toString(buf, fd.fieldClass(), 
GridUnsafe.getObjectField(obj, fd.offset()));
+                        toString(buf, fd.fieldClass(), fd.objectValue(obj));
 
                         break;
                     case GridToStringFieldDescriptor.FIELD_TYPE_BYTE:
-                        buf.a(GridUnsafe.getByteField(obj, fd.offset()));
+                        buf.a(fd.byteValue(obj));
 
                         break;
                     case GridToStringFieldDescriptor.FIELD_TYPE_BOOLEAN:
-                        buf.a(GridUnsafe.getBooleanField(obj, fd.offset()));
+                        buf.a(fd.booleanValue(obj));
 
                         break;
                     case GridToStringFieldDescriptor.FIELD_TYPE_CHAR:
-                        buf.a(GridUnsafe.getCharField(obj, fd.offset()));
+                        buf.a(fd.charValue(obj));
 
                         break;
                     case GridToStringFieldDescriptor.FIELD_TYPE_SHORT:
-                        buf.a(GridUnsafe.getShortField(obj, fd.offset()));
+                        buf.a(fd.shortValue(obj));
 
                         break;
                     case GridToStringFieldDescriptor.FIELD_TYPE_INT:
-                        buf.a(GridUnsafe.getIntField(obj, fd.offset()));
+                        buf.a(fd.intField(obj));
 
                         break;
                     case GridToStringFieldDescriptor.FIELD_TYPE_FLOAT:
-                        buf.a(GridUnsafe.getFloatField(obj, fd.offset()));
+                        buf.a(fd.floatField(obj));
 
                         break;
                     case GridToStringFieldDescriptor.FIELD_TYPE_LONG:
-                        buf.a(GridUnsafe.getLongField(obj, fd.offset()));
+                        buf.a(fd.longField(obj));
 
                         break;
                     case GridToStringFieldDescriptor.FIELD_TYPE_DOUBLE:
-                        buf.a(GridUnsafe.getDoubleField(obj, fd.offset()));
+                        buf.a(fd.doubleField(obj));
 
                         break;
                 }
@@ -1839,7 +1841,7 @@ public class GridToStringBuilder {
                 }
 
                 if (add) {
-                    GridToStringFieldDescriptor fd = new 
GridToStringFieldDescriptor(f);
+                    GridToStringFieldDescriptor fd = fldDescFactory.apply(f);
 
                     // Get order, if any.
                     final GridToStringOrder annOrder = 
f.getAnnotation(GridToStringOrder.class);
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/util/tostring/GridToStringFieldDescriptor.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/util/tostring/GridToStringFieldDescriptor.java
index 019ea784a6f..37a6aee131e 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/util/tostring/GridToStringFieldDescriptor.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/util/tostring/GridToStringFieldDescriptor.java
@@ -25,7 +25,7 @@ import org.intellij.lang.annotations.MagicConstant;
 /**
  * Simple field descriptor containing field name and its order in the class 
descriptor.
  */
-class GridToStringFieldDescriptor {
+public abstract class GridToStringFieldDescriptor {
     /** */
     public static final int FIELD_TYPE_OBJECT = 0;
 
@@ -72,7 +72,7 @@ class GridToStringFieldDescriptor {
     GridToStringFieldDescriptor(Field field) {
         assert (field.getModifiers() & Modifier.STATIC) == 0 : "Static fields 
are not allowed here: " + field;
 
-        off = GridUnsafe.objectFieldOffset(field);
+        off = offset(field);
 
         cls = field.getType();
 
@@ -142,4 +142,63 @@ class GridToStringFieldDescriptor {
     String getName() {
         return name;
     }
+
+    /**
+     * @return Field offset in bytes.
+     */
+    abstract long offset(Field field);
+
+    /**
+     * @param obj Object.
+     * @return field value.
+     */
+    public abstract Object objectValue(Object obj);
+
+    /**
+     * @param obj Object.
+     * @return field value.
+     */
+    public abstract byte byteValue(Object obj);
+
+    /**
+     * @param obj Object.
+     * @return field value.
+     */
+    public abstract boolean booleanValue(Object obj);
+
+    /**
+     * @param obj Object.
+     * @return field value.
+     */
+    public abstract char charValue(Object obj);
+
+    /**
+     * @param obj Object.
+     * @return field value.
+     */
+    public abstract short shortValue(Object obj);
+
+    /**
+     * @param obj Object.
+     * @return field value.
+     */
+    public abstract int intField(Object obj);
+
+    /**
+     * @param obj Object.
+     * @return field value.
+     */
+    public abstract float floatField(Object obj);
+
+    /**
+     * @param obj Object.
+     * @return field value.
+     */
+    public abstract long longField(Object obj);
+
+    /**
+     * @param obj Object.
+     * @return field value.
+     */
+    public abstract double doubleField(Object obj);
 }
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/util/tostring/ReflectionToStringFieldDescriptor.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/util/tostring/ReflectionToStringFieldDescriptor.java
new file mode 100644
index 00000000000..2d35282fd87
--- /dev/null
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/util/tostring/ReflectionToStringFieldDescriptor.java
@@ -0,0 +1,135 @@
+/*
+ * 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.util.tostring;
+
+import java.lang.reflect.Field;
+import org.apache.ignite.IgniteException;
+
+/**
+ * Simple field descriptor containing field name and its order in the class 
descriptor.
+ */
+class ReflectionToStringFieldDescriptor extends GridToStringFieldDescriptor {
+    /** Field. */
+    private final Field field;
+
+    /**
+     * @param field Field;
+     */
+    ReflectionToStringFieldDescriptor(Field field) {
+        super(field);
+
+        this.field = field;
+
+        field.setAccessible(true);
+    }
+
+    /** {@inheritDoc} */
+    @Override long offset(Field field) {
+        return 0;
+    }
+
+    /** {@inheritDoc} */
+    @Override public Object objectValue(Object obj) {
+        try {
+            return field.get(obj);
+        }
+        catch (IllegalAccessException e) {
+            throw new IgniteException(e);
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override public byte byteValue(Object obj) {
+        try {
+            return field.getByte(obj);
+        }
+        catch (IllegalAccessException e) {
+            throw new IgniteException(e);
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean booleanValue(Object obj) {
+        try {
+            return field.getBoolean(obj);
+        }
+        catch (IllegalAccessException e) {
+            throw new IgniteException(e);
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override public char charValue(Object obj) {
+        try {
+            return field.getChar(obj);
+        }
+        catch (IllegalAccessException e) {
+            throw new IgniteException(e);
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override public short shortValue(Object obj) {
+        try {
+            return field.getShort(obj);
+        }
+        catch (IllegalAccessException e) {
+            throw new IgniteException(e);
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override public int intField(Object obj) {
+        try {
+            return field.getInt(obj);
+        }
+        catch (IllegalAccessException e) {
+            throw new IgniteException(e);
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override public float floatField(Object obj) {
+        try {
+            return field.getFloat(obj);
+        }
+        catch (IllegalAccessException e) {
+            throw new IgniteException(e);
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override public long longField(Object obj) {
+        try {
+            return field.getLong(obj);
+        }
+        catch (IllegalAccessException e) {
+            throw new IgniteException(e);
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override public double doubleField(Object obj) {
+        try {
+            return field.getDouble(obj);
+        }
+        catch (IllegalAccessException e) {
+            throw new IgniteException(e);
+        }
+    }
+}
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/util/tostring/UnsafeToStringFieldDescriptor.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/util/tostring/UnsafeToStringFieldDescriptor.java
new file mode 100644
index 00000000000..cc32ec4122b
--- /dev/null
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/util/tostring/UnsafeToStringFieldDescriptor.java
@@ -0,0 +1,83 @@
+/*
+ * 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.util.tostring;
+
+import java.lang.reflect.Field;
+import org.apache.ignite.internal.util.GridUnsafe;
+
+/**
+ * Simple field descriptor containing field name and its order in the class 
descriptor.
+ */
+public class UnsafeToStringFieldDescriptor extends GridToStringFieldDescriptor 
{
+    /**
+     * @param field Field;
+     */
+    public UnsafeToStringFieldDescriptor(Field field) {
+        super(field);
+    }
+
+    /** {@inheritDoc} */
+    @Override long offset(Field field) {
+        return GridUnsafe.objectFieldOffset(field);
+    }
+
+    /** {@inheritDoc} */
+    @Override public Object objectValue(Object obj) {
+        return GridUnsafe.getObjectField(obj, offset());
+    }
+
+    /** {@inheritDoc} */
+    @Override public byte byteValue(Object obj) {
+        return GridUnsafe.getByteField(obj, offset());
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean booleanValue(Object obj) {
+        return GridUnsafe.getBooleanField(obj, offset());
+    }
+
+    /** {@inheritDoc} */
+    @Override public char charValue(Object obj) {
+        return GridUnsafe.getCharField(obj, offset());
+    }
+
+    /** {@inheritDoc} */
+    @Override public short shortValue(Object obj) {
+        return GridUnsafe.getShortField(obj, offset());
+    }
+
+    /** {@inheritDoc} */
+    @Override public int intField(Object obj) {
+        return GridUnsafe.getIntField(obj, offset());
+    }
+
+    /** {@inheritDoc} */
+    @Override public float floatField(Object obj) {
+        return GridUnsafe.getFloatField(obj, offset());
+    }
+
+    /** {@inheritDoc} */
+    @Override public long longField(Object obj) {
+        return GridUnsafe.getLongField(obj, offset());
+    }
+
+    /** {@inheritDoc} */
+    @Override public double doubleField(Object obj) {
+        return GridUnsafe.getDoubleField(obj, offset());
+    }
+}

Reply via email to