You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ignite.apache.org by GitBox <gi...@apache.org> on 2021/04/26 14:12:46 UTC

[GitHub] [ignite-3] tmagomedov commented on a change in pull request #105: IGNITE-14407 introduced in-memory vault implementation

tmagomedov commented on a change in pull request #105:
URL: https://github.com/apache/ignite-3/pull/105#discussion_r620336078



##########
File path: modules/core/src/main/java/org/apache/ignite/lang/ByteArray.java
##########
@@ -0,0 +1,98 @@
+/*
+ * 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.lang;
+
+import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * A class for handling byte array.
+ */
+public final class ByteArray implements Comparable<ByteArray> {
+    /** Byte-wise representation of the {@code ByteArray}. */
+    @NotNull
+    private final byte[] arr;
+
+    /**
+     * Constructs {@code ByteArray} instance from the given byte array. <em>Note:</em> copy of the given byte array will not be
+     * created in order to avoid redundant memory consumption.
+     *
+     * @param arr Byte array. Can't be {@code null}.
+     */
+    public ByteArray(@NotNull byte[] arr) {
+        this.arr = arr;
+    }
+
+    /**
+     * Constructs {@code ByteArray} instance from the given string.
+     *
+     * @param s The string {@code ByteArray} representation. Can't be {@code null}.
+     */
+    public static ByteArray fromString(@NotNull String s) {
+        return new ByteArray(s.getBytes(StandardCharsets.UTF_8));
+    }
+
+    /**
+     * Returns the {@code ByteArray} as byte array.
+     *
+     * @return Bytes of the {@code ByteArray}.
+     */
+    public byte[] bytes() {
+        return arr;
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean equals(Object o) {
+        if (this == o) return true;
+
+        if (o == null || getClass() != o.getClass()) return false;
+
+        ByteArray byteArray = (ByteArray)o;
+
+        return Arrays.equals(arr, byteArray.arr);
+    }
+
+    /** {@inheritDoc} */
+    @Override public int hashCode() {
+        return Arrays.hashCode(arr);

Review comment:
       Arrays.hashcode [uses](https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/util/Arrays.java#L4377) `hash = 31 * hash + a[i]` , not `+=`
   At the end we have expression
   `31^n + 31^(n-1) * a[0] + 31^(n-2) * a[1] + ... + 31^2 * a[n-3] + 31 * a[n-2] + a[n-1]`
   for byte array of length `n`.
   This can be vectorized (unrolled) either by using all pre-calculated `n` powers of 31 either by calculating expression from right part to left, reading a[] in inversed order and maintaining several powers of 31.
   Example of factor 4 unrolled `hashcode()`:
   ```java
   public static int hashCodeVectorized(byte a[]) {
       if (a == null)
           return 0;
   
       int p31[] = new int[4];
   
       p31[0] = 1;
       p31[1] = p31[0] * 31;
       p31[2] = p31[1] * 31;
       p31[3] = p31[2] * 31;
   
       int p31p4 = 31 * 31 * 31 * 31;
   
       int result = 0;
   
       int i;
   
       for (i = a.length - 1; i >= 3; i -= 4) {
           result += a[i] * p31[0];
           result += a[i - 1] * p31[1];
           result += a[i - 2] * p31[2];
           result += a[i - 3] * p31[3];
   
           p31[0] *= p31p4;
           p31[1] *= p31p4;
           p31[2] *= p31p4;
           p31[3] *= p31p4;
       }
       int j = 0;
       for (; i >= 0; i--) {
           result += a[i] * p31[j++];
       }
       result += p31[j];
   
       return result;
   }
   ```
   It is not clear however if it is really worth implementing instead of standard function. It is slower than standard one according to my tests.
   
   




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org