compiler: reduce synchronzed invocation (#2539)

not necessary to synchronze every time calling
getServiceDescriptor(), if the descriptor has been created already;

go with the double-checked locking idom
This commit is contained in:
ZHANG Dapeng 2016-12-29 12:21:04 -08:00 committed by GitHub
parent 4693492fda
commit 3d210ae875
15 changed files with 255 additions and 169 deletions

View File

@ -269,16 +269,22 @@ public class BenchmarkServiceGrpc {
} }
} }
private static io.grpc.ServiceDescriptor serviceDescriptor; private static volatile io.grpc.ServiceDescriptor serviceDescriptor;
public static synchronized io.grpc.ServiceDescriptor getServiceDescriptor() { public static io.grpc.ServiceDescriptor getServiceDescriptor() {
if (serviceDescriptor == null) { io.grpc.ServiceDescriptor result = serviceDescriptor;
serviceDescriptor = new io.grpc.ServiceDescriptor(SERVICE_NAME, if (result == null) {
new BenchmarkServiceDescriptorSupplier(), synchronized (BenchmarkServiceGrpc.class) {
METHOD_UNARY_CALL, result = serviceDescriptor;
METHOD_STREAMING_CALL); if (result == null) {
serviceDescriptor = result = new io.grpc.ServiceDescriptor(
SERVICE_NAME,
new BenchmarkServiceDescriptorSupplier(),
METHOD_UNARY_CALL,
METHOD_STREAMING_CALL);
}
}
} }
return result;
return serviceDescriptor;
} }
} }

View File

@ -387,18 +387,24 @@ public class WorkerServiceGrpc {
} }
} }
private static io.grpc.ServiceDescriptor serviceDescriptor; private static volatile io.grpc.ServiceDescriptor serviceDescriptor;
public static synchronized io.grpc.ServiceDescriptor getServiceDescriptor() { public static io.grpc.ServiceDescriptor getServiceDescriptor() {
if (serviceDescriptor == null) { io.grpc.ServiceDescriptor result = serviceDescriptor;
serviceDescriptor = new io.grpc.ServiceDescriptor(SERVICE_NAME, if (result == null) {
new WorkerServiceDescriptorSupplier(), synchronized (WorkerServiceGrpc.class) {
METHOD_RUN_SERVER, result = serviceDescriptor;
METHOD_RUN_CLIENT, if (result == null) {
METHOD_CORE_COUNT, serviceDescriptor = result = new io.grpc.ServiceDescriptor(
METHOD_QUIT_WORKER); SERVICE_NAME,
new WorkerServiceDescriptorSupplier(),
METHOD_RUN_SERVER,
METHOD_RUN_CLIENT,
METHOD_CORE_COUNT,
METHOD_QUIT_WORKER);
}
}
} }
return result;
return serviceDescriptor;
} }
} }

View File

