gortiz commented on code in PR #12223: URL: https://github.com/apache/pinot/pull/12223#discussion_r1463005976
########## pinot-spi/src/main/java/org/apache/pinot/spi/utils/FALFInterner.java: ########## @@ -0,0 +1,148 @@ +/** + * 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.pinot.spi.utils; + + +import com.google.common.collect.Interner; +import java.util.Objects; +import java.util.function.BiPredicate; +import java.util.function.ToIntFunction; + +/** + * Fixed-size Array-based, Lock-Free Interner. + * + * !!!!!!!!!!!!!!! READ THE PARAGRAPH BELOW BEFORE USING THIS CLASS !!!!!!!!!!!!!!!! + * This class is technically not thread-safe. Therefore if it's called from multiple + * threads, it should either be used with proper synchronization (in the same way as + * you would use e.g. a HashMap), or under the following conditions: + * all the objects being interned are not just immutable, but also final (that is, all + * their fields used in equals() and hashCode() methods are explicitly marked final). + * That's to ensure that all threads always see the same contents of these objects. If + * this rule is not followed, using this class from multiple threads may lead to strange + * non-deterministic errors. Note that objects with all private fields that are not + * marked final, or immutable collections created via Collection.unmodifiableMap() etc, + * don't qualify. + * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + * + * This interner is intended to be used when either: + * (a) distribution of values among the objects to be interned make them not suitable + * for standard interners + * (b) speed is more important than ultimate memory savings + * + * Problem (a) occurs when both the total number of objects AND the number of unique + * values is large. For example, there are 1M strings that look like "a", "a", "b", "b", + * "c", "c", ... - that is, for each unique value there are only two separate objects. + * Another problematic case is when "a" has 1000 copies, "b" has 900 copies, etc., + * but in the last few hundred thousand objects each one is unique. In both cases, if + * we use a standard interner such as a Guava interner or a ConcurrentHashMap to + * deduplicate such objects, the amount of memory consumed by the interner itself to Review Comment: This data structure is very simple: ```java private final Object[] _cache; private final int _cacheLengthMinusOne; private final BiPredicate<T, T> _equalsFunction; private final ToIntFunction<T> _hashFunction; ``` Assuming `_equalsFunction` and `_hashFunction` are tiny, most space is occuppied by `_cache`, which is initiated as `_cache = new Object[tableSizeFor(expectedCapacity)]`, where `expectedCapacity` is defined by the user. Then each value on `_cache` occupies space, but that space was already spend by dictionaries right now, so in case we have `S` segments with `A` unique values that are repeated in all `S` segments, a perfect hash and a cache size `C` close to `A`: - The footprint without this optimization would be `S` * `A` + not modified baseline (hash maps on each dictionary, etc). - With this optimization would be `C` + the same not modified baseline This happens because most A strings stored in the dictionary will be pointing to the strings stored in this cache instead of being created as new instances. -- 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: commits-unsubscr...@pinot.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org For additional commands, e-mail: commits-h...@pinot.apache.org