From: <pj...@us...> - 2009-03-27 21:45:11
|
Revision: 6111 http://jython.svn.sourceforge.net/jython/?rev=6111&view=rev Author: pjenvey Date: 2009-03-27 21:44:57 +0000 (Fri, 27 Mar 2009) Log Message: ----------- reduce our CHMs' concurrencyLevel to 2 from 16 to reduce allocation. speeds up pybench by 10% Modified Paths: -------------- trunk/jython/src/org/python/core/PyDictionary.java trunk/jython/src/org/python/core/PySet.java trunk/jython/src/org/python/core/PyStringMap.java trunk/jython/src/org/python/modules/PyTeeIterator.java trunk/jython/src/org/python/util/Generic.java Modified: trunk/jython/src/org/python/core/PyDictionary.java =================================================================== --- trunk/jython/src/org/python/core/PyDictionary.java 2009-03-27 20:34:45 UTC (rev 6110) +++ trunk/jython/src/org/python/core/PyDictionary.java 2009-03-27 21:44:57 UTC (rev 6111) @@ -4,20 +4,19 @@ import java.util.AbstractSet; import java.util.ArrayList; import java.util.Collection; -import java.util.Hashtable; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; -import java.util.concurrent.ConcurrentMap; import org.python.expose.ExposedClassMethod; import org.python.expose.ExposedMethod; import org.python.expose.ExposedNew; import org.python.expose.ExposedType; import org.python.expose.MethodType; +import org.python.util.Generic; /** @@ -34,7 +33,7 @@ * Create an empty dictionary. */ public PyDictionary() { - table = new ConcurrentHashMap<PyObject, PyObject>(); + table = Generic.concurrentMap(); } /** @@ -42,14 +41,15 @@ */ public PyDictionary(PyType subtype) { super(subtype); - table = new ConcurrentHashMap<PyObject, PyObject>(); + table = Generic.concurrentMap(); } /** * Create a new dictionary which is based on given map. */ public PyDictionary(Map<PyObject, PyObject> t) { - table = new ConcurrentHashMap<PyObject, PyObject>(t); + table = Generic.concurrentMap(); + table.putAll(t); } /** @@ -57,7 +57,8 @@ */ public PyDictionary(PyType subtype, Map<PyObject, PyObject> t) { super(subtype); - table = new ConcurrentHashMap<PyObject, PyObject>(t); + table = Generic.concurrentMap(); + table.putAll(t); } Modified: trunk/jython/src/org/python/core/PySet.java =================================================================== --- trunk/jython/src/org/python/core/PySet.java 2009-03-27 20:34:45 UTC (rev 6110) +++ trunk/jython/src/org/python/core/PySet.java 2009-03-27 21:44:57 UTC (rev 6111) @@ -2,12 +2,13 @@ import java.util.Iterator; import java.util.NoSuchElementException; +import java.util.Set; -import org.python.core.util.ConcurrentHashSet; import org.python.expose.ExposedMethod; import org.python.expose.ExposedNew; import org.python.expose.ExposedType; import org.python.expose.MethodType; +import org.python.util.Generic; @ExposedType(name = "set", base = PyObject.class) public class PySet extends BaseSet { @@ -15,17 +16,22 @@ public static final PyType TYPE = PyType.fromClass(PySet.class); public PySet() { - super(new ConcurrentHashSet<PyObject>()); + super(concurrentSet()); } public PySet(PyType type) { - super(type, new ConcurrentHashSet<PyObject>()); + super(type, concurrentSet()); } public PySet(PyObject data) { - super(_update(new ConcurrentHashSet<PyObject>(), data)); + super(_update(concurrentSet(), data)); } + /** Contextualize the needed Set<PyObject> type paramaters (generics workaround). */ + private static Set<PyObject> concurrentSet() { + return Generic.concurrentSet(); + } + @ExposedNew @ExposedMethod final void set___init__(PyObject[] args, String[] kwds) { Modified: trunk/jython/src/org/python/core/PyStringMap.java =================================================================== --- trunk/jython/src/org/python/core/PyStringMap.java 2009-03-27 20:34:45 UTC (rev 6110) +++ trunk/jython/src/org/python/core/PyStringMap.java 2009-03-27 21:44:57 UTC (rev 6111) @@ -9,6 +9,8 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; +import org.python.util.Generic; + /** * Special fast dict implementation for __dict__ instances. Allows interned String keys in addition * to PyObject unlike PyDictionary. @@ -22,11 +24,13 @@ } public PyStringMap(int capacity) { - table = new ConcurrentHashMap<Object, PyObject>(capacity); + table = new ConcurrentHashMap<Object, PyObject>(capacity, Generic.CHM_LOAD_FACTOR, + Generic.CHM_CONCURRENCY_LEVEL); } public PyStringMap(Map<Object, PyObject> map) { - table = new ConcurrentHashMap<Object, PyObject>(map); + table = Generic.concurrentMap(); + table.putAll(map); } public PyStringMap(PyObject elements[]) { Modified: trunk/jython/src/org/python/modules/PyTeeIterator.java =================================================================== --- trunk/jython/src/org/python/modules/PyTeeIterator.java 2009-03-27 20:34:45 UTC (rev 6110) +++ trunk/jython/src/org/python/modules/PyTeeIterator.java 2009-03-27 21:44:57 UTC (rev 6111) @@ -1,7 +1,6 @@ package org.python.modules; import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; import org.python.core.Py; import org.python.core.PyException; @@ -12,6 +11,7 @@ import org.python.expose.ExposedMethod; import org.python.expose.ExposedNew; import org.python.expose.ExposedType; +import org.python.util.Generic; @ExposedType(name = "itertools.tee", base = PyObject.class, isBaseType = false) public class PyTeeIterator extends PyIterator { @@ -45,7 +45,7 @@ throw Py.ValueError("n must be >= 0"); } PyObject iterator = iterable.__iter__(); - Map<Integer, PyObject> buffer = new ConcurrentHashMap<Integer, PyObject>(); + Map<Integer, PyObject> buffer = Generic.concurrentMap(); int[] offsets = new int[n]; PyTeeIterator[] tees = new PyTeeIterator[n]; for (int i = 0; i < n; i++) { Modified: trunk/jython/src/org/python/util/Generic.java =================================================================== --- trunk/jython/src/org/python/util/Generic.java 2009-03-27 20:34:45 UTC (rev 6110) +++ trunk/jython/src/org/python/util/Generic.java 2009-03-27 21:44:57 UTC (rev 6111) @@ -6,7 +6,11 @@ import java.util.List; import java.util.Map; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import org.python.core.util.ConcurrentHashSet; + /** * Static methods to make instances of collections with their generic types inferred from what * they're being assigned to. The idea is stolen from <code>Sets</code>, <code>Lists</code> and @@ -14,7 +18,16 @@ * Collections</a>. */ public class Generic { + /** + * Our default ConcurrentHashMap sizes. Only concurreny level differs from + * ConcurrentHashMap's defaults: it's significantly lower to reduce allocation cost. + */ + public static final int CHM_INITIAL_CAPACITY = 16; + public static final float CHM_LOAD_FACTOR = 0.75f; + public static final int CHM_CONCURRENCY_LEVEL = 2; + + /** * Makes a List with its generic type inferred from whatever it's being assigned to. */ public static <T> List<T> list() { @@ -40,6 +53,15 @@ } /** + * Makes a ConcurrentMap using generic types inferred from whatever this is being + * assigned to. + */ + public static <K, V> ConcurrentMap<K, V> concurrentMap() { + return new ConcurrentHashMap<K, V>(CHM_INITIAL_CAPACITY, CHM_LOAD_FACTOR, + CHM_CONCURRENCY_LEVEL); + } + + /** * Makes a Set using the generic type inferred from whatever this is being assigned to. */ public static <T> Set<T> set() { @@ -57,4 +79,13 @@ } return s; } + + /** + * Makes a Set, ensuring safe concurrent operations, using generic types inferred from + * whatever this is being assigned to. + */ + public static <T> Set<T> concurrentSet() { + return new ConcurrentHashSet<T>(CHM_INITIAL_CAPACITY, CHM_LOAD_FACTOR, + CHM_CONCURRENCY_LEVEL); + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |