Permalink
Browse files

scrooge: Use reusable buffer in generated java code

Summary: Problem/Solution:

Change the TMemoryBuffer to scrooge-core's TReusableBuffer for generated java code.

JIRA Issues: CSL-4346

Differential Revision: https://phabricator.twitter.biz/D60406
  • Loading branch information...
yufangong authored and jenkins committed Jun 15, 2017
1 parent fdb7264 commit 4cd9ee7017d75cec068f5acf14b97bc2955474ec
View
@@ -4,6 +4,9 @@
4.x
-----
- scrooge-generator: Generated Java code now is using `c.t.s.TReusableBuffer` to reduce
object allocations. This in turn adds `scrooge-core` as dependency for generated
java code. ``PHAB_ID=D60406``
4.18.0
------
@@ -26,6 +26,8 @@ import org.apache.thrift.meta_data.*;
import org.apache.thrift.transport.*;
import org.apache.thrift.protocol.*;
import com.twitter.scrooge.TReusableBuffer;
import com.twitter.scrooge.TReusableMemoryTransport;
import com.twitter.util.Future;
import com.twitter.util.Function;
import com.twitter.util.Function2;
@@ -159,6 +161,7 @@ public class OtherService {
public static class ServiceToClient extends com.twitter.thrift.TestServiceParent.ServiceToClient implements ServiceIface {
private final com.twitter.finagle.Service<ThriftClientRequest, byte[]> service;
private final TProtocolFactory protocolFactory;
private final TReusableBuffer tlReusableBuffer = new TReusableBuffer(512, 16 * 1024);
private final scala.PartialFunction<com.twitter.finagle.service.ReqRep,com.twitter.finagle.service.ResponseClass> responseClassifier;
public ServiceToClient(com.twitter.finagle.Service<ThriftClientRequest, byte[]> service, TProtocolFactory protocolFactory, scala.PartialFunction<com.twitter.finagle.service.ReqRep,com.twitter.finagle.service.ResponseClass> responseClassifier) {
super(service, protocolFactory, responseClassifier);
@@ -176,8 +179,7 @@ public class OtherService {
public Future<Boolean> test_endpoint(int req) {
try {
// TODO: size
TMemoryBuffer __memoryTransport__ = new TMemoryBuffer(512);
TReusableMemoryTransport __memoryTransport__ = tlReusableBuffer.get();
TProtocol __prot__ = this.protocolFactory.getProtocol(__memoryTransport__);
__prot__.writeMessageBegin(new TMessage("test_endpoint", TMessageType.CALL, 0));
test_endpoint_args __args__ = new test_endpoint_args();
@@ -223,6 +225,8 @@ public class OtherService {
});
} catch (TException e) {
return Future.exception(e);
} finally {
tlReusableBuffer.reset();
}
}
}
@@ -286,6 +290,7 @@ public class OtherService {
public static class Service extends com.twitter.thrift.TestServiceParent.Service {
private final ServiceIface iface;
private final TProtocolFactory protocolFactory;
private final TReusableBuffer tlReusableBuffer = new TReusableBuffer(512, 16 * 1024);
public Service(final ServiceIface iface, final TProtocolFactory protocolFactory) {
super(iface, protocolFactory);
this.iface = iface;
@@ -299,7 +304,7 @@ public class OtherService {
try {
iprot.readMessageEnd();
TApplicationException x = new TApplicationException(TApplicationException.PROTOCOL_ERROR, e.getMessage());
TMemoryBuffer memoryBuffer = new TMemoryBuffer(512);
TReusableMemoryTransport memoryBuffer = tlReusableBuffer.get();
TProtocol oprot = protocolFactory.getProtocol(memoryBuffer);
oprot.writeMessageBegin(new TMessage("test_endpoint", TMessageType.EXCEPTION, seqid));
@@ -310,6 +315,8 @@ public class OtherService {
return Future.value(buffer);
} catch (Exception e1) {
return Future.exception(e1);
} finally {
tlReusableBuffer.reset();
}
} catch (Exception e) {
return Future.exception(e);
@@ -335,7 +342,7 @@ public class OtherService {
result.setSuccessIsSet(true);
try {
TMemoryBuffer memoryBuffer = new TMemoryBuffer(512);
TReusableMemoryTransport memoryBuffer = tlReusableBuffer.get();
TProtocol oprot = protocolFactory.getProtocol(memoryBuffer);
oprot.writeMessageBegin(new TMessage("test_endpoint", TMessageType.REPLY, seqid));
@@ -345,6 +352,8 @@ public class OtherService {
return Future.value(Arrays.copyOfRange(memoryBuffer.getArray(), 0, memoryBuffer.length()));
} catch (Exception e) {
return Future.exception(e);
} finally {
tlReusableBuffer.reset();
}
}
}).rescue(new Function<Throwable, Future<byte[]>>() {
@@ -376,7 +385,7 @@ public class OtherService {
TProtocolUtil.skip(iprot, TType.STRUCT);
iprot.readMessageEnd();
TApplicationException x = new TApplicationException(TApplicationException.UNKNOWN_METHOD, "Invalid method name: '"+msg.name+"'");
TMemoryBuffer memoryBuffer = new TMemoryBuffer(512);
TReusableMemoryTransport memoryBuffer = tlReusableBuffer.get();
TProtocol oprot = protocolFactory.getProtocol(memoryBuffer);
oprot.writeMessageBegin(new TMessage(msg.name, TMessageType.EXCEPTION, msg.seqid));
x.write(oprot);
@@ -385,6 +394,8 @@ public class OtherService {
return Future.value(Arrays.copyOfRange(memoryBuffer.getArray(), 0, memoryBuffer.length()));
} catch (Exception e) {
return Future.exception(e);
} finally {
tlReusableBuffer.reset();
}
}
@@ -26,6 +26,8 @@ import org.apache.thrift.meta_data.*;
import org.apache.thrift.transport.*;
import org.apache.thrift.protocol.*;
import com.twitter.scrooge.TReusableBuffer;
import com.twitter.scrooge.TReusableMemoryTransport;
import com.twitter.util.Future;
import com.twitter.util.Function;
import com.twitter.util.Function2;
@@ -209,6 +211,7 @@ public class TestService {
public static class ServiceToClient extends TestServiceParent.ServiceToClient implements ServiceIface {
private final com.twitter.finagle.Service<ThriftClientRequest, byte[]> service;
private final TProtocolFactory protocolFactory;
private final TReusableBuffer tlReusableBuffer = new TReusableBuffer(512, 16 * 1024);
private final scala.PartialFunction<com.twitter.finagle.service.ReqRep,com.twitter.finagle.service.ResponseClass> responseClassifier;
public ServiceToClient(com.twitter.finagle.Service<ThriftClientRequest, byte[]> service, TProtocolFactory protocolFactory, scala.PartialFunction<com.twitter.finagle.service.ReqRep,com.twitter.finagle.service.ResponseClass> responseClassifier) {
@@ -227,8 +230,7 @@ public class TestService {
public Future<Boolean> test_endpoint(TestRequest request) {
try {
// TODO: size
TMemoryBuffer __memoryTransport__ = new TMemoryBuffer(512);
TReusableMemoryTransport __memoryTransport__ = tlReusableBuffer.get();
TProtocol __prot__ = this.protocolFactory.getProtocol(__memoryTransport__);
__prot__.writeMessageBegin(new TMessage("test_endpoint", TMessageType.CALL, 0));
test_endpoint_args __args__ = new test_endpoint_args();
@@ -275,12 +277,13 @@ public class TestService {
});
} catch (TException e) {
return Future.exception(e);
} finally {
tlReusableBuffer.reset();
}
}
public Future<Void> test_oneway(TestRequest request) {
try {
// TODO: size
TMemoryBuffer __memoryTransport__ = new TMemoryBuffer(512);
TReusableMemoryTransport __memoryTransport__ = tlReusableBuffer.get();
TProtocol __prot__ = this.protocolFactory.getProtocol(__memoryTransport__);
__prot__.writeMessageBegin(new TMessage("test_oneway", TMessageType.CALL, 0));
test_oneway_args __args__ = new test_oneway_args();
@@ -305,6 +308,8 @@ public class TestService {
});
} catch (TException e) {
return Future.exception(e);
} finally {
tlReusableBuffer.reset();
}
}
}
@@ -402,6 +407,7 @@ public class TestService {
public static class Service extends TestServiceParent.Service {
private final ServiceIface iface;
private final TProtocolFactory protocolFactory;
private final TReusableBuffer tlReusableBuffer = new TReusableBuffer(512, 16 * 1024);
public Service(final ServiceIface iface, final TProtocolFactory protocolFactory) {
super(iface, protocolFactory);
this.iface = iface;
@@ -415,7 +421,7 @@ public class TestService {
try {
iprot.readMessageEnd();
TApplicationException x = new TApplicationException(TApplicationException.PROTOCOL_ERROR, e.getMessage());
TMemoryBuffer memoryBuffer = new TMemoryBuffer(512);
TReusableMemoryTransport memoryBuffer = tlReusableBuffer.get();
TProtocol oprot = protocolFactory.getProtocol(memoryBuffer);
oprot.writeMessageBegin(new TMessage("test_endpoint", TMessageType.EXCEPTION, seqid));
@@ -426,6 +432,8 @@ public class TestService {
return Future.value(buffer);
} catch (Exception e1) {
return Future.exception(e1);
} finally {
tlReusableBuffer.reset();
}
} catch (Exception e) {
return Future.exception(e);
@@ -451,7 +459,7 @@ public class TestService {
result.setSuccessIsSet(true);
try {
TMemoryBuffer memoryBuffer = new TMemoryBuffer(512);
TReusableMemoryTransport memoryBuffer = tlReusableBuffer.get();
TProtocol oprot = protocolFactory.getProtocol(memoryBuffer);
oprot.writeMessageBegin(new TMessage("test_endpoint", TMessageType.REPLY, seqid));
@@ -461,6 +469,8 @@ public class TestService {
return Future.value(Arrays.copyOfRange(memoryBuffer.getArray(), 0, memoryBuffer.length()));
} catch (Exception e) {
return Future.exception(e);
} finally {
tlReusableBuffer.reset();
}
}
}).rescue(new Function<Throwable, Future<byte[]>>() {
@@ -473,7 +483,7 @@ public class TestService {
else {
return Future.exception(t);
}
TMemoryBuffer memoryBuffer = new TMemoryBuffer(512);
TReusableMemoryTransport memoryBuffer = tlReusableBuffer.get();
TProtocol oprot = protocolFactory.getProtocol(memoryBuffer);
oprot.writeMessageBegin(new TMessage("test_endpoint", TMessageType.REPLY, seqid));
result.write(oprot);
@@ -482,6 +492,8 @@ public class TestService {
return Future.value(Arrays.copyOfRange(memoryBuffer.getArray(), 0, memoryBuffer.length()));
} catch (Exception e) {
return Future.exception(e);
} finally {
tlReusableBuffer.reset();
}
}
});
@@ -499,7 +511,7 @@ public class TestService {
try {
iprot.readMessageEnd();
TApplicationException x = new TApplicationException(TApplicationException.PROTOCOL_ERROR, e.getMessage());
TMemoryBuffer memoryBuffer = new TMemoryBuffer(512);
TReusableMemoryTransport memoryBuffer = tlReusableBuffer.get();
TProtocol oprot = protocolFactory.getProtocol(memoryBuffer);
oprot.writeMessageBegin(new TMessage("test_oneway", TMessageType.EXCEPTION, seqid));
@@ -510,6 +522,8 @@ public class TestService {
return Future.value(buffer);
} catch (Exception e1) {
return Future.exception(e1);
} finally {
tlReusableBuffer.reset();
}
} catch (Exception e) {
return Future.exception(e);
@@ -553,7 +567,7 @@ public class TestService {
TProtocolUtil.skip(iprot, TType.STRUCT);
iprot.readMessageEnd();
TApplicationException x = new TApplicationException(TApplicationException.UNKNOWN_METHOD, "Invalid method name: '"+msg.name+"'");
TMemoryBuffer memoryBuffer = new TMemoryBuffer(512);
TReusableMemoryTransport memoryBuffer = tlReusableBuffer.get();
TProtocol oprot = protocolFactory.getProtocol(memoryBuffer);
oprot.writeMessageBegin(new TMessage(msg.name, TMessageType.EXCEPTION, msg.seqid));
x.write(oprot);
@@ -562,6 +576,8 @@ public class TestService {
return Future.value(Arrays.copyOfRange(memoryBuffer.getArray(), 0, memoryBuffer.length()));
} catch (Exception e) {
return Future.exception(e);
} finally {
tlReusableBuffer.reset();
}
}
Oops, something went wrong.

0 comments on commit 4cd9ee7

Please sign in to comment.