Github user rxin commented on a diff in the pull request:

    https://github.com/apache/spark/pull/1555#discussion_r15335029
  
    --- Diff: 
core/src/main/scala/org/apache/spark/util/collection/CompactBuffer.scala ---
    @@ -0,0 +1,155 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.spark.util.collection
    +
    +/**
    + * An append-only buffer similar to ArrayBuffer, but more memory-efficient 
for small buffers.
    + * ArrayBuffer always allocates an Object array to store the data, with 16 
entries by default,
    + * so it has about 80-100 bytes of overhead. In contrast, CompactBuffer 
can keep up to two
    + * elements in fields of the main object, and only allocates an 
Array[AnyRef] if there are more
    + * entries than that. This makes it more efficient for operations like 
groupBy where we expect
    + * some keys to have very few elements.
    + */
    +private[spark] class CompactBuffer[T] extends Seq[T] with Serializable {
    +  // First two elements
    +  private var element0: T = _
    +  private var element1: T = _
    +
    +  // Number of elements, including our two in the main object
    +  private var curSize = 0
    +
    +  // Array for extra elements
    +  private var otherElements: Array[AnyRef] = null
    +
    +  def apply(position: Int): T = {
    +    if (position < 0 || position >= curSize) {
    +      throw new IndexOutOfBoundsException
    +    }
    +    if (position == 0) {
    +      element0
    +    } else if (position == 1) {
    +      element1
    +    } else {
    +      otherElements(position - 2).asInstanceOf[T]
    +    }
    +  }
    +
    +  def update(position: Int, value: T): Unit = {
    +    if (position < 0 || position >= curSize) {
    +      throw new IndexOutOfBoundsException
    +    }
    +    if (position == 0) {
    +      element0 = value
    +    } else if (position == 1) {
    +      element1 = value
    +    } else {
    +      otherElements(position - 2) = value.asInstanceOf[AnyRef]
    +    }
    +  }
    +
    +  def += (value: T): CompactBuffer[T] = {
    +    val newIndex = curSize
    +    if (newIndex == 0) {
    +      element0 = value
    +      curSize = 1
    +    } else if (newIndex == 1) {
    +      element1 = value
    +      curSize = 2
    +    } else {
    +      growToSize(curSize + 1)
    +      otherElements(newIndex - 2) = value.asInstanceOf[AnyRef]
    +    }
    +    this
    +  }
    +
    +  def ++= (values: TraversableOnce[T]): CompactBuffer[T] = {
    +    values match {
    +      case compactBuf: CompactBuffer[T] =>
    +        val oldSize = curSize
    +        // Copy the other buffer's size and elements to local variables in 
case it is equal to us
    +        val itsSize = compactBuf.curSize
    +        val itsElements = compactBuf.otherElements
    +        growToSize(curSize + itsSize)
    +        if (itsSize > 0) {
    --- End diff --
    
    it might make sense to slightly re-arrange the conditions to reduce the 
number of branches. Right now it's a constant (3 branches for every case). If 
you do 
    ```
    if (itsSize > 2) {
    
    } else if (itsSize > 1) {
    
    } else {
    }
    ```
    
    you can reduce it to 1 branch for larger arrays, 2 branches for 2 element 
array, and 3 branches for 1 element array.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to