dschneider-pivotal commented on a change in pull request #6699: URL: https://github.com/apache/geode/pull/6699#discussion_r673358353
########## File path: geode-core/src/main/java/org/apache/geode/internal/size/SizeableObjectSizer.java ########## @@ -0,0 +1,33 @@ +/* + * 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.internal.size; + +/** + * A sizer that allows for efficient sizing of objects that implement the {@link Sizeable} + * interface, and delegates to {@link ReflectionSingleObjectSizer} otherwise. + */ +public class SizeableObjectSizer implements SingleObjectSizer { + + private final SingleObjectSizer sizer = new ReflectionSingleObjectSizer(); + + @Override + public long sizeof(Object object) { Review comment: I noticed you cast to "int" in the calls of this. Would it be better for this sizer to match Sizable and have sizeof return "int" instead of "long"? Instead of simply casting "sizer.sizeof" you could check if it is > Integer.MAX and if it is return Integer.MAX. I really doubt any objects will be larger than Integer.MAX but if so these seems better than a cast. ########## File path: geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/data/RedisSortedSet.java ########## @@ -173,20 +176,23 @@ public int getDSFID() { } protected synchronized byte[] memberAdd(byte[] memberToAdd, byte[] scoreToAdd) { - byte[] oldScore = null; - - OrderedSetEntry newEntry = new OrderedSetEntry(memberToAdd, scoreToAdd); - OrderedSetEntry orderedSetEntry = members.put(memberToAdd, newEntry); - if (orderedSetEntry == null) { + OrderedSetEntry existingEntry = members.get(memberToAdd); + if (existingEntry == null) { + OrderedSetEntry newEntry = new OrderedSetEntry(memberToAdd, scoreToAdd); + members.put(memberToAdd, newEntry); scoreSet.add(newEntry); - sizeInBytes += calculateSizeOfFieldValuePair(memberToAdd, scoreToAdd); + // Without this adjustment, we count the entry and member name array twice, since references + // to them appear in both backing collections. + sizeInBytesAdjustment += newEntry.getSizeInBytes() + calculateByteArraySize(memberToAdd); + return null; } else { - scoreSet.remove(orderedSetEntry); - scoreSet.add(newEntry); - oldScore = orderedSetEntry.getScoreBytes(); - sizeInBytes += scoreToAdd.length - oldScore.length; + scoreSet.remove(existingEntry); + byte[] oldScore = existingEntry.scoreBytes; + existingEntry.updateScore(stripTrailingZeroFromDouble(scoreToAdd)); + members.put(memberToAdd, existingEntry); Review comment: why is this members.put needed when we modified an existing entry? Is it simply to update the size if the byte array form has a different length? We are using extra memory by storing both the byte array form and the double form. We would use less memory if we just stored it as a "double". In this case storing it in both forms uses more CPU since we need to do the extra put. What operations are being optimized by storing it in both forms? ########## File path: geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/collections/SizeableObject2ObjectOpenCustomHashMapWithCursor.java ########## @@ -226,6 +226,14 @@ int calculateBackingArraysOverhead() { + BACKING_ARRAY_LENGTH_COEFFICIENT * getTotalBackingArrayLength(); } + private <E> int getElementSize(E element) { + if (element instanceof Sizeable) { Review comment: could this class also use your new SizeableObjectSizer? -- 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]
