dschneider-pivotal commented on a change in pull request #7261: URL: https://github.com/apache/geode/pull/7261#discussion_r797906937
########## File path: geode-for-redis/src/main/java/org/apache/geode/redis/internal/data/collections/SizeableByteArrayList.java ########## @@ -0,0 +1,90 @@ +/* + * 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.geode.redis.internal.data.collections; + +import static org.apache.geode.internal.JvmSizeUtils.getObjectHeaderSize; +import static org.apache.geode.internal.JvmSizeUtils.getReferenceSize; +import static org.apache.geode.internal.JvmSizeUtils.memoryOverhead; +import static org.apache.geode.internal.JvmSizeUtils.roundUpSize; + +import java.util.Arrays; +import java.util.LinkedList; +import java.util.ListIterator; + +import org.apache.geode.internal.size.Sizeable; + +public class SizeableByteArrayList extends LinkedList<byte[]> implements Sizeable { + private static final int BYTE_ARRAY_LIST_OVERHEAD = memoryOverhead(SizeableByteArrayList.class); + private static final int NODE_OVERHEAD = + roundUpSize(getObjectHeaderSize() + 3 * getReferenceSize()); + private static final int BYTE_ARRAY_BASE_OVERHEAD = 16; + private int memberOverhead; + + @Override + public int indexOf(Object o) { + ListIterator<byte[]> iterator = this.listIterator(); + while (iterator.hasNext()) { + int index = iterator.nextIndex(); + byte[] element = iterator.next(); + if (Arrays.equals(element, (byte[]) o)) { + return index; + } + } + return -1; + } + + @Override + public int lastIndexOf(Object o) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean remove(Object o) { + int index = indexOf(o); + if (index == -1) { + return false; + } + memberOverhead -= calculateByteArrayOverhead((byte[]) o); + remove(index); + return true; + } + + @Override + public byte[] remove(int index) { + byte[] element = super.remove(index); + memberOverhead -= calculateByteArrayOverhead(element); + return element; + } + + @Override + public void addFirst(byte[] element) { + memberOverhead += calculateByteArrayOverhead(element); + super.addFirst(element); + } + + public boolean removeLastOccurrence(Object o) { + throw new UnsupportedOperationException(); + } + + private int calculateByteArrayOverhead(byte[] element) { + return BYTE_ARRAY_BASE_OVERHEAD + (element.length % 8 == 0 ? 0 : 8) + Review comment: I think you should definitely use JvmSizeUtils.memoryOverhead(byte[]) to compute the memory used by a a byte array; "element" in this case. If you look at the callers of this method you will see we already use it in RedisSet.MemberSet, RedisString, and SizeableBytes2ObjectOpenCustomHashMapWithCursor. If we have other code that does all this math to size a byte array, then yes, file a ticket to fix that. But don't let that stop you from cleaning up this new code now. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
