Skip to content

Commit 0457dea

Browse files
johnmayegonw
authored andcommitted
Remove Guava caches, one is no longer needed - we have matchRoot now. The second case is technically incorrect as the contents of the IAtomContainer may change so it's not safe to cache here. There is a bug with AtomContainer2.add which will be fixed in another commit - for now we can use the copy constructor.
1 parent 2b19524 commit 0457dea

File tree

3 files changed

+10
-55
lines changed

3 files changed

+10
-55
lines changed

base/isomorphism/src/main/java/org/openscience/cdk/isomorphism/DfState.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,7 @@ private StackFrame() {}
109109
"has a IChemObjectBuilder set!");
110110
}
111111

112-
IAtomContainer tmp = builder.newAtomContainer();
113-
tmp.add(query);
112+
IAtomContainer tmp = builder.newInstance(IAtomContainer.class, query);
114113
this.qbonds = new IQueryBond[tmp.getBondCount()];
115114
this.amap = new int[query.getAtomCount()];
116115

base/isomorphism/src/main/java/org/openscience/cdk/isomorphism/matchers/Expr.java

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@
2323

2424
package org.openscience.cdk.isomorphism.matchers;
2525

26-
import com.google.common.cache.CacheBuilder;
27-
import com.google.common.cache.CacheLoader;
28-
import com.google.common.cache.LoadingCache;
29-
import com.google.common.cache.Weigher;
3026
import org.openscience.cdk.CDKConstants;
3127
import org.openscience.cdk.ReactionRole;
3228
import org.openscience.cdk.config.Elements;
@@ -41,6 +37,7 @@
4137
import java.util.ArrayDeque;
4238
import java.util.Arrays;
4339
import java.util.Deque;
40+
import java.util.Map;
4441
import java.util.Objects;
4542

4643
/**
@@ -704,9 +701,6 @@ public IAtomContainer subquery() {
704701
return query;
705702
}
706703

707-
/* Property Caches */
708-
private static LoadingCache<IAtomContainer, int[]> cacheRCounts;
709-
710704
private static int[] getRingCounts(IAtomContainer mol) {
711705
int[] rcounts = new int[mol.getAtomCount()];
712706
for (int[] path : Cycles.mcb(mol).paths()) {
@@ -719,24 +713,10 @@ private static int[] getRingCounts(IAtomContainer mol) {
719713

720714
private static int getRingCount(IAtom atom) {
721715
final IAtomContainer mol = atom.getContainer();
722-
if (cacheRCounts == null) {
723-
cacheRCounts = CacheBuilder.newBuilder()
724-
.maximumWeight(1000) // 4KB
725-
.weigher(new Weigher<IAtomContainer, int[]>() {
726-
@Override
727-
public int weigh(IAtomContainer key,
728-
int[] value) {
729-
return value.length;
730-
}
731-
})
732-
.build(new CacheLoader<IAtomContainer, int[]>() {
733-
@Override
734-
public int[] load(IAtomContainer key) throws Exception {
735-
return getRingCounts(key);
736-
}
737-
});
738-
}
739-
return cacheRCounts.getUnchecked(mol)[atom.getIndex()];
716+
// we used to have a cache here but this would be incorrect if
717+
// an IAtomContainer was reused - better would be if we did the
718+
// matching in the pattern
719+
return getRingCounts(mol)[atom.getIndex()];
740720
}
741721

742722
/**

legacy/src/main/java/org/openscience/cdk/isomorphism/matchers/smarts/RecursiveSmartsAtom.java

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,11 @@
1818
*/
1919
package org.openscience.cdk.isomorphism.matchers.smarts;
2020

21-
import com.google.common.cache.CacheBuilder;
22-
import com.google.common.cache.CacheLoader;
23-
import com.google.common.cache.LoadingCache;
2421
import org.openscience.cdk.interfaces.IAtom;
25-
import org.openscience.cdk.interfaces.IAtomContainer;
26-
import org.openscience.cdk.isomorphism.Pattern;
22+
import org.openscience.cdk.isomorphism.DfPattern;
2723
import org.openscience.cdk.isomorphism.matchers.IQueryAtom;
2824
import org.openscience.cdk.isomorphism.matchers.IQueryAtomContainer;
2925

30-
import java.util.BitSet;
31-
3226
/**
3327
* This matches recursive smarts atoms.
3428
*
@@ -42,8 +36,7 @@ public final class RecursiveSmartsAtom extends SMARTSAtom {
4236
/** The IQueryAtomContainer created by parsing the recursive smarts */
4337
private final IQueryAtomContainer query;
4438

45-
/** Query cache. */
46-
private final LoadingCache<IAtomContainer, BitSet> cache;
39+
private final DfPattern ptrn;
4740

4841
/**
4942
* Creates a new instance
@@ -53,19 +46,7 @@ public final class RecursiveSmartsAtom extends SMARTSAtom {
5346
public RecursiveSmartsAtom(final IQueryAtomContainer query) {
5447
super(query.getBuilder());
5548
this.query = query;
56-
this.cache = CacheBuilder.newBuilder().maximumSize(42).weakKeys()
57-
.build(new CacheLoader<IAtomContainer, BitSet>() {
58-
59-
@Override
60-
public BitSet load(IAtomContainer target) throws Exception {
61-
BitSet hits = new BitSet();
62-
for (int[] mapping : Pattern.findSubstructure(query)
63-
.matchAll(target)) {
64-
hits.set(mapping[0]);
65-
}
66-
return hits;
67-
}
68-
});
49+
this.ptrn = DfPattern.findSubstructure(query);
6950
}
7051

7152
/*
@@ -76,13 +57,8 @@ public BitSet load(IAtomContainer target) throws Exception {
7657
*/
7758
@Override
7859
public boolean matches(IAtom atom) {
79-
8060
if (!((IQueryAtom) query.getAtom(0)).matches(atom)) return false;
81-
8261
if (query.getAtomCount() == 1) return true;
83-
84-
IAtomContainer target = invariants(atom).target();
85-
86-
return cache.getUnchecked(target).get(target.indexOf(atom));
62+
return ptrn.matchesRoot(atom);
8763
}
8864
}

0 commit comments

Comments
 (0)