You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by rxin <gi...@git.apache.org> on 2014/08/20 10:00:25 UTC

[GitHub] spark pull request: [SPARK-3135] Created ByteArrayChunkOutputStrea...

GitHub user rxin opened a pull request:

    https://github.com/apache/spark/pull/2054

    [SPARK-3135] Created ByteArrayChunkOutputStream and used it to avoid mem copy in TorrentBroadcast.

    

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/rxin/spark ByteArrayChunkOutputStream

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/spark/pull/2054.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #2054
    
----
commit 8f1a8eb59af3760651a391ea0fc76255d855cf2e
Author: Reynold Xin <rx...@apache.org>
Date:   2014-08-20T07:59:01Z

    [SPARK-3135] Created ByteArrayChunkOutputStream and used it to avoid memory copy in TorrentBroadcast.

----


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-3135] Avoid extra mem copy in TorrentBr...

Posted by aarondav <gi...@git.apache.org>.
Github user aarondav commented on a diff in the pull request:

    https://github.com/apache/spark/pull/2054#discussion_r16509806
  
    --- Diff: core/src/main/scala/org/apache/spark/util/io/ByteArrayChunkOutputStream.scala ---
    @@ -0,0 +1,87 @@
    +/*
    + * 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.io
    +
    +import java.io.OutputStream
    +
    +import scala.collection.mutable.ArrayBuffer
    +
    +
    +/**
    + * An OutputStream that writes to fixed-size chunks of byte arrays.
    + */
    +private[spark]
    +class ByteArrayChunkOutputStream(chunkSize: Int) extends OutputStream {
    +
    +  private val chunks = new ArrayBuffer[Array[Byte]]
    +
    +  private var lastChunkIndex = -1
    +  private var position = chunkSize
    +
    +  def toArrays: Array[Array[Byte]] = {
    +    if (lastChunkIndex == -1) {
    +      new Array[Array[Byte]](0)
    +    } else {
    +      // Copy the first n-1 chunks to the output, and then create an array that fits the last chunk.
    +      // An alternative would have been returning an array of ByteBuffers, with the last buffer
    +      // bounded to only the last chunk's position. However, given our use case in Spark (to put
    +      // the chunks in block manager), only limiting the view bound of the buffer would still
    +      // require the block manager to store the whole chunk.
    +      val ret = new Array[Array[Byte]](chunks.size)
    +      for (i <- 0 until chunks.size - 1) {
    +        ret(i) = chunks(i)
    +      }
    +      if (position == chunkSize) {
    +        ret(lastChunkIndex) = chunks(lastChunkIndex)
    +      } else {
    +        ret(lastChunkIndex) = new Array[Byte](position)
    +        System.arraycopy(chunks(lastChunkIndex), 0, ret(lastChunkIndex), 0, position)
    +      }
    +      ret
    +    }
    +  }
    +
    +  override def write(b: Int): Unit = {
    +    allocateNewChunkIfNeeded()
    +    chunks(lastChunkIndex)(position) = b.toByte
    +    position += 1
    +  }
    +
    +  override def write(bytes: Array[Byte]): Unit = write(bytes, 0, bytes.length)
    --- End diff --
    
    nit: It's technically not necessary to implement this, as OutputStream already does.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-3135] Avoid extra mem copy in TorrentBr...

Posted by aarondav <gi...@git.apache.org>.
Github user aarondav commented on a diff in the pull request:

    https://github.com/apache/spark/pull/2054#discussion_r16509294
  
    --- Diff: core/src/main/scala/org/apache/spark/util/io/ByteArrayChunkOutputStream.scala ---
    @@ -0,0 +1,87 @@
    +/*
    + * 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.io
    +
    +import java.io.OutputStream
    +
    +import scala.collection.mutable.ArrayBuffer
    +
    +
    +/**
    + * An OutputStream that writes to fixed-size chunks of byte arrays.
    + */
    +private[spark]
    +class ByteArrayChunkOutputStream(chunkSize: Int) extends OutputStream {
    --- End diff --
    
    `@param chunkSize`


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-3135] Avoid extra mem copy in TorrentBr...

Posted by rxin <gi...@git.apache.org>.
Github user rxin commented on a diff in the pull request:

    https://github.com/apache/spark/pull/2054#discussion_r16510277
  
    --- Diff: core/src/main/scala/org/apache/spark/util/io/ByteArrayChunkOutputStream.scala ---
    @@ -0,0 +1,87 @@
    +/*
    + * 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.io
    +
    +import java.io.OutputStream
    +
    +import scala.collection.mutable.ArrayBuffer
    +
    +
    +/**
    + * An OutputStream that writes to fixed-size chunks of byte arrays.
    + */
    +private[spark]
    +class ByteArrayChunkOutputStream(chunkSize: Int) extends OutputStream {
    +
    +  private val chunks = new ArrayBuffer[Array[Byte]]
    +
    +  private var lastChunkIndex = -1
    +  private var position = chunkSize
    +
    +  def toArrays: Array[Array[Byte]] = {
    +    if (lastChunkIndex == -1) {
    +      new Array[Array[Byte]](0)
    +    } else {
    +      // Copy the first n-1 chunks to the output, and then create an array that fits the last chunk.
    +      // An alternative would have been returning an array of ByteBuffers, with the last buffer
    +      // bounded to only the last chunk's position. However, given our use case in Spark (to put
    +      // the chunks in block manager), only limiting the view bound of the buffer would still
    +      // require the block manager to store the whole chunk.
    +      val ret = new Array[Array[Byte]](chunks.size)
    +      for (i <- 0 until chunks.size - 1) {
    +        ret(i) = chunks(i)
    +      }
    +      if (position == chunkSize) {
    --- End diff --
    
    position can never be 0 in this case?


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-3135] Avoid extra mem copy in TorrentBr...

Posted by rxin <gi...@git.apache.org>.
Github user rxin commented on a diff in the pull request:

    https://github.com/apache/spark/pull/2054#discussion_r16510254
  
    --- Diff: core/src/main/scala/org/apache/spark/util/io/ByteArrayChunkOutputStream.scala ---
    @@ -0,0 +1,87 @@
    +/*
    + * 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.io
    +
    +import java.io.OutputStream
    +
    +import scala.collection.mutable.ArrayBuffer
    +
    +
    +/**
    + * An OutputStream that writes to fixed-size chunks of byte arrays.
    + */
    +private[spark]
    +class ByteArrayChunkOutputStream(chunkSize: Int) extends OutputStream {
    +
    +  private val chunks = new ArrayBuffer[Array[Byte]]
    +
    +  private var lastChunkIndex = -1
    +  private var position = chunkSize
    +
    +  def toArrays: Array[Array[Byte]] = {
    +    if (lastChunkIndex == -1) {
    +      new Array[Array[Byte]](0)
    +    } else {
    +      // Copy the first n-1 chunks to the output, and then create an array that fits the last chunk.
    +      // An alternative would have been returning an array of ByteBuffers, with the last buffer
    +      // bounded to only the last chunk's position. However, given our use case in Spark (to put
    +      // the chunks in block manager), only limiting the view bound of the buffer would still
    +      // require the block manager to store the whole chunk.
    +      val ret = new Array[Array[Byte]](chunks.size)
    +      for (i <- 0 until chunks.size - 1) {
    +        ret(i) = chunks(i)
    +      }
    +      if (position == chunkSize) {
    +        ret(lastChunkIndex) = chunks(lastChunkIndex)
    +      } else {
    +        ret(lastChunkIndex) = new Array[Byte](position)
    +        System.arraycopy(chunks(lastChunkIndex), 0, ret(lastChunkIndex), 0, position)
    +      }
    +      ret
    +    }
    +  }
    +
    +  override def write(b: Int): Unit = {
    +    allocateNewChunkIfNeeded()
    +    chunks(lastChunkIndex)(position) = b.toByte
    +    position += 1
    +  }
    +
    +  override def write(bytes: Array[Byte]): Unit = write(bytes, 0, bytes.length)
    +
    +  override def write(bytes: Array[Byte], off: Int, len: Int): Unit = {
    +    var written = off
    --- End diff --
    
    yea except then i'd need to add off in arraycopy ...


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-3135] Avoid extra mem copy in TorrentBr...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the pull request:

    https://github.com/apache/spark/pull/2054#issuecomment-52867732
  
      [QA tests have started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/19028/consoleFull) for   PR 2054 at commit [`618d9c8`](https://github.com/apache/spark/commit/618d9c8f9c8a70e2ff0b5ed15e71400ce608001e).
     * This patch merges cleanly.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-3135] Avoid extra mem copy in TorrentBr...

Posted by rxin <gi...@git.apache.org>.
Github user rxin commented on the pull request:

    https://github.com/apache/spark/pull/2054#issuecomment-52889531
  
    Updated & tests passed.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-3135] Avoid extra mem copy in TorrentBr...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the pull request:

    https://github.com/apache/spark/pull/2054#issuecomment-52826580
  
      [QA tests have started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/18979/consoleFull) for   PR 2054 at commit [`93f5a51`](https://github.com/apache/spark/commit/93f5a5117379f3bbb611c5d9bc37405cd305e2db).
     * This patch merges cleanly.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-3135] Avoid extra mem copy in TorrentBr...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the pull request:

    https://github.com/apache/spark/pull/2054#issuecomment-52834834
  
      [QA tests have finished](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/18979/consoleFull) for   PR 2054 at commit [`93f5a51`](https://github.com/apache/spark/commit/93f5a5117379f3bbb611c5d9bc37405cd305e2db).
     * This patch **passes** unit tests.
     * This patch merges cleanly.
     * This patch adds no public classes.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-3135] Avoid extra mem copy in TorrentBr...

Posted by aarondav <gi...@git.apache.org>.
Github user aarondav commented on a diff in the pull request:

    https://github.com/apache/spark/pull/2054#discussion_r16509304
  
    --- Diff: core/src/main/scala/org/apache/spark/util/io/ByteArrayChunkOutputStream.scala ---
    @@ -0,0 +1,87 @@
    +/*
    + * 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.io
    +
    +import java.io.OutputStream
    +
    +import scala.collection.mutable.ArrayBuffer
    +
    +
    +/**
    + * An OutputStream that writes to fixed-size chunks of byte arrays.
    + */
    +private[spark]
    +class ByteArrayChunkOutputStream(chunkSize: Int) extends OutputStream {
    +
    +  private val chunks = new ArrayBuffer[Array[Byte]]
    +
    +  private var lastChunkIndex = -1
    +  private var position = chunkSize
    +
    +  def toArrays: Array[Array[Byte]] = {
    +    if (lastChunkIndex == -1) {
    +      new Array[Array[Byte]](0)
    +    } else {
    +      // Copy the first n-1 chunks to the output, and then create an array that fits the last chunk.
    +      // An alternative would have been returning an array of ByteBuffers, with the last buffer
    +      // bounded to only the last chunk's position. However, given our use case in Spark (to put
    +      // the chunks in block manager), only limiting the view bound of the buffer would still
    +      // require the block manager to store the whole chunk.
    +      val ret = new Array[Array[Byte]](chunks.size)
    +      for (i <- 0 until chunks.size - 1) {
    +        ret(i) = chunks(i)
    +      }
    +      if (position == chunkSize) {
    +        ret(lastChunkIndex) = chunks(lastChunkIndex)
    +      } else {
    +        ret(lastChunkIndex) = new Array[Byte](position)
    +        System.arraycopy(chunks(lastChunkIndex), 0, ret(lastChunkIndex), 0, position)
    +      }
    +      ret
    +    }
    +  }
    +
    +  override def write(b: Int): Unit = {
    +    allocateNewChunkIfNeeded()
    +    chunks(lastChunkIndex)(position) = b.toByte
    +    position += 1
    +  }
    +
    +  override def write(bytes: Array[Byte]): Unit = write(bytes, 0, bytes.length)
    +
    +  override def write(bytes: Array[Byte], off: Int, len: Int): Unit = {
    +    var written = off
    --- End diff --
    
    I think written could be 0, total could be len (i.e., deleted), and `total - written` could be `len - written`. That way all the variables mean what they say :)


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-3135] Avoid extra mem copy in TorrentBr...

Posted by rxin <gi...@git.apache.org>.
Github user rxin commented on a diff in the pull request:

    https://github.com/apache/spark/pull/2054#discussion_r16510308
  
    --- Diff: core/src/main/scala/org/apache/spark/util/io/ByteArrayChunkOutputStream.scala ---
    @@ -0,0 +1,87 @@
    +/*
    + * 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.io
    +
    +import java.io.OutputStream
    +
    +import scala.collection.mutable.ArrayBuffer
    +
    +
    +/**
    + * An OutputStream that writes to fixed-size chunks of byte arrays.
    + */
    +private[spark]
    +class ByteArrayChunkOutputStream(chunkSize: Int) extends OutputStream {
    +
    +  private val chunks = new ArrayBuffer[Array[Byte]]
    +
    +  private var lastChunkIndex = -1
    +  private var position = chunkSize
    +
    +  def toArrays: Array[Array[Byte]] = {
    +    if (lastChunkIndex == -1) {
    +      new Array[Array[Byte]](0)
    +    } else {
    +      // Copy the first n-1 chunks to the output, and then create an array that fits the last chunk.
    +      // An alternative would have been returning an array of ByteBuffers, with the last buffer
    +      // bounded to only the last chunk's position. However, given our use case in Spark (to put
    +      // the chunks in block manager), only limiting the view bound of the buffer would still
    +      // require the block manager to store the whole chunk.
    +      val ret = new Array[Array[Byte]](chunks.size)
    +      for (i <- 0 until chunks.size - 1) {
    +        ret(i) = chunks(i)
    +      }
    +      if (position == chunkSize) {
    --- End diff --
    
    i will add some comment explaining position can never be 0


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-3135] Created ByteArrayChunkOutputStrea...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the pull request:

    https://github.com/apache/spark/pull/2054#issuecomment-52746053
  
      [QA tests have started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/18942/consoleFull) for   PR 2054 at commit [`ee88e73`](https://github.com/apache/spark/commit/ee88e7338c9d07da9a3e647eb4ba36302c29e3f6).
     * This patch merges cleanly.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-3135] Avoid extra mem copy in TorrentBr...

Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:

    https://github.com/apache/spark/pull/2054


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-3135] Avoid extra mem copy in TorrentBr...

Posted by mengxr <gi...@git.apache.org>.
Github user mengxr commented on a diff in the pull request:

    https://github.com/apache/spark/pull/2054#discussion_r16503290
  
    --- Diff: core/src/main/scala/org/apache/spark/broadcast/TorrentBroadcast.scala ---
    @@ -201,29 +202,13 @@ private object TorrentBroadcast extends Logging {
       }
     
       def blockifyObject[T: ClassTag](obj: T): Array[ByteBuffer] = {
    -    // TODO: Create a special ByteArrayOutputStream that splits the output directly into chunks
         // so we don't need to do the extra memory copy.
    --- End diff --
    
    forgot to remove this line


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-3135] Avoid extra mem copy in TorrentBr...

Posted by rxin <gi...@git.apache.org>.
Github user rxin commented on a diff in the pull request:

    https://github.com/apache/spark/pull/2054#discussion_r16463419
  
    --- Diff: core/src/main/scala/org/apache/spark/util/io/ByteArrayChunkOutputStream.scala ---
    @@ -0,0 +1,82 @@
    +/*
    + * 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.io
    +
    +import java.io.OutputStream
    +
    +import scala.collection.mutable.ArrayBuffer
    +
    +
    +/**
    + * An OutputStream that writes to fixed-size chunks of byte arrays.
    + */
    +private[spark]
    +class ByteArrayChunkOutputStream(chunkSize: Int) extends OutputStream {
    +
    +  private val chunks = new ArrayBuffer[Array[Byte]]
    +
    +  private var lastChunkIndex = -1
    +  private var position = chunkSize
    +
    +  @inline
    +  private def allocateNewChunkIfNeeded(): Unit = {
    +    if (position == chunkSize) {
    +      chunks += new Array[Byte](chunkSize)
    +      lastChunkIndex += 1
    +      position = 0
    +    }
    +  }
    +
    +  def toArrays: Array[Array[Byte]] = {
    +    if (lastChunkIndex == -1) {
    +      new Array[Array[Byte]](0)
    +    } else {
    +      val ret = new Array[Array[Byte]](chunks.size)
    +      for (i <- 0 until chunks.size - 1) {
    +        ret(i) = chunks(i)
    +      }
    +      if (position == chunkSize) {
    +        ret(lastChunkIndex) = chunks(lastChunkIndex)
    +      } else {
    +        ret(lastChunkIndex) = new Array[Byte](position)
    +        System.arraycopy(chunks(lastChunkIndex), 0, ret(lastChunkIndex), 0, position)
    --- End diff --
    
    note to self: add comment explaining why we don't return bytebuffers with limit set


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-3135] Avoid extra mem copy in TorrentBr...

Posted by aarondav <gi...@git.apache.org>.
Github user aarondav commented on a diff in the pull request:

    https://github.com/apache/spark/pull/2054#discussion_r16509300
  
    --- Diff: core/src/main/scala/org/apache/spark/util/io/ByteArrayChunkOutputStream.scala ---
    @@ -0,0 +1,87 @@
    +/*
    + * 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.io
    +
    +import java.io.OutputStream
    +
    +import scala.collection.mutable.ArrayBuffer
    +
    +
    +/**
    + * An OutputStream that writes to fixed-size chunks of byte arrays.
    + */
    +private[spark]
    +class ByteArrayChunkOutputStream(chunkSize: Int) extends OutputStream {
    +
    +  private val chunks = new ArrayBuffer[Array[Byte]]
    +
    +  private var lastChunkIndex = -1
    +  private var position = chunkSize
    +
    +  def toArrays: Array[Array[Byte]] = {
    --- End diff --
    
    nit: can we move this later in the class definition? Just from a "reading-the-code" standpoint, it shouldn't be first. Probably last.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-3135] Avoid extra mem copy in TorrentBr...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the pull request:

    https://github.com/apache/spark/pull/2054#issuecomment-52751016
  
      [QA tests have finished](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/18942/consoleFull) for   PR 2054 at commit [`ee88e73`](https://github.com/apache/spark/commit/ee88e7338c9d07da9a3e647eb4ba36302c29e3f6).
     * This patch **passes** unit tests.
     * This patch merges cleanly.
     * This patch adds the following public classes _(experimental)_:
      * `class ByteArrayChunkOutputStream(chunkSize: Int) extends OutputStream `



---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-3135] Avoid extra mem copy in TorrentBr...

Posted by rxin <gi...@git.apache.org>.
Github user rxin commented on the pull request:

    https://github.com/apache/spark/pull/2054#issuecomment-54106297
  
    Merging this in master. Thanks for looking at it.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-3135] Avoid extra mem copy in TorrentBr...

Posted by aarondav <gi...@git.apache.org>.
Github user aarondav commented on a diff in the pull request:

    https://github.com/apache/spark/pull/2054#discussion_r16509640
  
    --- Diff: core/src/main/scala/org/apache/spark/util/io/ByteArrayChunkOutputStream.scala ---
    @@ -0,0 +1,87 @@
    +/*
    + * 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.io
    +
    +import java.io.OutputStream
    +
    +import scala.collection.mutable.ArrayBuffer
    +
    +
    +/**
    + * An OutputStream that writes to fixed-size chunks of byte arrays.
    + */
    +private[spark]
    +class ByteArrayChunkOutputStream(chunkSize: Int) extends OutputStream {
    +
    +  private val chunks = new ArrayBuffer[Array[Byte]]
    +
    +  private var lastChunkIndex = -1
    +  private var position = chunkSize
    +
    +  def toArrays: Array[Array[Byte]] = {
    +    if (lastChunkIndex == -1) {
    +      new Array[Array[Byte]](0)
    +    } else {
    +      // Copy the first n-1 chunks to the output, and then create an array that fits the last chunk.
    +      // An alternative would have been returning an array of ByteBuffers, with the last buffer
    +      // bounded to only the last chunk's position. However, given our use case in Spark (to put
    +      // the chunks in block manager), only limiting the view bound of the buffer would still
    +      // require the block manager to store the whole chunk.
    +      val ret = new Array[Array[Byte]](chunks.size)
    +      for (i <- 0 until chunks.size - 1) {
    +        ret(i) = chunks(i)
    +      }
    +      if (position == chunkSize) {
    --- End diff --
    
    If position is 0, I think the correct behavior would be to not return the last allocated buffer, at least to be consistent with the base case. To note, there should not be a 0-position buffer with the current code, but nor should would it be incorrect if one existed in the future or due to an exception after allocateNewChunkIfNeeded().


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-3135] Avoid extra mem copy in TorrentBr...

Posted by aarondav <gi...@git.apache.org>.
Github user aarondav commented on a diff in the pull request:

    https://github.com/apache/spark/pull/2054#discussion_r16511704
  
    --- Diff: core/src/main/scala/org/apache/spark/util/io/ByteArrayChunkOutputStream.scala ---
    @@ -0,0 +1,87 @@
    +/*
    + * 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.io
    +
    +import java.io.OutputStream
    +
    +import scala.collection.mutable.ArrayBuffer
    +
    +
    +/**
    + * An OutputStream that writes to fixed-size chunks of byte arrays.
    + */
    +private[spark]
    +class ByteArrayChunkOutputStream(chunkSize: Int) extends OutputStream {
    +
    +  private val chunks = new ArrayBuffer[Array[Byte]]
    +
    +  private var lastChunkIndex = -1
    +  private var position = chunkSize
    +
    +  def toArrays: Array[Array[Byte]] = {
    +    if (lastChunkIndex == -1) {
    +      new Array[Array[Byte]](0)
    +    } else {
    +      // Copy the first n-1 chunks to the output, and then create an array that fits the last chunk.
    +      // An alternative would have been returning an array of ByteBuffers, with the last buffer
    +      // bounded to only the last chunk's position. However, given our use case in Spark (to put
    +      // the chunks in block manager), only limiting the view bound of the buffer would still
    +      // require the block manager to store the whole chunk.
    +      val ret = new Array[Array[Byte]](chunks.size)
    +      for (i <- 0 until chunks.size - 1) {
    +        ret(i) = chunks(i)
    +      }
    +      if (position == chunkSize) {
    +        ret(lastChunkIndex) = chunks(lastChunkIndex)
    +      } else {
    +        ret(lastChunkIndex) = new Array[Byte](position)
    +        System.arraycopy(chunks(lastChunkIndex), 0, ret(lastChunkIndex), 0, position)
    +      }
    +      ret
    +    }
    +  }
    +
    +  override def write(b: Int): Unit = {
    +    allocateNewChunkIfNeeded()
    +    chunks(lastChunkIndex)(position) = b.toByte
    +    position += 1
    +  }
    +
    +  override def write(bytes: Array[Byte]): Unit = write(bytes, 0, bytes.length)
    +
    +  override def write(bytes: Array[Byte], off: Int, len: Int): Unit = {
    +    var written = off
    --- End diff --
    
    That will never be remotely costly. I mean, you're also saving one register.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-3135] Avoid extra mem copy in TorrentBr...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the pull request:

    https://github.com/apache/spark/pull/2054#issuecomment-52870664
  
      [QA tests have finished](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/19028/consoleFull) for   PR 2054 at commit [`618d9c8`](https://github.com/apache/spark/commit/618d9c8f9c8a70e2ff0b5ed15e71400ce608001e).
     * This patch **passes** unit tests.
     * This patch merges cleanly.
     * This patch adds the following public classes _(experimental)_:
      * `class ByteArrayChunkOutputStream(chunkSize: Int) extends OutputStream `



---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org