Copilot commented on code in PR #17: URL: https://github.com/apache/sling-org-apache-sling-rewriter/pull/17#discussion_r4413814554
########## src/main/java/org/apache/sling/rewriter/impl/FactoryMap.java: ########## @@ -0,0 +1,62 @@ +/* + * 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.sling.rewriter.impl; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.osgi.framework.ServiceReference; + +/** + * Stores the highest-ranked factory for each pipeline type. + */ +class FactoryMap<T> { + + protected final Map<ServiceReference<T>, T> services = new HashMap<>(); + + private volatile Map<String, T> factories = Collections.emptyMap(); + + public T getFactory(final String type) { + return this.factories.get(type); + } + + public synchronized void bind(final ServiceReference<T> reference, final T factory) { + this.services.put(reference, factory); + this.updateFactories(); + } + + public synchronized void unbind(final ServiceReference<T> reference) { + this.services.remove(reference); + this.updateFactories(); + } + + private void updateFactories() { + final List<ServiceReference<T>> references = new ArrayList<>(this.services.keySet()); + references.sort(Collections.reverseOrder()); + final Map<String, T> updated = new HashMap<>(); + for (final ServiceReference<T> reference : references) { + final String type = (String) reference.getProperty(FactoryCache.PROPERTY_TYPE); + if (type != null) { + updated.putIfAbsent(type, this.services.get(reference)); + } + } + this.factories = updated; + } Review Comment: `updateFactories()` rebuilds the full type->factory map and sorts *all* tracked references on every bind/unbind/update. If many factories exist or service properties churn, this can become a hotspot. Consider maintaining per-type ranked sets (or per-type best reference) so updates are O(log n) per affected type rather than O(n log n) over all services each time. ########## src/main/java/org/apache/sling/rewriter/impl/TransformerFactoryCache.java: ########## @@ -0,0 +1,98 @@ +/* + * 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.sling.rewriter.impl; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import org.apache.sling.rewriter.ProcessingContext; +import org.apache.sling.rewriter.TransformerFactory; +import org.apache.sling.rewriter.impl.FactoryCache.TransformerFactoryEntry; +import org.osgi.framework.ServiceReference; + +final class TransformerFactoryCache extends FactoryMap<TransformerFactory> { + + static final TransformerFactoryEntry[] EMPTY_ENTRY_ARRAY = new TransformerFactoryEntry[0]; + static final TransformerFactoryEntry[][] EMPTY_DOUBLE_ENTRY_ARRAY = {EMPTY_ENTRY_ARRAY, EMPTY_ENTRY_ARRAY}; + static final TransformerFactory[] EMPTY_FACTORY_ARRAY = new TransformerFactory[0]; + static final TransformerFactory[][] EMPTY_DOUBLE_FACTORY_ARRAY = {EMPTY_FACTORY_ARRAY, EMPTY_FACTORY_ARRAY}; + + private TransformerFactoryEntry[][] cached; + + @Override + public synchronized void bind(final ServiceReference<TransformerFactory> reference, final TransformerFactory factory) { + super.bind(reference, factory); + this.cached = null; + } + + @Override + public synchronized void unbind(final ServiceReference<TransformerFactory> reference) { + super.unbind(reference); + this.cached = null; + } + + /** + * Get global factories in ascending service ranking order, split into pre and post transformers. + */ + public synchronized TransformerFactoryEntry[][] getGlobalTransformerFactoryEntries() { + if (this.cached == null) { + final List<ServiceReference<TransformerFactory>> references = new ArrayList<>(this.services.keySet()); + Collections.sort(references); + final List<TransformerFactoryEntry> pre = new ArrayList<>(); + final List<TransformerFactoryEntry> post = new ArrayList<>(); + for (final ServiceReference<TransformerFactory> reference : references) { + if (FactoryCache.MODE_GLOBAL.equalsIgnoreCase((String) reference.getProperty(FactoryCache.PROPERTY_MODE))) { + final TransformerFactoryEntry entry = new TransformerFactoryEntry(this.services.get(reference), reference); + if (entry.order < 0) { + pre.add(entry); + } else { + post.add(entry); + } + } + } + if (pre.isEmpty() && post.isEmpty()) { + this.cached = EMPTY_DOUBLE_ENTRY_ARRAY; + } else { + this.cached = new TransformerFactoryEntry[][] { + pre.toArray(EMPTY_ENTRY_ARRAY), post.toArray(EMPTY_ENTRY_ARRAY) + }; + } + } + return this.cached; + } Review Comment: The previous implementation explicitly sorted the resulting `TransformerFactoryEntry[]` arrays; this version relies solely on `ServiceReference` ordering and insertion order in `pre`/`post`. If `TransformerFactoryEntry` ordering differs from `ServiceReference` ordering (or changes in the future), this can alter global transformer execution order. To preserve prior behavior deterministically, sort `pre` and `post` (or the resulting arrays) using `TransformerFactoryEntry`’s natural ordering before caching. -- 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]