@ -905,44 +905,40 @@ static void PrintGetServiceDescriptorMethod(const ServiceDescriptor* service,
p->Print(*vars, "}\n"); p->Print(*vars, "}\n");
p->Outdent(); p->Outdent();
p->Print(*vars, "}\n\n"); p->Print(*vars, "}\n\n");
p->Print(
*vars,
"private static $ServiceDescriptor$ serviceDescriptor;\n\n");
p->Print(
*vars,
"public static synchronized $ServiceDescriptor$ getServiceDescriptor() {\n");
p->Indent();
p->Print("if (serviceDescriptor == null) {\n");
p->Indent();
p->Print(
*vars,
"serviceDescriptor = new $ServiceDescriptor$(SERVICE_NAME,\n");
p->Indent();
p->Indent();
p->Print(
*vars,
"new $proto_descriptor_supplier$()");
p->Outdent();
p->Outdent();
} else {
p->Print(
*vars,
"private static $ServiceDescriptor$ serviceDescriptor;\n\n");
p->Print(
*vars,
"public static synchronized $ServiceDescriptor$ getServiceDescriptor() {\n");
p->Indent();
p->Print("if (serviceDescriptor == null) {\n");
p->Indent();
p->Print(
*vars,
"serviceDescriptor = new $ServiceDescriptor$(SERVICE_NAME");
} }
p->Print(
*vars,
"private static volatile $ServiceDescriptor$ serviceDescriptor;\n\n");
p->Print(
*vars,
"public static $ServiceDescriptor$ getServiceDescriptor() {\n");
p->Indent();
p->Print(
*vars,
"$ServiceDescriptor$ result = serviceDescriptor;\n");
p->Print("if (result == null) {\n");
p->Indent();
p->Print(
*vars,
"synchronized ($service_class_name$.class) {\n");
p->Indent();
p->Print("result = serviceDescriptor;\n");
p->Print("if (result == null) {\n");
p->Indent();
p->Print(
*vars,
"serviceDescriptor = result = new $ServiceDescriptor$(\n");
p->Indent(); p->Indent();
p->Indent(); p->Indent();
p->Print("SERVICE_NAME");
if (flavor == ProtoFlavor::NORMAL) {
p->Print(
*vars,
",\nnew $proto_descriptor_supplier$()");
}
for (int i = 0; i < service->method_count(); ++i) { for (int i = 0; i < service->method_count(); ++i) {
const MethodDescriptor* method = service->method(i); const MethodDescriptor* method = service->method(i);
(*vars)["method_field_name"] = MethodPropertiesFieldName(method); (*vars)["method_field_name"] = MethodPropertiesFieldName(method);
@ -951,8 +947,14 @@ static void PrintGetServiceDescriptorMethod(const ServiceDescriptor* service,
p->Print(");\n"); p->Print(");\n");
p->Outdent(); p->Outdent();
p->Outdent(); p->Outdent();
p->Outdent(); p->Outdent();
p->Print("}\n\nreturn serviceDescriptor;\n"); p->Print("}\n");
p->Outdent();
p->Print("}\n");
p->Outdent();
p->Print("}\n");
p->Print("return result;\n");
p->Outdent(); p->Outdent();
p->Print("}\n"); p->Print("}\n");
} }

View File

@ -432,19 +432,25 @@ public class TestServiceGrpc {
} }
} }
private static io.grpc.ServiceDescriptor serviceDescriptor; private static volatile io.grpc.ServiceDescriptor serviceDescriptor;
public static synchronized io.grpc.ServiceDescriptor getServiceDescriptor() { public static io.grpc.ServiceDescriptor getServiceDescriptor() {
if (serviceDescriptor == null) { io.grpc.ServiceDescriptor result = serviceDescriptor;
serviceDescriptor = new io.grpc.ServiceDescriptor(SERVICE_NAME, if (result == null) {
new TestServiceDescriptorSupplier(), synchronized (TestServiceGrpc.class) {
METHOD_UNARY_CALL, result = serviceDescriptor;
METHOD_STREAMING_OUTPUT_CALL, if (result == null) {
METHOD_STREAMING_INPUT_CALL, serviceDescriptor = result = new io.grpc.ServiceDescriptor(
METHOD_FULL_BIDI_CALL, SERVICE_NAME,
METHOD_HALF_BIDI_CALL); new TestServiceDescriptorSupplier(),
METHOD_UNARY_CALL,
METHOD_STREAMING_OUTPUT_CALL,
METHOD_STREAMING_INPUT_CALL,
METHOD_FULL_BIDI_CALL,
METHOD_HALF_BIDI_CALL);
}
}
} }
return result;
return serviceDescriptor;
} }
} }

View File

