eric-maynard commented on code in PR #490: URL: https://github.com/apache/polaris/pull/490#discussion_r2006857848
########## polaris-core/src/main/java/org/apache/polaris/core/persistence/cache/EntityWeigher.java: ########## @@ -0,0 +1,64 @@ +/* + * 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.polaris.core.persistence.cache; + +import com.github.benmanes.caffeine.cache.Weigher; +import org.checkerframework.checker.index.qual.NonNegative; + +/** + * A {@link Weigher} implementation that weighs {@link EntityCacheEntry} objects by the approximate + * size of the entity object. + */ +public class EntityWeigher implements Weigher<Long, EntityCacheEntry> { + + /** The amount of weight that is expected to roughly equate to 1MB of memory usage */ + public static final long WEIGHT_PER_MB = 1024 * 1024; + + /* Represents the approximate size of an entity beyond the properties */ + private static final int APPROXIMATE_ENTITY_OVERHEAD = 1000; + + /** Singleton instance */ + private static final EntityWeigher instance = new EntityWeigher(); + + private EntityWeigher() {} + + /** Gets the singleton {@link EntityWeigher} */ + public static EntityWeigher getInstance() { + return instance; + } + + /** + * Computes the weight of a given entity + * + * @param key The entity's key; not used + * @param value The entity to be cached + * @return The weight of the entity + */ + @Override + public @NonNegative int weigh(Long key, EntityCacheEntry value) { + return APPROXIMATE_ENTITY_OVERHEAD + + value.getEntity().getProperties().length() + + value.getEntity().getInternalProperties().length(); Review Comment: Yeah absolutely, I'm just hoping to clarify what exactly you're looking for since I think it's easy enough to collect more data. I also don't really have an opinion about the multiplier or weight, so I'm pretty happy to take whatever values you feel the data supports. To that end, can you clarify the exact visualization or comparison you're looking for? In case you are indeed looking for this... > Or instead are you talking about not the weight when the cache stabilizes, but as entries are first added to it? I rendered another graph here using the latest data (post-GC heap size): <img width="500" alt="Screenshot 2025-03-20 at 10 00 51 PM" src="https://github.com/user-attachments/assets/ae5243fa-ce17-46f4-8611-fc0917b2e0b7" /> This is for multiplier 3, and you can see that heap size does indeed roughly correlate with total weight to a point. For a multiplier of 2, it tracks more closely: <img width="500" alt="Screenshot 2025-03-20 at 10 04 08 PM" src="https://github.com/user-attachments/assets/eb031bd7-53b5-4926-a153-8f663e14cd43" /> But this is bound to change if you look at non-ASCII properties. -- 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: issues-unsubscr...@polaris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org