From 50511efc0e3cdeaee95ee7254483b1ea9c1f9fea Mon Sep 17 00:00:00 2001 From: Stian Soiland-Reyes Date: Thu, 19 Mar 2015 02:52:08 +0000 Subject: [PATCH 1/8] JENA-901 A simple BoundedMap - based on LinkedHashMap --- .../java/com/hp/hpl/jena/util/BoundedMap.java | 58 +++++++++++++++++ .../com/hp/hpl/jena/util/TestBoundedMap.java | 62 +++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 jena-core/src/main/java/com/hp/hpl/jena/util/BoundedMap.java create mode 100644 jena-core/src/test/java/com/hp/hpl/jena/util/TestBoundedMap.java diff --git a/jena-core/src/main/java/com/hp/hpl/jena/util/BoundedMap.java b/jena-core/src/main/java/com/hp/hpl/jena/util/BoundedMap.java new file mode 100644 index 00000000000..8c1f6de0ffe --- /dev/null +++ b/jena-core/src/main/java/com/hp/hpl/jena/util/BoundedMap.java @@ -0,0 +1,58 @@ +/* + * 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 com.hp.hpl.jena.util; + +import java.util.LinkedHashMap; +import java.util.Map; + +/** + * Bounded Map with a maximum size. + *

+ * On insertion of entries beyond the maximum size, the eldest accessed entry is + * removed. + * + * @param + * Type of keys + * @param + * Type of values + */ +public class BoundedMap extends LinkedHashMap implements Map { + + private static final long serialVersionUID = -1424511852972661771L; + private int maxEntries; + + /** + * Construct a BoundedMap + * + * @param maxEntries Maximum number of entries + */ + public BoundedMap(int maxEntries) { + super(Math.max(maxEntries/16, 16), 0.75f, true); + if (maxEntries <= 0) { + throw new IllegalArgumentException("maxEntries <= 0"); + } + this.maxEntries = maxEntries; + } + + @Override + protected boolean removeEldestEntry(java.util.Map.Entry eldest) { + return size() > maxEntries; + } +} diff --git a/jena-core/src/test/java/com/hp/hpl/jena/util/TestBoundedMap.java b/jena-core/src/test/java/com/hp/hpl/jena/util/TestBoundedMap.java new file mode 100644 index 00000000000..5248e33485b --- /dev/null +++ b/jena-core/src/test/java/com/hp/hpl/jena/util/TestBoundedMap.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 com.hp.hpl.jena.util; + +import junit.framework.TestCase; +import junit.framework.TestSuite; + +import org.junit.Test; + +public class TestBoundedMap extends TestCase { + public static TestSuite suite() { + return new TestSuite(TestBoundedMap.class, "TestBoundedMap"); + } + + @Test + public void testBoundedMap() throws Exception { + BoundedMap map = new BoundedMap(4); + map.put(1, 1); + map.put(2, 2); + map.put(3, 3); + map.put(4, 4); + assertEquals(4, map.size()); + map.put(5, 5); + assertEquals(4, map.size()); + // 1 was the oldest + assertFalse(map.containsKey(1)); + for (int i=2; i<=5; i++) { + assertTrue(map.containsKey(i)); + } + map.get(2); + map.put(6, 6); + assertEquals(4, map.size()); + // 2 should not have been removed as we just .get it + assertTrue(map.containsKey(2)); + // while 3 would now have been the oldest + assertFalse(map.containsKey(3)); + assertEquals(4, map.size()); + map.remove(2); + map.put(7, 7); + assertEquals(4, map.size()); + + + } + +} From 06abde7d170bdc76ac3f9a7ef4159bb625028cfe Mon Sep 17 00:00:00 2001 From: Stian Soiland-Reyes Date: Thu, 19 Mar 2015 02:52:30 +0000 Subject: [PATCH 2/8] JENA-901 Use BoundedMap for tabledGoals cache configurable with system property jena.rulesys.lp.max_cached_tabled_goals --- .../reasoner/rulesys/impl/LPBRuleEngine.java | 38 ++++++++++++++----- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/jena-core/src/main/java/com/hp/hpl/jena/reasoner/rulesys/impl/LPBRuleEngine.java b/jena-core/src/main/java/com/hp/hpl/jena/reasoner/rulesys/impl/LPBRuleEngine.java index 8553f1dcccf..84556678bcd 100644 --- a/jena-core/src/main/java/com/hp/hpl/jena/reasoner/rulesys/impl/LPBRuleEngine.java +++ b/jena-core/src/main/java/com/hp/hpl/jena/reasoner/rulesys/impl/LPBRuleEngine.java @@ -18,14 +18,26 @@ package com.hp.hpl.jena.reasoner.rulesys.impl; -import com.hp.hpl.jena.graph.*; -import com.hp.hpl.jena.reasoner.*; -import com.hp.hpl.jena.reasoner.rulesys.*; -import com.hp.hpl.jena.util.iterator.*; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.*; + +import com.hp.hpl.jena.graph.Node; +import com.hp.hpl.jena.graph.Triple; +import com.hp.hpl.jena.reasoner.ReasonerException; +import com.hp.hpl.jena.reasoner.TriplePattern; +import com.hp.hpl.jena.reasoner.rulesys.BackwardRuleInfGraphI; +import com.hp.hpl.jena.reasoner.rulesys.Rule; +import com.hp.hpl.jena.util.BoundedMap; +import com.hp.hpl.jena.util.iterator.ExtendedIterator; +import com.hp.hpl.jena.util.iterator.WrappedIterator; /** * LP version of the core backward chaining engine. For each parent inference @@ -53,10 +65,14 @@ public class LPBRuleEngine { /** List of engine instances which are still processing queries */ protected List activeInterpreters = new ArrayList<>(); - + + protected final int MAX_CACHED_TABLED_GOALS = + Integer.getInteger("jena.rulesys.lp.max_cached_tabled_goals", 512*1024); + /** Table mapping tabled goals to generators for those goals. - * This is here so that partial goal state can be shared across multiple queries. */ - protected HashMap tabledGoals = new HashMap<>(); + * This is here so that partial goal state can be shared across multiple queries. + */ + protected Map tabledGoals = new BoundedMap<>(MAX_CACHED_TABLED_GOALS); /** Set of generators waiting to be run */ protected LinkedList agenda = new LinkedList<>(); @@ -113,7 +129,7 @@ public synchronized ExtendedIterator find(TriplePattern goal) { */ public synchronized void reset() { checkSafeToUpdate(); - tabledGoals = new HashMap<>(); + tabledGoals.clear(); agenda.clear(); } @@ -284,6 +300,10 @@ public synchronized Generator generatorFor(TriplePattern goal) { return generator; } + int cachedTabledGoals() { + return tabledGoals.size(); + } + /** * Register that a generator or specific generator state (Consumer choice point) * is now ready to run. From 0b2e72b7d604940c5b084f026c437484856e929a Mon Sep 17 00:00:00 2001 From: Stian Soiland-Reyes Date: Thu, 19 Mar 2015 02:55:06 +0000 Subject: [PATCH 3/8] JENA-901 Test saturation of tabledGoals --- .../rulesys/impl/TestLPBRuleEngine.java | 94 +++++++++++++++++++ .../reasoner/rulesys/test/TestPackage.java | 4 + .../com/hp/hpl/jena/util/TestPackage.java | 3 +- 3 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 jena-core/src/test/java/com/hp/hpl/jena/reasoner/rulesys/impl/TestLPBRuleEngine.java diff --git a/jena-core/src/test/java/com/hp/hpl/jena/reasoner/rulesys/impl/TestLPBRuleEngine.java b/jena-core/src/test/java/com/hp/hpl/jena/reasoner/rulesys/impl/TestLPBRuleEngine.java new file mode 100644 index 00000000000..320463bdd9a --- /dev/null +++ b/jena-core/src/test/java/com/hp/hpl/jena/reasoner/rulesys/impl/TestLPBRuleEngine.java @@ -0,0 +1,94 @@ +/* + * 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 com.hp.hpl.jena.reasoner.rulesys.impl; + +import java.lang.reflect.Field; +import java.util.List; + +import junit.framework.TestCase; +import junit.framework.TestSuite; + +import org.junit.Test; + +import com.hp.hpl.jena.graph.Factory; +import com.hp.hpl.jena.graph.Graph; +import com.hp.hpl.jena.graph.Node; +import com.hp.hpl.jena.graph.NodeFactory; +import com.hp.hpl.jena.graph.Triple; +import com.hp.hpl.jena.reasoner.rulesys.FBRuleInfGraph; +import com.hp.hpl.jena.reasoner.rulesys.FBRuleReasoner; +import com.hp.hpl.jena.reasoner.rulesys.Rule; +import com.hp.hpl.jena.vocabulary.RDF; +import com.hp.hpl.jena.vocabulary.RDFS; + +public class TestLPBRuleEngine extends TestCase { + public static TestSuite suite() { + return new TestSuite( TestLPBRuleEngine.class, "TestLPBRuleEngine" ); + } + + protected Node a = NodeFactory.createURI("a"); + protected Node p = NodeFactory.createURI("p"); + protected Node C1 = NodeFactory.createURI("C1"); + protected Node C2 = NodeFactory.createURI("C2"); + protected Node ty = RDF.Nodes.type; + + public FBRuleReasoner createReasoner(List rules) { + FBRuleReasoner reasoner = new FBRuleReasoner(rules); + reasoner.tablePredicate(RDFS.Nodes.subClassOf); + reasoner.tablePredicate(RDF.Nodes.type); + reasoner.tablePredicate(p); + return reasoner; + } + + @Test + public void testSaturateTabledGoals() throws Exception { + // Set the cache size very small just for this test + System.setProperty("jena.rulesys.lp.max_cached_tabled_goals", "100"); + try { + Graph data = Factory.createGraphMem(); + data.add(new Triple(a, ty, C1)); + List rules = Rule.parseRules( + "[r1: (?x p ?t) <- (?x rdf:type C1), makeInstance(?x, p, C2, ?t)]" + + "[r2: (?t rdf:type C2) <- (?x rdf:type C1), makeInstance(?x, p, C2, ?t)]"); + + FBRuleInfGraph infgraph = (FBRuleInfGraph) createReasoner(rules).bind(data); + + // JENA-901 + // Let's ask about lots of unknown subjects + for (int i=0; i<256; i++) { + Node test = NodeFactory.createURI("test" + i); + infgraph.find(test, ty, C2).close(); + } + + // Let's see how many were cached + Field bEngine = FBRuleInfGraph.class.getDeclaredField("bEngine"); + bEngine.setAccessible(true); + LPBRuleEngine engine = (LPBRuleEngine) bEngine.get(infgraph); + assertEquals(100, engine.tabledGoals.size()); + } finally { + System.clearProperty("jena.rulesys.lp.max_cached_tabled_goals"); + + } + + } + + + +} diff --git a/jena-core/src/test/java/com/hp/hpl/jena/reasoner/rulesys/test/TestPackage.java b/jena-core/src/test/java/com/hp/hpl/jena/reasoner/rulesys/test/TestPackage.java index 5334dc251d7..661a4906f7c 100755 --- a/jena-core/src/test/java/com/hp/hpl/jena/reasoner/rulesys/test/TestPackage.java +++ b/jena-core/src/test/java/com/hp/hpl/jena/reasoner/rulesys/test/TestPackage.java @@ -20,9 +20,12 @@ import junit.framework.TestSuite ; + import org.slf4j.Logger ; import org.slf4j.LoggerFactory ; +import com.hp.hpl.jena.reasoner.rulesys.impl.TestLPBRuleEngine; + /** * Aggregate tester that runs all the test associated with the rulesys package. */ @@ -45,6 +48,7 @@ private TestPackage() { addTest( "TestBackchainer", TestBackchainer.suite() ); addTest( "TestLPBasics", TestBasicLP.suite() ); addTest( "TestLPDerivation", TestLPDerivation.suite() ); + addTest( TestLPBRuleEngine.suite() ); addTest( "TestFBRules", TestFBRules.suite() ); addTest( "TestGenericRules", TestGenericRules.suite() ); addTest( "TestRETE", TestRETE.suite() ); diff --git a/jena-core/src/test/java/com/hp/hpl/jena/util/TestPackage.java b/jena-core/src/test/java/com/hp/hpl/jena/util/TestPackage.java index 36ecbda1fe5..413d967a76c 100644 --- a/jena-core/src/test/java/com/hp/hpl/jena/util/TestPackage.java +++ b/jena-core/src/test/java/com/hp/hpl/jena/util/TestPackage.java @@ -35,7 +35,8 @@ static public TestSuite suite() { /** Creates new TestPackage */ private TestPackage() { super( "util" ); - addTest( "TestCache", TestCache.suite() ); + addTest( "TestBoundedMap", TestBoundedMap.suite() ); + addTest( TestCache.suite() ); addTest( "TestTokenzier", TestTokenizer.suite()); addTest( "TestFileUtils", TestFileUtils.suite() ); addTest( "TestHashUtils", TestCollectionFactory.suite() ); From 83d7e8cfdd50f3de849731bf56cadf1d1daec15c Mon Sep 17 00:00:00 2001 From: Stian Soiland-Reyes Date: Thu, 19 Mar 2015 03:53:02 +0000 Subject: [PATCH 4/8] measure size of engine --- jena-core/pom.xml | 7 +++++++ .../hpl/jena/reasoner/rulesys/impl/LPBRuleEngine.java | 3 +++ .../jena/reasoner/rulesys/impl/TestLPBRuleEngine.java | 11 ++++++++--- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/jena-core/pom.xml b/jena-core/pom.xml index c6fbc5d7019..4b4b65a42ba 100644 --- a/jena-core/pom.xml +++ b/jena-core/pom.xml @@ -45,6 +45,13 @@ test + + com.carrotsearch + java-sizeof + 0.0.4 + test + + org.slf4j slf4j-api diff --git a/jena-core/src/main/java/com/hp/hpl/jena/reasoner/rulesys/impl/LPBRuleEngine.java b/jena-core/src/main/java/com/hp/hpl/jena/reasoner/rulesys/impl/LPBRuleEngine.java index 84556678bcd..723a3d97093 100644 --- a/jena-core/src/main/java/com/hp/hpl/jena/reasoner/rulesys/impl/LPBRuleEngine.java +++ b/jena-core/src/main/java/com/hp/hpl/jena/reasoner/rulesys/impl/LPBRuleEngine.java @@ -36,6 +36,8 @@ import com.hp.hpl.jena.reasoner.rulesys.BackwardRuleInfGraphI; import com.hp.hpl.jena.reasoner.rulesys.Rule; import com.hp.hpl.jena.util.BoundedMap; +import com.hp.hpl.jena.util.cache.CacheControl; +import com.hp.hpl.jena.util.cache.CacheManager; import com.hp.hpl.jena.util.iterator.ExtendedIterator; import com.hp.hpl.jena.util.iterator.WrappedIterator; @@ -73,6 +75,7 @@ public class LPBRuleEngine { * This is here so that partial goal state can be shared across multiple queries. */ protected Map tabledGoals = new BoundedMap<>(MAX_CACHED_TABLED_GOALS); + //protected Map tabledGoals = new HashMap<>(); /** Set of generators waiting to be run */ protected LinkedList agenda = new LinkedList<>(); diff --git a/jena-core/src/test/java/com/hp/hpl/jena/reasoner/rulesys/impl/TestLPBRuleEngine.java b/jena-core/src/test/java/com/hp/hpl/jena/reasoner/rulesys/impl/TestLPBRuleEngine.java index 320463bdd9a..77840870ecb 100644 --- a/jena-core/src/test/java/com/hp/hpl/jena/reasoner/rulesys/impl/TestLPBRuleEngine.java +++ b/jena-core/src/test/java/com/hp/hpl/jena/reasoner/rulesys/impl/TestLPBRuleEngine.java @@ -27,11 +27,13 @@ import org.junit.Test; +import com.carrotsearch.sizeof.RamUsageEstimator; import com.hp.hpl.jena.graph.Factory; import com.hp.hpl.jena.graph.Graph; import com.hp.hpl.jena.graph.Node; import com.hp.hpl.jena.graph.NodeFactory; import com.hp.hpl.jena.graph.Triple; +import com.hp.hpl.jena.reasoner.TriplePattern; import com.hp.hpl.jena.reasoner.rulesys.FBRuleInfGraph; import com.hp.hpl.jena.reasoner.rulesys.FBRuleReasoner; import com.hp.hpl.jena.reasoner.rulesys.Rule; @@ -59,8 +61,9 @@ public FBRuleReasoner createReasoner(List rules) { @Test public void testSaturateTabledGoals() throws Exception { + final int MAX = 1; // Set the cache size very small just for this test - System.setProperty("jena.rulesys.lp.max_cached_tabled_goals", "100"); + System.setProperty("jena.rulesys.lp.max_cached_tabled_goals", ""+MAX); try { Graph data = Factory.createGraphMem(); data.add(new Triple(a, ty, C1)); @@ -72,7 +75,7 @@ public void testSaturateTabledGoals() throws Exception { // JENA-901 // Let's ask about lots of unknown subjects - for (int i=0; i<256; i++) { + for (int i=0; i Date: Thu, 19 Mar 2015 04:16:02 +0000 Subject: [PATCH 5/8] more reset.. still uses lots of memory --- .../rulesys/impl/TestLPBRuleEngine.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/jena-core/src/test/java/com/hp/hpl/jena/reasoner/rulesys/impl/TestLPBRuleEngine.java b/jena-core/src/test/java/com/hp/hpl/jena/reasoner/rulesys/impl/TestLPBRuleEngine.java index 77840870ecb..6b80866ceae 100644 --- a/jena-core/src/test/java/com/hp/hpl/jena/reasoner/rulesys/impl/TestLPBRuleEngine.java +++ b/jena-core/src/test/java/com/hp/hpl/jena/reasoner/rulesys/impl/TestLPBRuleEngine.java @@ -87,6 +87,24 @@ public void testSaturateTabledGoals() throws Exception { assertEquals(MAX, engine.tabledGoals.size()); System.gc(); System.out.println(RamUsageEstimator.sizeOf(engine)); + data.clear(); + System.out.println(RamUsageEstimator.sizeOf(engine)); + System.out.println(RamUsageEstimator.sizeOf(engine)); + for (int i=0; i Date: Fri, 20 Mar 2015 16:23:36 +0000 Subject: [PATCH 6/8] JENA-901 use a LinkedList instead of ArrayList for activeInterpreters. They could be closed in arbitrary order, and also this avoids a large ArrayList remaining after a big burst of concurrent queries. Simplify test. Check activeInterpreters is empty - this didn't happen as I had forgotten to call it.hasNext() (which any use of an iterator would normally do). Test now with MAX=1024 and 128M find() calls. --- .../reasoner/rulesys/impl/LPBRuleEngine.java | 2 +- .../rulesys/impl/TestLPBRuleEngine.java | 42 +++++++------------ 2 files changed, 15 insertions(+), 29 deletions(-) diff --git a/jena-core/src/main/java/com/hp/hpl/jena/reasoner/rulesys/impl/LPBRuleEngine.java b/jena-core/src/main/java/com/hp/hpl/jena/reasoner/rulesys/impl/LPBRuleEngine.java index 723a3d97093..bbac95db281 100644 --- a/jena-core/src/main/java/com/hp/hpl/jena/reasoner/rulesys/impl/LPBRuleEngine.java +++ b/jena-core/src/main/java/com/hp/hpl/jena/reasoner/rulesys/impl/LPBRuleEngine.java @@ -66,7 +66,7 @@ public class LPBRuleEngine { protected boolean recordDerivations; /** List of engine instances which are still processing queries */ - protected List activeInterpreters = new ArrayList<>(); + protected List activeInterpreters = new LinkedList<>(); protected final int MAX_CACHED_TABLED_GOALS = Integer.getInteger("jena.rulesys.lp.max_cached_tabled_goals", 512*1024); diff --git a/jena-core/src/test/java/com/hp/hpl/jena/reasoner/rulesys/impl/TestLPBRuleEngine.java b/jena-core/src/test/java/com/hp/hpl/jena/reasoner/rulesys/impl/TestLPBRuleEngine.java index 6b80866ceae..0ddb9c52998 100644 --- a/jena-core/src/test/java/com/hp/hpl/jena/reasoner/rulesys/impl/TestLPBRuleEngine.java +++ b/jena-core/src/test/java/com/hp/hpl/jena/reasoner/rulesys/impl/TestLPBRuleEngine.java @@ -27,16 +27,15 @@ import org.junit.Test; -import com.carrotsearch.sizeof.RamUsageEstimator; import com.hp.hpl.jena.graph.Factory; import com.hp.hpl.jena.graph.Graph; import com.hp.hpl.jena.graph.Node; import com.hp.hpl.jena.graph.NodeFactory; import com.hp.hpl.jena.graph.Triple; -import com.hp.hpl.jena.reasoner.TriplePattern; import com.hp.hpl.jena.reasoner.rulesys.FBRuleInfGraph; import com.hp.hpl.jena.reasoner.rulesys.FBRuleReasoner; import com.hp.hpl.jena.reasoner.rulesys.Rule; +import com.hp.hpl.jena.util.iterator.ExtendedIterator; import com.hp.hpl.jena.vocabulary.RDF; import com.hp.hpl.jena.vocabulary.RDFS; @@ -61,7 +60,7 @@ public FBRuleReasoner createReasoner(List rules) { @Test public void testSaturateTabledGoals() throws Exception { - final int MAX = 1; + final int MAX = 1024; // Set the cache size very small just for this test System.setProperty("jena.rulesys.lp.max_cached_tabled_goals", ""+MAX); try { @@ -72,39 +71,26 @@ public void testSaturateTabledGoals() throws Exception { "[r2: (?t rdf:type C2) <- (?x rdf:type C1), makeInstance(?x, p, C2, ?t)]"); FBRuleInfGraph infgraph = (FBRuleInfGraph) createReasoner(rules).bind(data); + + Field bEngine = FBRuleInfGraph.class.getDeclaredField("bEngine"); + bEngine.setAccessible(true); + LPBRuleEngine engine = (LPBRuleEngine) bEngine.get(infgraph); + assertEquals(0, engine.activeInterpreters.size()); + assertEquals(0, engine.tabledGoals.size()); // JENA-901 // Let's ask about lots of unknown subjects - for (int i=0; i it = infgraph.find(test, ty, C2); + assertFalse(it.hasNext()); + it.close(); } // Let's see how many were cached - Field bEngine = FBRuleInfGraph.class.getDeclaredField("bEngine"); - bEngine.setAccessible(true); - LPBRuleEngine engine = (LPBRuleEngine) bEngine.get(infgraph); assertEquals(MAX, engine.tabledGoals.size()); - System.gc(); - System.out.println(RamUsageEstimator.sizeOf(engine)); - data.clear(); - System.out.println(RamUsageEstimator.sizeOf(engine)); - System.out.println(RamUsageEstimator.sizeOf(engine)); - for (int i=0; i Date: Fri, 20 Mar 2015 16:31:26 +0000 Subject: [PATCH 7/8] JENA-901 BoundedMap -> BoundedLRUMap .. to point out that it's the least accessed entry that is pruned first --- .../hp/hpl/jena/reasoner/rulesys/impl/LPBRuleEngine.java | 4 ++-- .../hpl/jena/util/{BoundedMap.java => BoundedLRUMap.java} | 6 +++--- .../util/{TestBoundedMap.java => TestBoundedLRUMap.java} | 6 +++--- .../src/test/java/com/hp/hpl/jena/util/TestPackage.java | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-) rename jena-core/src/main/java/com/hp/hpl/jena/util/{BoundedMap.java => BoundedLRUMap.java} (91%) rename jena-core/src/test/java/com/hp/hpl/jena/util/{TestBoundedMap.java => TestBoundedLRUMap.java} (89%) diff --git a/jena-core/src/main/java/com/hp/hpl/jena/reasoner/rulesys/impl/LPBRuleEngine.java b/jena-core/src/main/java/com/hp/hpl/jena/reasoner/rulesys/impl/LPBRuleEngine.java index bbac95db281..f6ec3502010 100644 --- a/jena-core/src/main/java/com/hp/hpl/jena/reasoner/rulesys/impl/LPBRuleEngine.java +++ b/jena-core/src/main/java/com/hp/hpl/jena/reasoner/rulesys/impl/LPBRuleEngine.java @@ -35,7 +35,7 @@ import com.hp.hpl.jena.reasoner.TriplePattern; import com.hp.hpl.jena.reasoner.rulesys.BackwardRuleInfGraphI; import com.hp.hpl.jena.reasoner.rulesys.Rule; -import com.hp.hpl.jena.util.BoundedMap; +import com.hp.hpl.jena.util.BoundedLRUMap; import com.hp.hpl.jena.util.cache.CacheControl; import com.hp.hpl.jena.util.cache.CacheManager; import com.hp.hpl.jena.util.iterator.ExtendedIterator; @@ -74,7 +74,7 @@ public class LPBRuleEngine { /** Table mapping tabled goals to generators for those goals. * This is here so that partial goal state can be shared across multiple queries. */ - protected Map tabledGoals = new BoundedMap<>(MAX_CACHED_TABLED_GOALS); + protected Map tabledGoals = new BoundedLRUMap<>(MAX_CACHED_TABLED_GOALS); //protected Map tabledGoals = new HashMap<>(); /** Set of generators waiting to be run */ diff --git a/jena-core/src/main/java/com/hp/hpl/jena/util/BoundedMap.java b/jena-core/src/main/java/com/hp/hpl/jena/util/BoundedLRUMap.java similarity index 91% rename from jena-core/src/main/java/com/hp/hpl/jena/util/BoundedMap.java rename to jena-core/src/main/java/com/hp/hpl/jena/util/BoundedLRUMap.java index 8c1f6de0ffe..7ddd2a03f55 100644 --- a/jena-core/src/main/java/com/hp/hpl/jena/util/BoundedMap.java +++ b/jena-core/src/main/java/com/hp/hpl/jena/util/BoundedLRUMap.java @@ -33,17 +33,17 @@ * @param * Type of values */ -public class BoundedMap extends LinkedHashMap implements Map { +public class BoundedLRUMap extends LinkedHashMap implements Map { private static final long serialVersionUID = -1424511852972661771L; private int maxEntries; /** - * Construct a BoundedMap + * Construct a BoundedLRUMap * * @param maxEntries Maximum number of entries */ - public BoundedMap(int maxEntries) { + public BoundedLRUMap(int maxEntries) { super(Math.max(maxEntries/16, 16), 0.75f, true); if (maxEntries <= 0) { throw new IllegalArgumentException("maxEntries <= 0"); diff --git a/jena-core/src/test/java/com/hp/hpl/jena/util/TestBoundedMap.java b/jena-core/src/test/java/com/hp/hpl/jena/util/TestBoundedLRUMap.java similarity index 89% rename from jena-core/src/test/java/com/hp/hpl/jena/util/TestBoundedMap.java rename to jena-core/src/test/java/com/hp/hpl/jena/util/TestBoundedLRUMap.java index 5248e33485b..95dd0d959b3 100644 --- a/jena-core/src/test/java/com/hp/hpl/jena/util/TestBoundedMap.java +++ b/jena-core/src/test/java/com/hp/hpl/jena/util/TestBoundedLRUMap.java @@ -24,14 +24,14 @@ import org.junit.Test; -public class TestBoundedMap extends TestCase { +public class TestBoundedLRUMap extends TestCase { public static TestSuite suite() { - return new TestSuite(TestBoundedMap.class, "TestBoundedMap"); + return new TestSuite(TestBoundedLRUMap.class, "TestBoundedLRUMap"); } @Test public void testBoundedMap() throws Exception { - BoundedMap map = new BoundedMap(4); + BoundedLRUMap map = new BoundedLRUMap(4); map.put(1, 1); map.put(2, 2); map.put(3, 3); diff --git a/jena-core/src/test/java/com/hp/hpl/jena/util/TestPackage.java b/jena-core/src/test/java/com/hp/hpl/jena/util/TestPackage.java index 413d967a76c..401fed86720 100644 --- a/jena-core/src/test/java/com/hp/hpl/jena/util/TestPackage.java +++ b/jena-core/src/test/java/com/hp/hpl/jena/util/TestPackage.java @@ -35,7 +35,7 @@ static public TestSuite suite() { /** Creates new TestPackage */ private TestPackage() { super( "util" ); - addTest( "TestBoundedMap", TestBoundedMap.suite() ); + addTest( "TestBoundedLRUMap", TestBoundedLRUMap.suite() ); addTest( TestCache.suite() ); addTest( "TestTokenzier", TestTokenizer.suite()); addTest( "TestFileUtils", TestFileUtils.suite() ); From a293eca312f031c421e04cc4c7571f1321548a22 Mon Sep 17 00:00:00 2001 From: Stian Soiland-Reyes Date: Fri, 20 Mar 2015 22:16:51 +0000 Subject: [PATCH 8/8] Not using java-sizeof for testing anymore --- jena-core/pom.xml | 7 ------- 1 file changed, 7 deletions(-) diff --git a/jena-core/pom.xml b/jena-core/pom.xml index 4b4b65a42ba..c6fbc5d7019 100644 --- a/jena-core/pom.xml +++ b/jena-core/pom.xml @@ -45,13 +45,6 @@ test - - com.carrotsearch - java-sizeof - 0.0.4 - test - - org.slf4j slf4j-api