@ -425,18 +425,24 @@ public class TestServiceGrpc {
} }
} }
private static io.grpc.ServiceDescriptor serviceDescriptor; private static volatile io.grpc.ServiceDescriptor serviceDescriptor;
public static synchronized io.grpc.ServiceDescriptor getServiceDescriptor() { public static io.grpc.ServiceDescriptor getServiceDescriptor() {
if (serviceDescriptor == null) { io.grpc.ServiceDescriptor result = serviceDescriptor;
serviceDescriptor = new io.grpc.ServiceDescriptor(SERVICE_NAME, if (result == null) {
METHOD_UNARY_CALL, synchronized (TestServiceGrpc.class) {
METHOD_STREAMING_OUTPUT_CALL, result = serviceDescriptor;
METHOD_STREAMING_INPUT_CALL, if (result == null) {
METHOD_FULL_BIDI_CALL, serviceDescriptor = result = new io.grpc.ServiceDescriptor(
METHOD_HALF_BIDI_CALL); SERVICE_NAME,
METHOD_UNARY_CALL,
METHOD_STREAMING_OUTPUT_CALL,
METHOD_STREAMING_INPUT_CALL,
METHOD_FULL_BIDI_CALL,
METHOD_HALF_BIDI_CALL);
}
}
} }
return result;
return serviceDescriptor;
} }
} }

View File

@ -503,18 +503,24 @@ public class TestServiceGrpc {
} }
} }
private static io.grpc.ServiceDescriptor serviceDescriptor; private static volatile io.grpc.ServiceDescriptor serviceDescriptor;
public static synchronized io.grpc.ServiceDescriptor getServiceDescriptor() { public static io.grpc.ServiceDescriptor getServiceDescriptor() {
if (serviceDescriptor == null) { io.grpc.ServiceDescriptor result = serviceDescriptor;
serviceDescriptor = new io.grpc.ServiceDescriptor(SERVICE_NAME, if (result == null) {
METHOD_UNARY_CALL, synchronized (TestServiceGrpc.class) {
METHOD_STREAMING_OUTPUT_CALL, result = serviceDescriptor;
METHOD_STREAMING_INPUT_CALL, if (result == null) {
METHOD_FULL_BIDI_CALL, serviceDescriptor = result = new io.grpc.ServiceDescriptor(
METHOD_HALF_BIDI_CALL); SERVICE_NAME,
METHOD_UNARY_CALL,
METHOD_STREAMING_OUTPUT_CALL,
METHOD_STREAMING_INPUT_CALL,
METHOD_FULL_BIDI_CALL,
METHOD_HALF_BIDI_CALL);
}
}
} }
return result;
return serviceDescriptor;
} }
} }

View File

@ -200,15 +200,21 @@ public class LoadBalancerGrpc {
} }
} }
private static io.grpc.ServiceDescriptor serviceDescriptor; private static volatile io.grpc.ServiceDescriptor serviceDescriptor;
public static synchronized io.grpc.ServiceDescriptor getServiceDescriptor() { public static io.grpc.ServiceDescriptor getServiceDescriptor() {
if (serviceDescriptor == null) { io.grpc.ServiceDescriptor result = serviceDescriptor;
serviceDescriptor = new io.grpc.ServiceDescriptor(SERVICE_NAME, if (result == null) {
new LoadBalancerDescriptorSupplier(), synchronized (LoadBalancerGrpc.class) {
METHOD_BALANCE_LOAD); result = serviceDescriptor;
if (result == null) {
serviceDescriptor = result = new io.grpc.ServiceDescriptor(
SERVICE_NAME,
new LoadBalancerDescriptorSupplier(),
METHOD_BALANCE_LOAD);
}
}
} }
return result;
return serviceDescriptor;
} }
} }

View File

@ -210,15 +210,21 @@ public class HealthGrpc {
} }
} }
private static io.grpc.ServiceDescriptor serviceDescriptor; private static volatile io.grpc.ServiceDescriptor serviceDescriptor;
public static synchronized io.grpc.ServiceDescriptor getServiceDescriptor() { public static io.grpc.ServiceDescriptor getServiceDescriptor() {
if (serviceDescriptor == null) { io.grpc.ServiceDescriptor result = serviceDescriptor;
serviceDescriptor = new io.grpc.ServiceDescriptor(SERVICE_NAME, if (result == null) {
new HealthDescriptorSupplier(), synchronized (HealthGrpc.class) {
METHOD_CHECK); result = serviceDescriptor;
if (result == null) {
serviceDescriptor = result = new io.grpc.ServiceDescriptor(
SERVICE_NAME,
new HealthDescriptorSupplier(),
METHOD_CHECK);
}
}
} }
return result;
return serviceDescriptor;
} }
} }

