-
-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
scalapb-runtime-grpc: optimize Marshaller for InProcessTransport (#1615)
* scalapb-runtime-grpc: optimize Marshaller for InProcessTransport * post-review: drop redundant check; fix 2.13 compilation * post-review: formatting * post-review: added unit test for ProtoInputStream * remove code duplication * post-review: format code
- Loading branch information
Showing
9 changed files
with
262 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
import com.thesamet.proto.e2e.service.SealedResponseMessage | ||
import org.scalatest.funspec.AnyFunSpec | ||
import org.scalatest.matchers.must.Matchers | ||
import scalapb.grpc.ProtoInputStream | ||
import com.thesamet.proto.e2e.service.Res1 | ||
|
||
import scala.util.Random | ||
|
||
class ProtoInputStreamSpec extends AnyFunSpec with Matchers { | ||
|
||
trait Setup { | ||
// message.serializedSize == 4 | ||
val message = SealedResponseMessage( | ||
SealedResponseMessage.SealedValue.Res1(Res1(42)) | ||
) | ||
val stream = new ProtoInputStream(message) | ||
def newBuffer = Array.fill[Byte](message.serializedSize * 2)(0) | ||
|
||
def fullyDrainStream() = { | ||
stream.read(newBuffer, 0, message.serializedSize + 1) | ||
} | ||
|
||
def partiallyDrainStream() = { | ||
stream.read(newBuffer, 0, message.serializedSize - 1) | ||
} | ||
} | ||
|
||
describe("#available()") { | ||
it("returns full length for a fresh stream") { | ||
new Setup { | ||
stream.available() must be(message.serializedSize) | ||
} | ||
} | ||
|
||
it("returns zero for drained stream") { | ||
new Setup { | ||
fullyDrainStream() | ||
|
||
stream.available() must be(0) | ||
} | ||
} | ||
|
||
it("returns remaining length for partially drained stream") { | ||
new Setup { | ||
partiallyDrainStream() | ||
|
||
stream.available() must be(1) | ||
} | ||
} | ||
} | ||
|
||
describe("#read(buffer, offset, length)") { | ||
|
||
it("returns -1 for a fully drained stream") { | ||
new Setup { | ||
fullyDrainStream() | ||
|
||
stream.read(newBuffer, 0, 10) must be(-1) | ||
} | ||
} | ||
|
||
it("returns requested length and fills the buffer") { | ||
new Setup { | ||
val buf1 = newBuffer | ||
stream.read(buf1, 0, 2) must be(2) | ||
buf1.take(2) must be(message.toByteArray.take(2)) | ||
} | ||
} | ||
|
||
it("fully readable in chunks") { | ||
new Setup { | ||
var offset = 0 | ||
var count = 0 | ||
var buf = newBuffer | ||
val res = Array.newBuilder[Byte] | ||
do { | ||
res ++= buf.slice(offset, offset + count) | ||
buf = newBuffer | ||
offset += count | ||
count = stream.read(buf, offset, Random.nextInt(3)) | ||
} while (count !== -1) | ||
|
||
res.result() must be(message.toByteArray) | ||
} | ||
} | ||
} | ||
|
||
describe("#read()") { | ||
|
||
it("returns bytes for a fresh stream") { | ||
new Setup { | ||
val bytes = message.toByteArray | ||
|
||
stream.read() must be(bytes(0)) | ||
stream.read() must be(bytes(1)) | ||
stream.read() must be(bytes(2)) | ||
} | ||
} | ||
|
||
it("returns -1 when fully drained") { | ||
new Setup { | ||
fullyDrainStream() | ||
|
||
stream.read() must be(-1) | ||
} | ||
} | ||
|
||
it("returns next byte when partially drained") { | ||
new Setup { | ||
partiallyDrainStream() | ||
|
||
stream.read() must be(message.toByteArray.last) | ||
} | ||
} | ||
} | ||
|
||
describe("#message") { | ||
it("returns the same instance passed in the constructor") { | ||
new Setup { | ||
stream.message must be theSameInstanceAs (message) | ||
} | ||
} | ||
|
||
it("throws when fully drained") { | ||
new Setup { | ||
fullyDrainStream() | ||
|
||
an[IllegalStateException] should be thrownBy stream.message | ||
} | ||
} | ||
|
||
it("throws when partially drained") { | ||
new Setup { | ||
partiallyDrainStream() | ||
|
||
an[IllegalStateException] should be thrownBy stream.message | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
scalapb-runtime-grpc/src/main/scala/scalapb/grpc/ProtoInputStream.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package scalapb.grpc | ||
|
||
import com.google.protobuf.CodedOutputStream | ||
import scalapb.GeneratedMessage | ||
|
||
import java.io.{ByteArrayInputStream, InputStream} | ||
|
||
/** Allows skipping serialization completely when the io.grpc.inprocess.InProcessTransport is used. | ||
* Inspired by | ||
* https://github.com/grpc/grpc-java/blob/master/protobuf-lite/src/main/java/io/grpc/protobuf/lite/ProtoInputStream.java | ||
*/ | ||
class ProtoInputStream[T <: GeneratedMessage](msg: T) extends InputStream { | ||
|
||
private var state: State = Message(msg) | ||
|
||
private sealed trait State { | ||
def message: T = throw new IllegalStateException("message not available") | ||
def available: Int | ||
def read(): Int | ||
def read(b: Array[Byte], off: Int, len: Int): Int | ||
} | ||
|
||
private object Drained extends State { | ||
override def available: Int = 0 | ||
override def read(): Int = -1 | ||
override def read(b: Array[Byte], off: Int, len: Int): Int = -1 | ||
} | ||
|
||
private case class Message(value: T) extends State { | ||
override def available: Int = value.serializedSize | ||
override def message: T = value | ||
override def read(): Int = toStream.read() | ||
override def read(b: Array[Byte], off: Int, len: Int): Int = { | ||
value.serializedSize match { | ||
case 0 => toDrained.read(b, off, len) | ||
case size if size <= len => | ||
val stream = CodedOutputStream.newInstance(b, off, size) | ||
message.writeTo(stream) | ||
stream.flush() | ||
stream.checkNoSpaceLeft() | ||
toDrained | ||
size | ||
case _ => toStream.read(b, off, len) | ||
} | ||
} | ||
private def toStream: State = { | ||
state = Stream(new ByteArrayInputStream(value.toByteArray)) | ||
state | ||
} | ||
private def toDrained: State = { | ||
state = Drained | ||
state | ||
} | ||
} | ||
|
||
private case class Stream(value: InputStream) extends State { | ||
override def available: Int = value.available() | ||
override def read(): Int = value.read() | ||
override def read(b: Array[Byte], off: Int, len: Int): Int = value.read(b, off, len) | ||
} | ||
|
||
override def read(): Int = state.read() | ||
override def read(b: Array[Byte], off: Int, len: Int): Int = state.read(b, off, len) | ||
override def available(): Int = state.available | ||
def message: T = state.message | ||
} |