netudima commented on code in PR #4117: URL: https://github.com/apache/cassandra/pull/4117#discussion_r2095586772
########## src/java/org/apache/cassandra/utils/memory/BigEndianMemoryUtil.java: ########## @@ -0,0 +1,146 @@ +/* + * 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.cassandra.utils.memory; + + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; + +import com.google.common.annotations.VisibleForTesting; + +import org.apache.cassandra.utils.Architecture; + +public class BigEndianMemoryUtil extends MemoryUtil +{ + public static int getUnsignedShort(long address) + { + if (Architecture.IS_UNALIGNED || (address & 0b1) == 0L) + return (Architecture.BIG_ENDIAN ? unsafe.getShort(address) : Short.reverseBytes(unsafe.getShort(address))) & 0xffff; + else + return getShortByByte(address) & 0xffff; + } + + public static int getInt(long address) + { + if (Architecture.IS_UNALIGNED || (address & 0b11) == 0L) + return Architecture.BIG_ENDIAN ? unsafe.getInt(address) : Integer.reverseBytes(unsafe.getInt(address)); + else + return getIntByByte(address); + } + + public static long getLong(long address) + { + if (Architecture.IS_UNALIGNED || (address & 0b111) == 0L) + return Architecture.BIG_ENDIAN ? unsafe.getLong(address) : Long.reverseBytes(unsafe.getLong(address)); + else + return getLongByByte(address); + } + + public static void setShort(long address, short s) + { + if (Architecture.IS_UNALIGNED || (address & 0b1) == 0L) + unsafe.putShort(address, Architecture.BIG_ENDIAN ? s : Short.reverseBytes(s)); + else + putShortByByte(address, s); + } + + public static void setInt(long address, int l) + { + if (Architecture.IS_UNALIGNED || (address & 0b11) == 0L) + unsafe.putInt(address, Architecture.BIG_ENDIAN ? l : Integer.reverseBytes(l)); + else + putIntByByte(address, l); + } + + public static void setLong(long address, long l) + { + if (Architecture.IS_UNALIGNED || (address & 0b111) == 0L) + unsafe.putLong(address, Architecture.BIG_ENDIAN ? l : Long.reverseBytes(l)); + else + putLongByByte(address, l); + } + + @VisibleForTesting + static long getLongByByte(long address) + { + return (((long) unsafe.getByte(address ) ) << 56) | Review Comment: yes, I thought about something similar, like in JDK the following more optimal approach is used: https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/jdk/internal/misc/Unsafe.java#L3576 Reading 2 longs in this case can be tricky because we can read beyond an allocated memory in some cases .. At the same time I am not sure if we need to spend extra efforts for aligned architectures without an explicit request from users if they are so rare .. -- 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. To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org For additional commands, e-mail: pr-h...@cassandra.apache.org