Author: manik.surtani(a)jboss.com
Date: 2008-01-05 07:52:51 -0500 (Sat, 05 Jan 2008)
New Revision: 4998
Modified:
core/trunk/src/main/java/org/jboss/cache/marshall/CacheMarshaller200.java
Log:
Improved performance of reference mapping collection
Modified: core/trunk/src/main/java/org/jboss/cache/marshall/CacheMarshaller200.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/marshall/CacheMarshaller200.java 2008-01-05
12:52:33 UTC (rev 4997)
+++ core/trunk/src/main/java/org/jboss/cache/marshall/CacheMarshaller200.java 2008-01-05
12:52:51 UTC (rev 4998)
@@ -117,7 +117,7 @@
{
// not region based!
if (trace) log.trace("Marshalling object " + o);
- Map<Object, Integer> refMap = new HashMap<Object, Integer>();
+ ReferencesMap refMap = new ReferencesMap();
marshallObject(o, out, refMap);
}
}
@@ -140,7 +140,7 @@
public void objectToObjectStream(Object o, ObjectOutputStream out, Fqn region) throws
Exception
{
if (trace) log.trace("Marshalling object " + o);
- Map<Object, Integer> refMap = new HashMap<Object, Integer>();
+ ReferencesMap refMap = new ReferencesMap();
if (useRegionBasedMarshalling) // got to check again in case this meth is called
directly
{
log.trace("Writing region " + region + " to stream");
@@ -257,16 +257,18 @@
}
// --------- Marshalling methods
- private void marshallObject(Object o, ObjectOutputStream out, Map<Object,
Integer> refMap) throws Exception
+ private void marshallObject(Object o, ObjectOutputStream out, ReferencesMap refMap)
throws Exception
{
+ int reference;
+
if (o == null)
{
out.writeByte(MAGICNUMBER_NULL);
}
- else if (refMap.containsKey(o))// see if this object has been marshalled before.
+ else if ((reference = refMap.getReference(o)) > -1)// see if this object has
been marshalled before.
{
out.writeByte(MAGICNUMBER_REF);
- writeReference(out, refMap.get(o));
+ writeReference(out, reference);
}
else if (o instanceof MethodCall)
{
@@ -402,7 +404,7 @@
}
}
- private void marshallGravitateResult(GravitateResult gravitateResult,
ObjectOutputStream out, Map<Object, Integer> refMap) throws Exception
+ private void marshallGravitateResult(GravitateResult gravitateResult,
ObjectOutputStream out, ReferencesMap refMap) throws Exception
{
marshallObject(gravitateResult.isDataFound(), out, refMap);
if (gravitateResult.isDataFound())
@@ -413,20 +415,19 @@
}
- private int createReference(Object o, Map<Object, Integer> refMap)
+ private int createReference(Object o, ReferencesMap refMap)
{
- int reference = refMap.size();
- refMap.put(o, reference);
+ int reference = refMap.nextAvailableReference();
+ refMap.put(reference, o);
return reference;
}
private void marshallString(String s, ObjectOutputStream out) throws Exception
{
- //StringUtil.saveString(out, s);
out.writeObject(s);
}
- private void marshallMethodCall(MethodCall methodCall, ObjectOutputStream out,
Map<Object, Integer> refMap) throws Exception
+ private void marshallMethodCall(MethodCall methodCall, ObjectOutputStream out,
ReferencesMap refMap) throws Exception
{
out.writeShort(methodCall.getMethodId());
Object[] args = methodCall.getArgs();
@@ -439,14 +440,14 @@
}
}
- private void marshallGlobalTransaction(GlobalTransaction globalTransaction,
ObjectOutputStream out, Map<Object, Integer> refMap) throws Exception
+ private void marshallGlobalTransaction(GlobalTransaction globalTransaction,
ObjectOutputStream out, ReferencesMap refMap) throws Exception
{
out.writeLong(globalTransaction.getId());
marshallObject(globalTransaction.getAddress(), out, refMap);
}
- private void marshallFqn(Fqn fqn, ObjectOutputStream out, Map<Object, Integer>
refMap) throws Exception
+ private void marshallFqn(Fqn fqn, ObjectOutputStream out, ReferencesMap refMap) throws
Exception
{
boolean isRoot = fqn.isRoot();
out.writeBoolean(isRoot);
@@ -465,7 +466,7 @@
ipAddress.writeExternal(out);
}
- private void marshallCollection(Collection c, ObjectOutputStream out, Map refMap)
throws Exception
+ private void marshallCollection(Collection c, ObjectOutputStream out, ReferencesMap
refMap) throws Exception
{
writeUnsignedInt(out, c.size());
for (Object o : c)
@@ -474,7 +475,7 @@
}
}
- private void marshallMap(Map map, ObjectOutputStream out, Map<Object, Integer>
refMap) throws Exception
+ private void marshallMap(Map map, ObjectOutputStream out, ReferencesMap refMap) throws
Exception
{
int mapSize = map.size();
writeUnsignedInt(out, mapSize);
@@ -809,3 +810,28 @@
out.writeLong(i);
}
}
+
+class ReferencesMap
+{
+ private ArrayList<Object> referencedObjects = new ArrayList<Object>();
+
+ void put(int reference, Object object)
+ {
+ referencedObjects.add(reference, object);
+ }
+
+ Object get(int reference)
+ {
+ return referencedObjects.get(reference);
+ }
+
+ int nextAvailableReference()
+ {
+ return referencedObjects.size();
+ }
+
+ int getReference(Object object)
+ {
+ return referencedObjects.indexOf(object);
+ }
+}