Skip to content
This repository has been archived by the owner on Jan 27, 2025. It is now read-only.

Commit

Permalink
Avoid unnecessary (and expensive) work when matching path wildcards. (#…
Browse files Browse the repository at this point in the history
…207, #97)

`SimpleRegexTrie` is only used to determine whether a field name matches a pattern. The result of this computation only depends on these two values, not on the underlying data. So we can cache and reuse the result across the whole transformation process.

It might be possible to implement the matching in a more optimized way for this particular use case, but it's delegated to `SimpleRegexTrie` for compatibility and maintainability purposes.
  • Loading branch information
blackwinter committed Apr 11, 2022
1 parent 1ecbcdf commit 90cbde4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
14 changes: 10 additions & 4 deletions metafix/src/main/java/org/metafacture/metafix/Value.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.ConcurrentModificationException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
Expand Down Expand Up @@ -459,9 +460,10 @@ public void remove(final int index) {
*/
public static class Hash extends AbstractValueType {

private final Map<String, Value> map = new LinkedHashMap<>();
private static final Map<String, Map<String, Boolean>> TRIE_CACHE = new HashMap<>();
private static final SimpleRegexTrie<String> TRIE = new SimpleRegexTrie<>();

private final SimpleRegexTrie<String> trie = new SimpleRegexTrie<>();
private final Map<String, Value> map = new LinkedHashMap<>();

/**
* Creates an empty instance of {@link Hash}.
Expand Down Expand Up @@ -695,8 +697,12 @@ private Stream<String> findFields(final String pattern) {
}

private <T> T matchFields(final String pattern, final BiFunction<Stream<String>, Predicate<String>, T> function) {
trie.put(pattern, pattern);
return function.apply(map.keySet().stream(), f -> trie.get(f).contains(pattern));
final Map<String, Boolean> matcher = TRIE_CACHE.computeIfAbsent(pattern, k -> {
TRIE.put(k, k);
return new HashMap<>();
});

return function.apply(map.keySet().stream(), f -> matcher.computeIfAbsent(f, k -> TRIE.get(k).contains(pattern)));
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package org.metafacture.metafix;

import org.metafacture.commons.tries.SimpleRegexTrie;

import nl.jqno.equalsverifier.EqualsVerifier;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -46,8 +44,6 @@ public HashValueTest() {
public void shouldSatisfyEqualsContract() {
EqualsVerifier.forClass(Value.Hash.class)
.withPrefabValues(Value.class, Value.newArray(), Value.newHash())
.withPrefabValues(SimpleRegexTrie.class, new SimpleRegexTrie<String>(), new SimpleRegexTrie<String>())
.withIgnoredFields("trie")
.verify();
}

Expand Down

0 comments on commit 90cbde4

Please sign in to comment.