View File

@ -202,15 +202,21 @@ public class ServerReflectionGrpc {
} }
} }
private static io.grpc.ServiceDescriptor serviceDescriptor; private static volatile io.grpc.ServiceDescriptor serviceDescriptor;
public static synchronized io.grpc.ServiceDescriptor getServiceDescriptor() { public static io.grpc.ServiceDescriptor getServiceDescriptor() {
if (serviceDescriptor == null) { io.grpc.ServiceDescriptor result = serviceDescriptor;
serviceDescriptor = new io.grpc.ServiceDescriptor(SERVICE_NAME, if (result == null) {
new ServerReflectionDescriptorSupplier(), synchronized (ServerReflectionGrpc.class) {
METHOD_SERVER_REFLECTION_INFO); result = serviceDescriptor;
if (result == null) {
serviceDescriptor = result = new io.grpc.ServiceDescriptor(
SERVICE_NAME,
new ServerReflectionDescriptorSupplier(),
METHOD_SERVER_REFLECTION_INFO);
}
}
} }
return result;
return serviceDescriptor;
} }
} }

View File

@ -210,15 +210,21 @@ public class DynamicServiceGrpc {
} }
} }
private static io.grpc.ServiceDescriptor serviceDescriptor; private static volatile io.grpc.ServiceDescriptor serviceDescriptor;
public static synchronized io.grpc.ServiceDescriptor getServiceDescriptor() { public static io.grpc.ServiceDescriptor getServiceDescriptor() {
if (serviceDescriptor == null) { io.grpc.ServiceDescriptor result = serviceDescriptor;
serviceDescriptor = new io.grpc.ServiceDescriptor(SERVICE_NAME, if (result == null) {
new DynamicServiceDescriptorSupplier(), synchronized (DynamicServiceGrpc.class) {
METHOD_METHOD); result = serviceDescriptor;
if (result == null) {
serviceDescriptor = result = new io.grpc.ServiceDescriptor(
SERVICE_NAME,
new DynamicServiceDescriptorSupplier(),
METHOD_METHOD);
}
}
} }
return result;
return serviceDescriptor;
} }
} }

View File

@ -210,15 +210,21 @@ public class ReflectableServiceGrpc {
} }
} }
private static io.grpc.ServiceDescriptor serviceDescriptor; private static volatile io.grpc.ServiceDescriptor serviceDescriptor;
public static synchronized io.grpc.ServiceDescriptor getServiceDescriptor() { public static io.grpc.ServiceDescriptor getServiceDescriptor() {
if (serviceDescriptor == null) { io.grpc.ServiceDescriptor result = serviceDescriptor;
serviceDescriptor = new io.grpc.ServiceDescriptor(SERVICE_NAME, if (result == null) {
new ReflectableServiceDescriptorSupplier(), synchronized (ReflectableServiceGrpc.class) {
METHOD_METHOD); result = serviceDescriptor;
if (result == null) {
serviceDescriptor = result = new io.grpc.ServiceDescriptor(
SERVICE_NAME,
new ReflectableServiceDescriptorSupplier(),
METHOD_METHOD);
}
}
} }
return result;
return serviceDescriptor;
} }
} }

View File

@ -278,16 +278,22 @@ public class MetricsServiceGrpc {
} }
} }
private static io.grpc.ServiceDescriptor serviceDescriptor; private static volatile io.grpc.ServiceDescriptor serviceDescriptor;
public static synchronized io.grpc.ServiceDescriptor getServiceDescriptor() { public static io.grpc.ServiceDescriptor getServiceDescriptor() {
if (serviceDescriptor == null) { io.grpc.ServiceDescriptor result = serviceDescriptor;
serviceDescriptor = new io.grpc.ServiceDescriptor(SERVICE_NAME, if (result == null) {
new MetricsServiceDescriptorSupplier(), synchronized (MetricsServiceGrpc.class) {
METHOD_GET_ALL_GAUGES, result = serviceDescriptor;
METHOD_GET_GAUGE); if (result == null) {
serviceDescriptor = result = new io.grpc.ServiceDescriptor(
SERVICE_NAME,
new MetricsServiceDescriptorSupplier(),
METHOD_GET_ALL_GAUGES,
METHOD_GET_GAUGE);
}
}
} }
return result;
return serviceDescriptor;
} }
} }

