FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Enable free-threaded Python and improve concurrency in CEL Python. by copybara-service[bot] · Pull Request #104 · cel-expr/cel-python · GitHub

Enable free-threaded Python and improve concurrency in CEL Python. - #104

Merged
copybara-service[bot] merged 1 commit into
mainfrom
test_969972475
Aug 26, 2026
Merged

Enable free-threaded Python and improve concurrency in CEL Python.#104
copybara-service[bot] merged 1 commit into
mainfrom
test_969972475

Conversation

copybara-service Bot commented Aug 25, 2026
edited
Loading

Copy link
Copy Markdown

Enable free-threaded Python and improve concurrency in CEL Python.

  • 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

copybara-service Bot force-pushed the test_969972475 branch 2 times, most recently from c38e17f to 0f15c8f Compare August 26, 2026 00:55
copybara-service Bot changed the title Enable free-threaded Python and improve concurrency in CEL Python Enable free-threaded Python and improve concurrency in CEL Python. Aug 26, 2026
copybara-service Bot force-pushed the test_969972475 branch 2 times, most recently from 53a8096 to baae4fa Compare August 26, 2026 01:44
- 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 merged commit 4e64447 into main Aug 26, 2026
2 checks passed
copybara-service Bot deleted the test_969972475 branch August 26, 2026 01:49
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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant


Back | FazBrowse Home | New Git URL