Skip to content
Browse files

Reuse the same empty Group instance. This is an easy win for both mem…

…ory and bloom filter creation costs
  • Loading branch information...
1 parent febd26a commit 36d0978098d3b1ac742b1c38f3441cd298306422 @josharnold52 josharnold52 committed
Showing with 4 additions and 2 deletions.
  1. +4 −2 src/main/scala/com/codecommit/antixml/Group.scala
View
6 src/main/scala/com/codecommit/antixml/Group.scala
@@ -390,10 +390,12 @@ object Group {
def newBuilder[A <: Node] = VectorCase.newBuilder[A] mapResult { new Group(_) }
+ private val theEmpty: Group[Nothing] = new Group(Vector0)
+
/**
* @return An empty [[com.codecommit.antixml.Group]] with the given parameter type.
*/
- def empty[A <: Node] = new Group[A](VectorCase.empty)
+ def empty[A <: Node]: Group[A] = theEmpty
/**
* Builds a new group with the specified set of nodes in order. The most specific
@@ -415,6 +417,6 @@ object Group {
* to ensure that the sequences you pass to this method are always of type
* `Vector`, since this will avoid the penalty.
*/
- def fromSeq[A <: Node](seq: Seq[A]) = new Group(VectorCase(seq: _*))
+ def fromSeq[A <: Node](seq: Seq[A]): Group[A] = if (seq.isEmpty) theEmpty else new Group(VectorCase(seq: _*))
}

0 comments on commit 36d0978

Please sign in to comment.
Something went wrong with that request. Please try again.