View File

@ -276,16 +276,22 @@ public class ReconnectServiceGrpc {
} }
} }
private static io.grpc.ServiceDescriptor serviceDescriptor; private static volatile io.grpc.ServiceDescriptor serviceDescriptor;
public static synchronized io.grpc.ServiceDescriptor getServiceDescriptor() { public static io.grpc.ServiceDescriptor getServiceDescriptor() {
if (serviceDescriptor == null) { io.grpc.ServiceDescriptor result = serviceDescriptor;
serviceDescriptor = new io.grpc.ServiceDescriptor(SERVICE_NAME, if (result == null) {
new ReconnectServiceDescriptorSupplier(), synchronized (ReconnectServiceGrpc.class) {
METHOD_START, result = serviceDescriptor;
METHOD_STOP); if (result == null) {
serviceDescriptor = result = new io.grpc.ServiceDescriptor(
SERVICE_NAME,
new ReconnectServiceDescriptorSupplier(),
METHOD_START,
METHOD_STOP);
}
}
} }
return result;
return serviceDescriptor;
} }
} }

View File

@ -563,21 +563,27 @@ public class TestServiceGrpc {
} }
} }
private static io.grpc.ServiceDescriptor serviceDescriptor; private static volatile io.grpc.ServiceDescriptor serviceDescriptor;
public static synchronized io.grpc.ServiceDescriptor getServiceDescriptor() { public static io.grpc.ServiceDescriptor getServiceDescriptor() {
if (serviceDescriptor == null) { io.grpc.ServiceDescriptor result = serviceDescriptor;
serviceDescriptor = new io.grpc.ServiceDescriptor(SERVICE_NAME, if (result == null) {
new TestServiceDescriptorSupplier(), synchronized (TestServiceGrpc.class) {
METHOD_EMPTY_CALL, result = serviceDescriptor;
METHOD_UNARY_CALL, if (result == null) {
METHOD_STREAMING_OUTPUT_CALL, serviceDescriptor = result = new io.grpc.ServiceDescriptor(
METHOD_STREAMING_INPUT_CALL, SERVICE_NAME,
METHOD_FULL_DUPLEX_CALL, new TestServiceDescriptorSupplier(),
METHOD_HALF_DUPLEX_CALL, METHOD_EMPTY_CALL,
METHOD_UNIMPLEMENTED_CALL); METHOD_UNARY_CALL,
METHOD_STREAMING_OUTPUT_CALL,
METHOD_STREAMING_INPUT_CALL,
METHOD_FULL_DUPLEX_CALL,
METHOD_HALF_DUPLEX_CALL,
METHOD_UNIMPLEMENTED_CALL);
}
}
} }
return result;
return serviceDescriptor;
} }
} }

View File

@ -242,15 +242,21 @@ public class UnimplementedServiceGrpc {
} }
} }
private static io.grpc.ServiceDescriptor serviceDescriptor; private static volatile io.grpc.ServiceDescriptor serviceDescriptor;
public static synchronized io.grpc.ServiceDescriptor getServiceDescriptor() { public static io.grpc.ServiceDescriptor getServiceDescriptor() {
if (serviceDescriptor == null) { io.grpc.ServiceDescriptor result = serviceDescriptor;
serviceDescriptor = new io.grpc.ServiceDescriptor(SERVICE_NAME, if (result == null) {
new UnimplementedServiceDescriptorSupplier(), synchronized (UnimplementedServiceGrpc.class) {
METHOD_UNIMPLEMENTED_CALL); result = serviceDescriptor;
if (result == null) {
serviceDescriptor = result = new io.grpc.ServiceDescriptor(
SERVICE_NAME,
new UnimplementedServiceDescriptorSupplier(),
METHOD_UNIMPLEMENTED_CALL);
}
}
} }
return result;
return serviceDescriptor;
} }
} }