daniellansun commented on code in PR #3260: URL: https://github.com/apache/groovy/pull/3260#discussion_r4531493441
########## src/main/java/org/apache/groovy/vmplugin/internal/DefaultImportPackages.java: ########## @@ -0,0 +1,127 @@ +/* + * 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.groovy.vmplugin.internal; + +import groovy.transform.Internal; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * Orders default-import package prefixes by a caller-supplied precedence + * array. Iteration order of the returned sets is that array's order. + * Packages that are not in the array are dropped, so a {@code HashSet} + * walk cannot put a later package ahead of an earlier one. + * + * @since 7.0.0 + */ +@Internal +public final class DefaultImportPackages { + + private DefaultImportPackages() { + } + + /** + * Returns the members of {@code packages} that occur in {@code precedence}, + * iterated in {@code precedence} order. The result is unmodifiable. + * An empty result is {@link Collections#emptySet()}. A single match is + * {@link Collections#singleton(Object)}. + * + * @param packages packages discovered for one simple name; {@code null} is empty + * @param precedence package prefixes in the order they must be tried; {@code null} yields an empty set + * @return an immutable set in precedence order + */ + public static Set<String> ordered(final Collection<String> packages, final String[] precedence) { + if (packages == null || packages.isEmpty() || precedence == null || precedence.length == 0) { + return Collections.emptySet(); + } + List<String> matches = new ArrayList<>(Math.min(packages.size(), precedence.length)); + for (String candidate : precedence) { + if (packages.contains(candidate)) { + matches.add(candidate); + } + } + if (matches.isEmpty()) { + return Collections.emptySet(); + } + if (matches.size() == 1) { + return Collections.singleton(matches.get(0)); + } + return Collections.unmodifiableSet(new LinkedHashSet<>(matches)); + } + + /** + * Returns {@code existing} when it already contains {@code packagePrefix}. + * Otherwise returns the precedence-ordered union. {@code packagePrefix} + * values absent from {@code precedence} are not added. + * + * @param existing the packages already recorded, or {@code null} + * @param packagePrefix the package to add + * @param precedence package prefixes in try-order + * @return an immutable set in precedence order, or {@code existing} when unchanged + */ + public static Set<String> including(final Set<String> existing, final String packagePrefix, final String[] precedence) { + if (existing != null && existing.contains(packagePrefix)) { + return existing; + } + Set<String> combined = new HashSet<>(existing == null ? 2 : existing.size() + 1); Review Comment: The `new HashSet(2)` line is the previous code. On the current tip the constructor is `new HashSet<>(existing == null ? 1 : existing.size() + 1)`, so a null `existing` really does allocate capacity 1. I agree one versus two is not important here; I changed it only because you asked why 2 was chosen, and 1 matches the single element that is added. ########## src/main/java/org/apache/groovy/vmplugin/internal/DefaultImportPackages.java: ########## @@ -0,0 +1,127 @@ +/* + * 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.groovy.vmplugin.internal; + +import groovy.transform.Internal; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * Orders default-import package prefixes by a caller-supplied precedence + * array. Iteration order of the returned sets is that array's order. + * Packages that are not in the array are dropped, so a {@code HashSet} + * walk cannot put a later package ahead of an earlier one. + * + * @since 7.0.0 + */ +@Internal +public final class DefaultImportPackages { + + private DefaultImportPackages() { + } + + /** + * Returns the members of {@code packages} that occur in {@code precedence}, + * iterated in {@code precedence} order. The result is unmodifiable. + * An empty result is {@link Collections#emptySet()}. A single match is + * {@link Collections#singleton(Object)}. + * + * @param packages packages discovered for one simple name; {@code null} is empty + * @param precedence package prefixes in the order they must be tried; {@code null} yields an empty set + * @return an immutable set in precedence order + */ + public static Set<String> ordered(final Collection<String> packages, final String[] precedence) { + if (packages == null || packages.isEmpty() || precedence == null || precedence.length == 0) { + return Collections.emptySet(); + } + List<String> matches = new ArrayList<>(Math.min(packages.size(), precedence.length)); + for (String candidate : precedence) { + if (packages.contains(candidate)) { + matches.add(candidate); + } + } + if (matches.isEmpty()) { + return Collections.emptySet(); + } + if (matches.size() == 1) { + return Collections.singleton(matches.get(0)); + } + return Collections.unmodifiableSet(new LinkedHashSet<>(matches)); Review Comment: You are right on both counts. `DefaultImportPackagesTest` (and the `add` throws checks in `Groovy12725`) are API / regression tests: they lock the iteration order and the fact that a caller cannot `add` to a returned set. They do not establish concurrent behaviour, and I should not have pointed at them as if they did. The concurrency design is not the wrapper itself. It is that an update replaces the `ConcurrentHashMap` entry instead of mutating the set a reader may already be iterating. `compute` in `ResolveVisitor.resolveOneDefaultImport` calls `including()`, which returns either the same instance or a new set; it never adds to a set that has already been published. The map provides the safe publication of that reference. `unmodifiableSet` does not invent that race freedom, and it is also not the same as an immutable set: if the backing `LinkedHashSet` were still written after the wrap, the view would change and you could have a publication problem. What the wrapper is for: the same set instance is shared. Until a later hit replaces a cache entry, the cache holds the startup index sets (`assertSame` in `Groovy12725`). A caller who could mutate a cache value would also mutate that snapshot. The wrapper is a defensive contract against that, and the `add` tests are there to catch a later change that starts returning a mutable set or updating in place. They are not a thread-safety proof. I used `Collections.unmodifiableSet` rather than `Set.copyOf` / `Set.of` because those do not keep the `LinkedHashSet` insertion order, which is the precedence order. The mutable set is a local in `ordered()`; it is not retained or written after the wrap, so the published view does not change. Empty and single-element results are already `Collections.emptySet()` and `Collections.singleton`. -- 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]
