Skip to content

Commit

Permalink
[split] scrooge-generator: trim some allocations from generated scala…
Browse files Browse the repository at this point in the history
… code

https://jira.twitter.biz/browse/HOSEBIRD-1550

This is a variety of small changes to the generated scala code
that removes some unnecessary object allocations.

RB_ID=262691
  • Loading branch information
kevinoliver authored and CI committed Jan 9, 2014
1 parent 08a5d46 commit 0d154a6
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 26 deletions.
16 changes: 13 additions & 3 deletions scrooge-generator/src/main/resources/scalagen/finagleClient.scala
@@ -1,11 +1,10 @@
package {{package}}

import com.twitter.finagle.SourcedException
import com.twitter.finagle.{SourcedException, Service => FinagleService}
import com.twitter.finagle.stats.{NullStatsReceiver, StatsReceiver}
import com.twitter.finagle.thrift.ThriftClientRequest
import com.twitter.finagle.{Service => FinagleService}
import com.twitter.scrooge.{ThriftStruct, ThriftStructCodec}
import com.twitter.util.Future
import com.twitter.util.{Future, Return, Throw}
import java.nio.ByteBuffer
import java.util.Arrays
import org.apache.thrift.protocol._
Expand Down Expand Up @@ -63,6 +62,17 @@ class {{ServiceName}}$FinagleClient(
)
}

protected def setServiceName(ex: Exception): Exception =
if (this.serviceName == "") ex
else {
ex match {
case se: SourcedException =>
se.serviceName = this.serviceName
se
case _ => ex
}
}

// ----- end boilerplate.

{{/hasParent}}
Expand Down
Expand Up @@ -4,33 +4,41 @@ private[this] object {{__stats_name}} {
val FailuresCounter = scopedStats.scope("{{clientFuncNameForWire}}").counter("failures")
val FailuresScope = scopedStats.scope("{{clientFuncNameForWire}}").scope("failures")
}

{{#headerInfo}}{{>header}}{{/headerInfo}} = {
{{__stats_name}}.RequestsCounter.incr()
this.service(encodeRequest("{{clientFuncNameForWire}}", {{ArgsStruct}}({{argNames}}))) flatMap { response =>
val result = decodeResponse(response, {{ResultStruct}})
val exception =
val exception: Future[Nothing] =
{{#hasThrows}}
({{#throws}}result.{{throwName}}{{/throws| orElse }}).map(Future.exception)
if (false)
null // can never happen, but needed to open a block
{{#throws}}
else if (result.{{throwName}}.isDefined)
Future.exception(setServiceName(result.{{throwName}}.get))
{{/throws}}
else
null
{{/hasThrows}}
{{^hasThrows}}
None
null
{{/hasThrows}}

{{#isVoid}}
exception.getOrElse(Future.Done)
if (exception != null) exception else Future.Done
{{/isVoid}}
{{^isVoid}}
exception.orElse(result.success.map(Future.value)).getOrElse(Future.exception(missingResult("{{clientFuncNameForWire}}")))
if (result.success.isDefined)
Future.value(result.success.get)
else if (exception != null)
exception
else
Future.exception(missingResult("{{clientFuncNameForWire}}"))
{{/isVoid}}
} rescue {
case ex: SourcedException => {
if (this.serviceName != "") { ex.serviceName = this.serviceName }
Future.exception(ex)
}
} onSuccess { _ =>
{{__stats_name}}.SuccessCounter.incr()
} onFailure { ex =>
{{__stats_name}}.FailuresCounter.incr()
{{__stats_name}}.FailuresScope.counter(ex.getClass.getName).incr()
} respond {
case Return(_) =>
{{__stats_name}}.SuccessCounter.incr()
case Throw(ex) =>
{{__stats_name}}.FailuresCounter.incr()
{{__stats_name}}.FailuresScope.counter(ex.getClass.getName).incr()
}
}
Expand Up @@ -64,11 +64,15 @@ class {{ServiceName}}$FinagleService(

try {
val msg = iprot.readMessageBegin()
functionMap.get(msg.name) map { _.apply(iprot, msg.seqid) } getOrElse {
TProtocolUtil.skip(iprot, TType.STRUCT)
exception(msg.name, msg.seqid, TApplicationException.UNKNOWN_METHOD,
"Invalid method name: '" + msg.name + "'")
}
val func = functionMap.get(msg.name)
func match {
case Some(fn) =>
fn(iprot, msg.seqid)
case _ =>
TProtocolUtil.skip(iprot, TType.STRUCT)
exception(msg.name, msg.seqid, TApplicationException.UNKNOWN_METHOD,
"Invalid method name: '" + msg.name + "'")
}
} catch {
case e: Exception => Future.exception(e)
}
Expand Down
Expand Up @@ -10,6 +10,7 @@ import org.apache.thrift.TApplicationException
import org.apache.thrift.transport.TMemoryBuffer
import scala.collection.immutable.{Map => immutable$Map}
import scala.collection.mutable.{
Builder,
ArrayBuffer => mutable$ArrayBuffer, Buffer => mutable$Buffer,
HashMap => mutable$HashMap, HashSet => mutable$HashSet}
import scala.collection.{Map, Set}
Expand Down
11 changes: 9 additions & 2 deletions scrooge-generator/src/main/resources/scalagen/struct.scala
Expand Up @@ -8,6 +8,7 @@ import org.apache.thrift.transport.{TMemoryBuffer, TTransport}
import java.nio.ByteBuffer
import java.util.Arrays
import scala.collection.immutable.{Map => immutable$Map}
import scala.collection.mutable.Builder
import scala.collection.mutable.{
ArrayBuffer => mutable$ArrayBuffer, Buffer => mutable$Buffer,
HashMap => mutable$HashMap, HashSet => mutable$HashSet}
Expand All @@ -16,6 +17,7 @@ import scala.collection.{Map, Set}
{{/public}}
{{docstring}}
object {{StructName}} extends ThriftStructCodec3[{{StructName}}] {
private val NoPassthroughFields = immutable$Map.empty[Short, TFieldBlob]
val Struct = new TStruct("{{StructNameForWire}}")
{{#fields}}
val {{fieldConst}} = new TField("{{fieldNameForWire}}", TType.{{constType}}, {{id}})
Expand Down Expand Up @@ -54,7 +56,7 @@ object {{StructName}} extends ThriftStructCodec3[{{StructName}}] {
{{/required}}
{{/optional}}
{{/fields}}
var _passthroughFields = immutable$Map.newBuilder[Short, TFieldBlob]
var _passthroughFields: Builder[(Short, TFieldBlob), immutable$Map[Short, TFieldBlob]] = null
var _done = false

_iprot.readStructBegin()
Expand All @@ -70,6 +72,8 @@ object {{StructName}} extends ThriftStructCodec3[{{StructName}}] {
{{>readField}}
{{/fields}}
case _ =>
if (_passthroughFields == null)
_passthroughFields = immutable$Map.newBuilder[Short, TFieldBlob]
_passthroughFields += (_field.id -> TFieldBlob.read(_field, _iprot))
_readField = true
}
Expand All @@ -90,7 +94,10 @@ object {{StructName}} extends ThriftStructCodec3[{{StructName}}] {
{{#fields}}
{{fieldName}},
{{/fields}}
_passthroughFields.result()
if (_passthroughFields == null)
NoPassthroughFields
else
_passthroughFields.result()
)
}

Expand Down

0 comments on commit 0d154a6

Please sign in to comment.