Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Rewind buffer in call cases
  • Loading branch information
harishreedharan committed Oct 3, 2014
commit 164bd14022d3fbd998d85810c5b8f845a9041fac
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ private[streaming] class WriteAheadLogWriter(path: String) extends Closeable {
private var closed = false
private val hflushMethod = {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would wrap this method in hflush stuff as a function from cleanliness, so that we dont have to do hflushMethod.foreach , just call flush()

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wrapped the call in a different method. I don't want to do a look up every time we call hflush. Doing that on initialization saves a bunch of reflection lookups. For the sake of keeping it a val, I wrapped the lookup in a different method, which is called at the time of initialization

try {
Some(classOf[FSDataOutputStream].getMethod("hflush", new Array[Class[Object]](0): _*))
Some(classOf[FSDataOutputStream].getMethod("hflush"))
} catch {
case e: Exception => None
}
Expand All @@ -38,14 +38,14 @@ private[streaming] class WriteAheadLogWriter(path: String) extends Closeable {
// - Data - of length = Length
def write(data: ByteBuffer): FileSegment = synchronized {
assertOpen()
data.rewind() // Rewind to ensure all data in the buffer is retrieved
val lengthToWrite = data.remaining()
val segment = new FileSegment(path, nextOffset, lengthToWrite)
stream.writeInt(lengthToWrite)
if (data.hasArray) {
stream.write(data.array())
} else {
// If the buffer is not backed by an array we need to copy the data to an array
data.rewind() // Rewind to ensure all data in the buffer is retrieved
val dataArray = new Array[Byte](lengthToWrite)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We dont want to copy the data multiple times! Why not just write off the data one byte at a time. That should be faster than doing two passes on the data, and allocating memory.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

      while(byteBuffer.hasRemaining) {
        stream.writeByte(byteBuffer.get())
      }

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, never mind. Spoke to my colleagues and writing byte at a time is slower than doing extra memcopy. So its fine as is.

data.get(dataArray)
stream.write(dataArray)
Expand Down