Enable free-threaded Python and improve concurrency in CEL Python. - #104
Merged
Conversation
copybara-service
Bot
force-pushed
the
test_969972475
branch
2 times, most recently
from
August 26, 2026 00:55
c38e17f to
0f15c8f
Compare
copybara-service
Bot
force-pushed
the
test_969972475
branch
2 times, most recently
from
August 26, 2026 01:44
53a8096 to
baae4fa
Compare
- Declare free-threading compatibility on pybind11 modules (`mod_gil_not_used`) and BUILD targets (`freethreading_compatible`). - Introduce `FreeThreadingMutex`, a zero-cost abstraction that compiles away in GIL-enabled builds and uses `absl::Mutex` in free-threaded mode (`Py_GIL_DISABLED`). - Protect lazy `cel_program_` compilation in `PyCelExpression` with `FreeThreadingMutex`. - Protect Python object caching and lazy element resolution in `PyCelValue` and list/map accessors with `FreeThreadingMutex`. - Protect `PyMessageFactory::message_classes_` with `FreeThreadingMutex`. - Extract `BuildCompiler` and `BuildRuntime` to construct compiler/runtime objects without holding mutex locks, using double-checked locking in `GetCompiler` and `GetRuntime`. - Eagerly resolve `variable_types_` during `PyCelEnvInternal` initialization, making `GetVariableType` lock-free and thread-safe. - Release GIL during CPU-bound policy compilation and YAML parsing. - Avoid unconditional `gil_scoped_acquire` across destructors and error conversions when GIL state is already held (`PyGILState_Check`). - Initialize pybind11 internals at module import time to prevent DSO internals races in multi-threaded environments. - Prevent reference leaks in custom function invocation by releasing arguments and temporary results. - Add `test_freethreaded.sh` script and concurrency tests in `cel_parallel_test.py` and `cel_test.py`. ### Performance Benchmarks (`cel_parallel_test`, `-c opt`) | Benchmark Stage / Test Case | Standard Mode (GIL) | Free-Threaded Mode (No GIL) | Speedup / Impact | | :--- | :---: | :---: | :---: | | **Multi-Threaded Compilation** (1,000 items, 8 threads) | 75.74 ms | 23.14 ms | **3.27x faster** (69.5% faster) | | **Sequential Compilation** (1,000 items) | 61.77 ms | 63.28 ms | 0.98x | | **Multi-Threaded Evaluation** (10,000 items, 8 threads) | 234.79 ms | 138.88 ms | **1.69x faster** (40.9% faster) | | **Sequential Evaluation** (10,000 items) | 90.68 ms | 112.74 ms | 0.80x | PiperOrigin-RevId: 970936071
copybara-service
Bot
force-pushed
the
test_969972475
branch
from
August 26, 2026 01:49
baae4fa to
4e64447
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Enable free-threaded Python and improve concurrency in CEL Python.
mod_gil_not_used) and BUILD targets (freethreading_compatible).FreeThreadingMutex, a zero-cost abstraction that compiles away in GIL-enabled builds and usesabsl::Mutexin free-threaded mode (Py_GIL_DISABLED).cel_program_compilation inPyCelExpressionwithFreeThreadingMutex.PyCelValueand list/map accessors withFreeThreadingMutex.PyMessageFactory::message_classes_withFreeThreadingMutex.BuildCompilerandBuildRuntimeto construct compiler/runtime objects without holding mutex locks, using double-checked locking inGetCompilerandGetRuntime.variable_types_duringPyCelEnvInternalinitialization, makingGetVariableTypelock-free and thread-safe.gil_scoped_acquireacross destructors and error conversions when GIL state is already held (PyGILState_Check).test_freethreaded.shscript and concurrency tests incel_parallel_test.pyandcel_test.py.Performance Benchmarks (
cel_parallel_test,-c opt)