seregamorph commented on code in PR #1937: URL: https://github.com/apache/maven-resolver/pull/1937#discussion_r3486301808
########## maven-resolver-util/src/main/java/org/eclipse/aether/util/concurrency/ConcurrentWeakCache.java: ########## @@ -0,0 +1,250 @@ +/* + * 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.eclipse.aether.util.concurrency; + +import java.lang.ref.ReferenceQueue; +import java.lang.ref.WeakReference; +import java.util.concurrent.ConcurrentHashMap; + +/** + * A concurrent cache with weak keys and weak values, inspired by maven-impl's Cache class + * but stripped down and optimized for hot-path performance. + * <p> + * Design compared to the full Cache: + * <ul> + * <li><b>Zero allocation on get()</b> — uses a ThreadLocal reusable lookup key + * instead of allocating a new wrapper on every read</li> + * <li><b>O(1) cleanup per stale entry</b> — uses identity-based removal from + * the ReferenceQueue instead of a full {@code entrySet().removeIf()} scan</li> + * <li><b>Lock-free reads</b> — {@link ConcurrentHashMap#get} is a volatile read + * with no lock acquisition</li> + * <li><b>Lock-striped writes</b> — ConcurrentHashMap uses fine-grained locking</li> + * </ul> + * <p> + * Keys are held via {@link WeakReference}: when a key is no longer strongly referenced + * elsewhere, its entry becomes eligible for garbage collection. Values are also held + * via WeakReference. Stale entries are cleaned up lazily on {@link #put}. + * + * @param <K> the type of keys + * @param <V> the type of values + * @since 2.0.19 Review Comment: nit: this seems to be not correct, shouldn't it be 2.0.20? -- 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]
