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/23 14:41:33 UTC

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

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



##########
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 `hash += 31*hash + a[i]`  in a loop.
   Does anyone know if JVMs can unroll the loop and vectorize this well?
   I've read discussions about that and manual unrolling gives ~1.5-2x boost.
   
   int i = 0
   for (;I<= a.length-4; i+=4) {
     hash += 31*31*31*31 * a[i];
     hash += 31*31*31 * a[i+1];
     hash += 31* a[i + 2];
     hash += a[i + 3];
   }
   for (;I<= a.length; i++) 
     hash += 31* a[i];
   




-- 
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