dep-protobuf/python/google/protobuf/internal
Tony Liao 9d98477ca9 Optimize pure Python parse path for custom JSON enum names.
This implementation is similar to our Java idea in cl/949670944,
basically the idea is that we will have a local map (dictionary) that
will be populated the very first time we try to parse into an enum
field. The lifetime of this dictionary will be tied to the Parser
instance, similar to the Java implementation.

BENCHMARKS (baseline stats are recorded at cl/952170250):

Python:
```
[BENCHMARK] ParseJsonDefault:                med 10.06 us/op | p99 11.22 us/op | mean 10.10 ± 0.20 us/op
[BENCHMARK] ParseJsonCustom:                 med 20.33 us/op | p99 22.23 us/op | mean 20.40 ± 0.34 us/op
[BENCHMARK] ParseJsonUnknownIgnored:         med 24.21 us/op | p99 26.25 us/op | mean 24.31 ± 0.44 us/op
[BENCHMARK] ParseRepeatedJsonDefault:        med 1.94 us/item | p99 2.05 us/item | mean 1.95 ± 0.03 us/item
[BENCHMARK] ParseRepeatedJsonCustom:         med 2.26 us/item | p99 2.45 us/item | mean 2.27 ± 0.03 us/item
[BENCHMARK] ParseRepeatedJsonUnknownIgnored: med 5.31 us/item | p99 5.52 us/item | mean 5.32 ± 0.05 us/item
```

Cpp:
```
[BENCHMARK] ParseJsonDefault:                med 8.66 us/op | p99 9.21 us/op | mean 8.70 ± 0.13 us/op
[BENCHMARK] ParseJsonCustom:                 med 26.33 us/op | p99 29.71 us/op | mean 26.44 ± 0.69 us/op
[BENCHMARK] ParseJsonUnknownIgnored:         med 28.46 us/op | p99 31.80 us/op | mean 28.59 ± 0.67 us/op
[BENCHMARK] ParseRepeatedJsonDefault:        med 1.72 us/item | p99 2.19 us/item | mean 1.74 ± 0.06 us/item
[BENCHMARK] ParseRepeatedJsonCustom:         med 2.12 us/item | p99 2.38 us/item | mean 2.13 ± 0.04 us/item
[BENCHMARK] ParseRepeatedJsonUnknownIgnored: med 4.61 us/item | p99 4.92 us/item | mean 4.62 ± 0.07 us/item
```

UPB:
```
[BENCHMARK] ParseJsonDefault:                med 11.22 us/op | p99 12.29 us/op | mean 11.27 ± 0.21 us/op
[BENCHMARK] ParseJsonCustom:                 med 37.54 us/op | p99 39.25 us/op | mean 37.65 ± 0.46 us/op
[BENCHMARK] ParseJsonUnknownIgnored:         med 41.18 us/op | p99 48.45 us/op | mean 41.30 ± 0.89 us/op
[BENCHMARK] ParseRepeatedJsonDefault:        med 2.10 us/item | p99 2.19 us/item | mean 2.10 ± 0.02 us/item
[BENCHMARK] ParseRepeatedJsonCustom:         med 2.58 us/item | p99 2.73 us/item | mean 2.59 ± 0.03 us/item
[BENCHMARK] ParseRepeatedJsonUnknownIgnored: med 5.70 us/item | p99 5.90 us/item | mean 5.71 ± 0.04 us/item
```

Seeing these benchmarks, we can see a very noticeable improvement in
`ParseRepeatedJsonCustom` and `ParseRepeatedJsonUnknownIgnored` cases --
which correspond to the pathological cases that we are worried about.
Singular fields are taking longer to parse in our microbenchmark because
the cache that we've added is tied to each Parser instance and must be
re-instantiated across every `Parse` call.

