diff options
author | Robert Godfrey <rgodfrey@apache.org> | 2007-07-17 12:00:57 +0000 |
---|---|---|
committer | Robert Godfrey <rgodfrey@apache.org> | 2007-07-17 12:00:57 +0000 |
commit | e061b189402d96fbcaeb792c714461be21cd057f (patch) | |
tree | f1423810ca74285091c531ede9fc31e861d7c593 | |
parent | 0579b1408d8dae4d85089fba5f1c8c14e04d1b61 (diff) | |
download | qpid-python-e061b189402d96fbcaeb792c714461be21cd057f.tar.gz |
QPID-541 : Change to use threadlocal maps for intern for the common case to avoid excessive synchronization. In the uncommon case will require more lookup.
git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/branches/M2@556898 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r-- | java/common/src/main/java/org/apache/qpid/framing/AMQShortString.java | 50 |
1 files changed, 38 insertions, 12 deletions
diff --git a/java/common/src/main/java/org/apache/qpid/framing/AMQShortString.java b/java/common/src/main/java/org/apache/qpid/framing/AMQShortString.java index ec29d62847..df99f6589d 100644 --- a/java/common/src/main/java/org/apache/qpid/framing/AMQShortString.java +++ b/java/common/src/main/java/org/apache/qpid/framing/AMQShortString.java @@ -39,7 +39,16 @@ import java.lang.ref.WeakReference; public final class AMQShortString implements CharSequence, Comparable<AMQShortString>
{
- private static final Map<AMQShortString, WeakReference<AMQShortString>> internMap =
+ private static final ThreadLocal<Map<AMQShortString, WeakReference<AMQShortString>>> _localInternMap =
+ new ThreadLocal<Map<AMQShortString, WeakReference<AMQShortString>>>()
+ {
+ protected Map<AMQShortString, WeakReference<AMQShortString>> initialValue()
+ {
+ return new WeakHashMap<AMQShortString, WeakReference<AMQShortString>>();
+ };
+ };
+
+ private static final Map<AMQShortString, WeakReference<AMQShortString>> _globalInternMap =
new WeakHashMap<AMQShortString, WeakReference<AMQShortString>>();
private static final Logger _logger = LoggerFactory.getLogger(AMQShortString.class);
@@ -51,6 +60,7 @@ public final class AMQShortString implements CharSequence, Comparable<AMQShortSt public AMQShortString(byte[] data)
{
+
_data = ByteBuffer.wrap(data);
_length = data.length;
}
@@ -387,23 +397,39 @@ public final class AMQShortString implements CharSequence, Comparable<AMQShortSt public AMQShortString intern()
{
+
hashCode();
- synchronized(internMap)
+
+ Map<AMQShortString, WeakReference<AMQShortString>> localMap =
+ _localInternMap.get();
+
+ WeakReference<AMQShortString> ref = localMap.get(this);
+ AMQShortString internString;
+
+ if(ref != null)
+ {
+ internString = ref.get();
+ if(internString != null)
+ {
+ return internString;
+ }
+ }
+
+
+ synchronized(_globalInternMap)
{
- WeakReference<AMQShortString> ref = internMap.get(this);
- if(ref != null)
+ ref = _globalInternMap.get(this);
+ if((ref == null) || ((internString = ref.get()) == null))
{
- AMQShortString internString = ref.get();
- if(internString != null)
- {
- return internString;
- }
+ internString = new AMQShortString(getBytes());
+ ref = new WeakReference(internString);
+ _globalInternMap.put(internString, ref);
}
- AMQShortString internString = new AMQShortString(getBytes());
- internMap.put(internString, new WeakReference(internString));
- return internString;
}
+ localMap.put(internString, ref);
+ return internString;
+
}
}
|