Permalink
Browse files

var List is actually faster than mutable Stack

  • Loading branch information...
djspiewak committed May 11, 2011
1 parent 0b92bb7 commit b2a697a5e5261e54eafe9d23bf87fbc5d92548ae
@@ -29,7 +29,6 @@
package com.codecommit
package antixml
import scala.collection.mutable.Stack
import util._
import org.xml.sax.{Attributes => SAXAttributes}
@@ -50,7 +49,7 @@ class NodeSeqSAXHandler extends DefaultHandler2 {
private var elems = List[Group[Node] => Elem]()
private val text = new StringBuilder
private var isCDATA = false
private var scopes : Stack[Map[String, String]] = Stack(Map())
private var scopes = Map[String, String]() :: Nil
private var builders = VectorCase.newBuilder[Node] :: Nil
@@ -59,12 +58,12 @@ class NodeSeqSAXHandler extends DefaultHandler2 {
isCDATA = true
}
override def startPrefixMapping(prefix : String, namespace : String) {
scopes.push (scopes.top + (prefix -> namespace))
override def startPrefixMapping(prefix: String, namespace: String) {
scopes ::= (scopes.headOption map { _ + (prefix -> namespace) } getOrElse Map())
}
override def endPrefixMapping(prefix : String) {
scopes.pop
override def endPrefixMapping(prefix: String) {
scopes = scopes.tail
}
override def endCDATA() {
@@ -98,12 +97,14 @@ class NodeSeqSAXHandler extends DefaultHandler2 {
builders ::= VectorCase.newBuilder
elems ::= { children =>
val prefix = {
if (qName == localName) None else Some(qName.substring(0, qName.length - localName.length -1))
}
val prefix = if (qName == localName)
None
else
Some(qName.substring(0, qName.length - localName.length - 1))
val ns = if (uri == "") None else Some(uri)
Elem(QName(ns, prefix, localName), map, scopes.top, children)
Elem(QName(ns, prefix, localName), map, scopes.headOption getOrElse Map(), children)
}
}
@@ -28,7 +28,6 @@
package com.codecommit.antixml
import scala.collection.mutable.Stack
import util.VectorCase
import java.io.{InputStream, StringReader, Reader}
import javax.xml.stream.{XMLInputFactory, XMLStreamException}
@@ -59,7 +58,7 @@ class StAXParser extends XMLParser {
val xmlReader = XMLInputFactory.newInstance().createXMLStreamReader(source)
var elems: List[ElemBuilder] = Nil
var prefixMapping : Stack[Map[String, String]] = Stack(Map())
var prefixMapping = Map[String, String]() :: Nil
var results = VectorCase.newBuilder[Node] :: Nil
val text = new StringBuilder
while(xmlReader.hasNext) {
@@ -71,11 +70,16 @@ class StAXParser extends XMLParser {
val parents = elems.tail
val children = results.head
val ancestors = results.tail
val mapping = {
val back = prefixMapping.head
prefixMapping = prefixMapping.tail
back
}
if (text.size > 0) {
children += Text(text.result)
text.clear()
}
ancestors.head += Elem(QName(elem.ns, elem.prefix, elem.name), elem.attrs, prefixMapping.pop, Group fromSeq children.result)
ancestors.head += Elem(QName(elem.ns, elem.prefix, elem.name), elem.attrs, mapping, Group fromSeq children.result)
elems = parents
results = ancestors
}
@@ -85,14 +89,14 @@ class StAXParser extends XMLParser {
text.clear()
}
var i = 0
var prefixes = prefixMapping.top
var prefixes = prefixMapping.headOption getOrElse Map()
while (i < xmlReader.getNamespaceCount) {
val ns = Option(xmlReader.getNamespaceURI(i))
val prefix = xmlReader.getNamespacePrefix(i)
prefixes = prefixes + (prefix -> ns.getOrElse(""))
i = i + 1
}
prefixMapping.push(prefixes)
prefixMapping ::= prefixes
var attrs = Attributes()
while (i < xmlReader.getAttributeCount) {
val ns = Option(xmlReader.getAttributeNamespace(i))

0 comments on commit b2a697a

Please sign in to comment.