From 2c1dcb4e11c2ece2a70f04a216c4f32b3a5cecd8 Mon Sep 17 00:00:00 2001 From: Cameron Tew Date: Tue, 6 Oct 2020 11:37:18 -0700 Subject: [PATCH] Moves MessageFactory instantation to module-level private. Avoids costly redudant work when parsing FHIR json. PiperOrigin-RevId: 335684550 --- py/google/fhir/utils/proto_utils.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/py/google/fhir/utils/proto_utils.py b/py/google/fhir/utils/proto_utils.py index 64f5b3f1f..764d35571 100644 --- a/py/google/fhir/utils/proto_utils.py +++ b/py/google/fhir/utils/proto_utils.py @@ -20,6 +20,8 @@ from google.protobuf import message from google.protobuf import message_factory +_factory = message_factory.MessageFactory() + MessageOrDescriptorBase = Union[descriptor_pb2.DescriptorProto, message.Message, descriptor.DescriptorBase] @@ -270,8 +272,7 @@ def set_in_parent_or_add( def get_message_class_from_descriptor( desc: descriptor.Descriptor) -> Type[message.Message]: """Returns the class of the message type corresponding to the descriptor.""" - factory = message_factory.MessageFactory() - return factory.GetPrototype(desc) + return _factory.GetPrototype(desc) def create_message_from_descriptor(desc: descriptor.Descriptor,