dep-protobuf/python/google/protobuf
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
..
compiler Add __init__.py files to compiler and util subpackages (#4117) 2018-04-09 12:43:10 -07:00
internal Optimize pure Python parse path for custom JSON enum names. 2026-08-25 07:09:41 -07:00
pyext [Py/C++] Fixed data race in Python free threading by removing obsolete hack 2026-08-19 08:59:26 -07:00
testdata Add CI runs for testing upb Python wheels 2023-09-21 11:33:23 -07:00
util Add __init__.py files to compiler and util subpackages (#4117) 2018-04-09 12:43:10 -07:00
__init__.py Updating version.json and repo version numbers to: 37.0-dev (#28425) 2026-07-10 06:22:59 -07:00
any.py Auto-format Py Proto python files. 2026-04-29 13:40:58 -07:00
breaking_changes.h internal change 2026-05-28 12:21:38 -07:00
descriptor.py fix(python): reject mismatched CopyToProto targets (#28666) 2026-07-29 07:09:05 -07:00
descriptor_database.py Change Python DescriptorDatabase FindFileContainingSymbol() to accept leading "." to match DescriptorPool and other languages 2026-06-09 15:58:00 -07:00
descriptor_pool.py [Py/FreeThreading] Fixed remaining race conditions in Dealloc() 2026-07-22 12:43:18 -07:00
duration.py Nextgen Proto Pythonic API: Add duration.py 2024-08-08 17:06:20 -07:00
json_format.py Optimize pure Python parse path for custom JSON enum names. 2026-08-25 07:09:41 -07:00
link_error_fast_cpp.cc internal change 2024-03-25 20:02:03 -07:00
link_error_pure_python.cc internal change 2024-03-25 20:02:03 -07:00
link_error_upb.cc internal change 2024-03-25 20:02:03 -07:00
message.py Add link to the page which explains that deterministic != canonical on our apis which let you set determinsitic serialization 2026-06-11 06:20:08 -07:00
message_factory.py Auto-format Py Proto python files. 2026-04-29 13:40:58 -07:00
proto.py Serialize length-prefixed protos once (#27252) 2026-05-29 12:19:19 -07:00
proto_api.cc Fix potential Py_DECREF(NULL) crash in PythonMessageMutator destructor. 2026-07-24 16:57:59 -07:00
proto_api.h Add PyDescriptorPool_FromSharedPool(std::shared_ptr) API 2026-06-26 11:48:30 -07:00
proto_builder.py python: mark proto_builder sha1 as non-security use (#27473) 2026-06-09 08:53:29 -07:00
proto_json.py Auto-format Py Proto python files. 2026-04-29 13:40:58 -07:00
proto_text.py Auto-format Py Proto python files. 2026-04-29 13:40:58 -07:00
python_protobuf.h Shorten our license headers into an abbreviated form that references LICENSE instead of including it in full. 2023-09-08 18:51:51 -07:00
reflection.py Auto-format Py Proto python files. 2026-04-29 13:40:58 -07:00
runtime_version.py Updating version.json and repo version numbers to: 37.0-dev (#28425) 2026-07-10 06:22:59 -07:00
service_reflection.py Auto-format Py Proto python files. 2026-04-29 13:40:58 -07:00
symbol_database.py Auto-format Py Proto python files. 2026-04-29 13:40:58 -07:00
text_encoding.py Auto-format Py Proto python files. 2026-04-29 13:40:58 -07:00
text_format.py Auto-format Py Proto python files. 2026-04-29 13:40:58 -07:00
timestamp.py Nextgen Proto Pythonic API: Add timestamp.py 2024-08-07 11:46:45 -07:00
unknown_fields.py Auto-format Py Proto python files. 2026-04-29 13:40:58 -07:00
use_fast_cpp_protos.cc Automated Code Change 2026-07-07 06:47:10 -07:00
use_pure_python.cc internal change 2025-10-23 15:13:55 -07:00
use_upb_protos.cc internal change 2025-10-23 15:13:55 -07:00