diff options
author | Jisi Liu <liujisi@google.com> | 2017-03-06 16:48:54 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-03-06 16:48:54 -0800 |
commit | f6d8c833845b90f61b95234cd090ec6e70058d06 (patch) | |
tree | 19da828c022ecb2f1b331c4c2728340586b3b6c2 | |
parent | 06f9f609e166859a8ebe9d67a3fb95a825adbc12 (diff) | |
parent | 2c36cc30dd9e60f4172db6c55d552aebc692bea2 (diff) | |
download | protobuf-f6d8c833845b90f61b95234cd090ec6e70058d06.tar.gz protobuf-f6d8c833845b90f61b95234cd090ec6e70058d06.tar.bz2 protobuf-f6d8c833845b90f61b95234cd090ec6e70058d06.zip |
Merge pull request #2613 from aausch/fix_memory_leak
optimization and quick workaround to memory leak
-rwxr-xr-x | python/google/protobuf/reflection.py | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/python/google/protobuf/reflection.py b/python/google/protobuf/reflection.py index 51c83321..05bafd69 100755 --- a/python/google/protobuf/reflection.py +++ b/python/google/protobuf/reflection.py @@ -61,6 +61,8 @@ else: # Part of the public interface, but normally only used by message factories. GeneratedProtocolMessageType = message_impl.GeneratedProtocolMessageType +MESSAGE_CLASS_CACHE = {} + def ParseMessage(descriptor, byte_str): """Generate a new Message instance from this Descriptor and a byte string. @@ -104,11 +106,16 @@ def MakeClass(descriptor): Returns: The Message class object described by the descriptor. """ + if descriptor in MESSAGE_CLASS_CACHE: + return MESSAGE_CLASS_CACHE[descriptor] + attributes = {} for name, nested_type in descriptor.nested_types_by_name.items(): attributes[name] = MakeClass(nested_type) attributes[GeneratedProtocolMessageType._DESCRIPTOR_KEY] = descriptor - return GeneratedProtocolMessageType(str(descriptor.name), (message.Message,), + result = GeneratedProtocolMessageType(str(descriptor.name), (message.Message,), attributes) + MESSAGE_CLASS_CACHE[descriptor] = result + return result |