8000 Implement ListBuffer.isEmpty / nonEmpty / head efficiently · scala/scala@43f194d · GitHub
[go: up one dir, main page]

Skip to content

Commit 43f194d

Browse files
committed
Implement ListBuffer.isEmpty / nonEmpty / head efficiently
Uses the extra length information in comparison to list to provide efficient implementations. Evaluating these three methods turns up with about 6-7% of akka-http message parsing. From the profiles it looks the likely reason are polymorphic invokeinterface calls against List (i.e. just forwarding to `start.isEmpty` etc wouldn't help).
1 parent f2c6005 commit 43f194d

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

src/library/scala/collection/mutable/ListBuffer.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ final class ListBuffer[A]
119119
// Don't use the inherited size, which forwards to a List and is O(n).
120120
override def size = length
121121

122+
// Override with efficient implementations using the extra size information available to ListBuffer.
123+
override def isEmpty: Boolean = len == 0
124+
override def nonEmpty: Boolean = len > 0
125+
override def head: A = if (len > 0) start.asInstanceOf[::[A]].head else Nil.head
126+
122127
// Implementations of abstract methods in Buffer
123128

124129
override def apply(n: Int): A =

0 commit comments

Comments
 (0)
0