Skip to Content.
Sympa Menu

xom-interest - Re: [XOM-interest] QName flyweights

xom-interest AT lists.ibiblio.org

Subject: XOM API for Processing XML with Java

List archive

Chronological Thread  
  • From: norwoods <norwoods AT gbronline.com>
  • Cc: xom-interest AT lists.ibiblio.org
  • Subject: Re: [XOM-interest] QName flyweights
  • Date: Fri, 10 Jun 2005 16:39:09 -0700

Mik Lernout wrote:

My 2 cents: I work a lot in multi-threaded, heavy-usage environments, and those synchronized blocks are way to wide and will crush performance when running in ore than one thread. Elliotte: can you add concurrent access in your test cases.

You just need a weak referenced Map as a cache, and use "double checking", or not, as I don't think it is that dangerous that there are two equal QNames floating around of which only one is saved in the cache...

-mik

here is a try at a weak reference QName class. it uses a method, newQName(), to get a QName instead of a constructor, new QName(). it tries to clean up memory when newQName() is called but also has a trimToSize() method to clean up memory.


package nu.xom;

import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.util.ArrayList;

final class QName {
static ArrayList cache = new ArrayList();

private static ReferenceQueue queue = new ReferenceQueue();

private static QName target = new QName("");

private String localName;

private String name;

private String prefix;

private String uri;

private QName(String nada) {// constructor for target variable only
}

private QName() {
this.localName = target.localName.intern();
this.name = target.name.intern();
this.prefix = target.prefix.intern();
this.uri = target.uri.intern();
}

public static QName newQName(String name, String uri) {
if (name == null)
throw new NullPointerException();
target.name = name;
if (uri == null)
target.uri = "";
else
target.uri = uri;
int i = name.indexOf(':');
if (i < 0) {
target.prefix = "";
target.localName = name;
} else {
target.prefix = name.substring(0, i);
target.localName = name.substring(i + 1);
}
return checkCache();
}

public static QName newQName(String prefix, String localName, String uri) {
if (localName == null)
throw new NullPointerException();
target.localName = localName;
if (uri == null)
target.uri = "";
else
target.uri = uri;
if (prefix == null)
target.prefix = "";
else
target.prefix = prefix;
if (target.prefix == "")
target.name = localName;
else
target.name = prefix + ":" + localName;
return checkCache();
}

private static QName checkCache() {
Reference reference;
QName value;
while ((reference = queue.poll()) != null) {
cache.remove(reference);
}
int high = 0, low = 0, probe;
boolean recycle = true;
while (recycle) {
recycle = false;
high = cache.size();
low = -1;
while (high - low > 1) {
probe = (high + low) / 2;
reference = (WeakReference) cache.get(probe);
value = (QName) reference.get();
if (value == null) {
cache.remove(probe);
recycle = true;
break;
}
if ((value.getQualifiedName().compareTo(target
.getQualifiedName())) > 0
&& (value.getNamespaceURI().compareTo(target
.getNamespaceURI())) > 0)
high = probe;
else
low = probe;
}
}
if (low != -1) {
reference = (WeakReference) cache.get(low);
value = (QName) reference.get();
if (value == target)
return value;
}
value = new QName();
reference = new WeakReference(value, queue);
cache.add(high, (WeakReference) reference);
return value;
}

public static void trimToSize() {
Reference reference;
while ((reference = queue.poll()) != null) {
cache.remove(reference);
}
cache.trimToSize();
}

public String getLocalName() {
return localName;
}

public String getNamespacePrefix() {
return prefix;
}

public String getNamespaceURI() {
return uri;
}

public String getQualifiedName() {
return name;
}

public boolean equals(QName qName) {
if (this == qName)
return true;
return (name.equals(qName.getQualifiedName()) && (uri.equals(qName
.getNamespaceURI())));
}

public int hashCode() {
return uri.hashCode() ^ name.hashCode();
}
}




Archive powered by MHonArc 2.6.24.

Top of Page