PiperOrigin-RevId: 970528683
2026-08-25 07:09:41 -07:00
..
import_test_package Shorten our license headers into an abbreviated form that references LICENSE instead of including it in full. 2023-09-08 17:49:49 -07:00
numpy Internal change. 2026-07-01 12:07:16 -07:00
__init__.py Auto-format Py Proto python files. 2026-04-29 13:40:58 -07:00
allocation_count.py Check return values and handle alloc failures in python 2026-07-27 22:11:16 -07:00
any_test.py Add constructing unpack routine to Python Protobuf Any API. 2025-02-27 16:44:02 -08:00
api_implementation.cc Tag the api_implementation Python extension module as Python 3.13 no-GIL compatible. 2025-10-28 18:48:41 -07:00
api_implementation.py Auto-format Py Proto python files. 2026-04-29 13:40:58 -07:00
builder.py Auto-format Py Proto python files. 2026-04-29 13:40:58 -07:00
containers.py Emit future warning when mutating GetOptions() in OSS 2026-07-12 13:41:24 -07:00
decoder.py Fix messageset conformance for pure Python to match Py-C++ and Py-upb 2026-08-13 06:25:58 -07:00
decoder_test.py Auto-format Py Proto python files. 2026-04-29 13:40:58 -07:00
descriptor_database_test.py Change Python DescriptorDatabase FindFileContainingSymbol() to accept leading "." to match DescriptorPool and other languages 2026-06-09 15:58:00 -07:00
descriptor_pool_test.py Auto-format Py Proto python files. 2026-04-29 13:40:58 -07:00
descriptor_pool_test1.proto Fix minor typos (#17682) 2024-09-20 20:50:06 -07:00
descriptor_pool_test2.proto Shorten our license headers into an abbreviated form that references LICENSE instead of including it in full. 2023-09-08 18:50:27 -07:00
descriptor_test.py fix(python): reject mismatched CopyToProto targets (#28666) 2026-07-29 07:09:05 -07:00
duration_test.py Automated Code Change 2026-07-02 04:39:38 -07:00
encoder.py Improve serialization performance in the case of recursive maps in pure Python Protobuf. 2026-05-05 13:45:18 -07:00
enum_type_wrapper.py Create an enum_type_wrapper.pyi stub with DESCRIPTOR: EnumDescriptor 2026-05-12 06:14:01 -07:00
extension_dict.py [py/pure python] Implement Make GetOptions() return immutable options. Raise a TypeError when options returned GetOptions() by is mutated. 2026-06-03 10:20:31 -07:00
factory_test1.proto On demand create message meta class for upb python 2023-09-15 11:34:38 -07:00
factory_test2.proto Automated rollback of commit 4af0b887a5. 2025-11-14 13:18:18 -08:00
field_mask.py Auto-format Py Proto python files. 2026-04-29 13:40:58 -07:00
field_mask_test.py Change field_mask.py to avoid recursion 2026-03-25 05:26:34 -07:00
file_options_test.proto Shorten our license headers into an abbreviated form that references LICENSE instead of including it in full. 2023-09-08 18:50:27 -07:00
generator_test.py Auto-format Py Proto python files. 2026-04-29 13:40:58 -07:00
import_test.py Add python tests for import option 2025-09-02 16:20:53 -07:00
json_format_benchmark.py Additional unit tests for Python handling of custom JSON enum names. 2026-08-18 14:57:30 -07:00
json_format_test.py Optimize pure Python parse path for custom JSON enum names. 2026-08-25 07:09:41 -07:00
keywords_test.py Auto-format Py Proto python files. 2026-04-29 13:40:58 -07:00
message_benchmark.py Internal change. 2026-06-24 05:13:39 -07:00
message_factory_test.py Fix segfault via strcmp(NULL) in numpy type detection 2026-06-25 09:52:49 -07:00
message_listener.py Auto-format Py Proto python files. 2026-04-29 13:40:58 -07:00
message_set_extensions.proto Add recursion guards for the following nested messages: 2026-02-11 15:13:37 -08:00
message_test.py fix missed oom handling in unset required 2026-07-30 10:42:17 -07:00
missing_enum_values.proto Shorten our license headers into an abbreviated form that references LICENSE instead of including it in full. 2023-09-08 18:50:27 -07:00
more_extensions.proto Shorten our license headers into an abbreviated form that references LICENSE instead of including it in full. 2023-09-08 18:50:27 -07:00
more_extensions_dynamic.proto Shorten our license headers into an abbreviated form that references LICENSE instead of including it in full. 2023-09-08 18:50:27 -07:00
more_messages.proto Fixed a bug in msg.MergeFrom(msg2) in Python. 2026-03-12 14:04:14 -07:00
no_package.proto Shorten our license headers into an abbreviated form that references LICENSE instead of including it in full. 2023-09-08 18:50:27 -07:00
packed_field_test.proto Shorten our license headers into an abbreviated form that references LICENSE instead of including it in full. 2023-09-08 18:50:27 -07:00
proto_api_test.py Add PyDescriptorPool_FromSharedPool(std::shared_ptr) API 2026-06-26 11:48:30 -07:00
proto_api_test_ext.cc Automated Code Change 2026-07-01 23:28:07 -07:00
proto_builder_test.py python: mark proto_builder sha1 as non-security use (#27473) 2026-06-09 08:53:29 -07:00
proto_json_test.py Auto-format Py Proto python files. 2026-04-29 13:40:58 -07:00
proto_test.py Protocol buffers support being created from memoryviews. 2026-08-17 14:23:02 -07:00
proto_text_test.py Nextgen Proto Pythonic API: “Add-on” proto for Text Format serialize/parse 2025-01-09 18:02:40 -08:00
pybind11_test_module.cc Fix stack overflow on self-recursive messages with pybind11 2024-06-12 02:35:05 -07:00
python_edition_defaults.py.template Implement Editions in Pure Python. 2023-11-07 14:40:26 -08:00
python_message.py Emit future warning when mutating GetOptions() in OSS 2026-07-12 13:41:24 -07:00
python_protobuf.cc Automated Code Change 2025-03-30 20:54:41 -07:00
recursive_message_pybind11_test.py Fix stack overflow on self-recursive messages with pybind11 2024-06-12 02:35:05 -07:00
reflection_cpp_test.py Auto-format Py Proto python files. 2026-04-29 13:40:58 -07:00
reflection_test.py Auto-format Py Proto python files. 2026-04-29 13:40:58 -07:00
runtime_version_test.py Auto-format Py Proto python files. 2026-04-29 13:40:58 -07:00
self_recursive.proto Add recursion depth limits to pure python 2025-05-13 14:45:52 -07:00
self_recursive_from_py.proto Fix stack overflow on self-recursive messages with pybind11 2024-06-12 02:35:05 -07:00
service_reflection_test.py Auto-format Py Proto python files. 2026-04-29 13:40:58 -07:00
symbol_database_test.py Auto-format Py Proto python files. 2026-04-29 13:40:58 -07:00
test_bad_identifiers.proto Shorten our license headers into an abbreviated form that references LICENSE instead of including it in full. 2023-09-08 18:50:27 -07:00
test_proto2.proto [Py/C++] Fixed data race in Python free threading by removing obsolete hack 2026-08-19 08:59:26 -07:00
test_proto3_optional.proto Add new including_default_value_without_presence_fields to Python JSON serializer. 2024-01-31 14:16:54 -08:00
test_util.py Auto-format Py Proto python files. 2026-04-29 13:40:58 -07:00
testing_refleaks.py Auto-format Py Proto python files. 2026-04-29 13:40:58 -07:00
text_encoding_test.py Auto-format Py Proto python files. 2026-04-29 13:40:58 -07:00
text_format_test.py Auto-format Py Proto python files. 2026-04-29 13:40:58 -07:00
thread_safe_test.py [Py/C++] Fixed data race in Python free threading by removing obsolete hack 2026-08-19 08:59:26 -07:00
timestamp_test.py Automated Code Change 2026-07-02 04:39:38 -07:00
type_checkers.py Fix segfault via strcmp(NULL) in numpy type detection 2026-06-25 09:52:49 -07:00
unknown_fields_test.py Auto-format Py Proto python files. 2026-04-29 13:40:58 -07:00
well_known_types.py Auto-format Py Proto python files. 2026-04-29 13:40:58 -07:00
well_known_types_test.proto Add construction support for repeated Timestamp/Duration/Struct/ListValue. 2025-09-08 11:47:20 -07:00
well_known_types_test.py Improve serialization performance in the case of recursive maps in pure Python Protobuf. 2026-05-05 13:45:18 -07:00
wire_format.py Auto-format Py Proto python files. 2026-04-29 13:40:58 -07:00
wire_format_test.py Auto-format Py Proto python files. 2026-04-29 13:40:58 -07:00