Skip to content

Optimize nif_process_ready_tasks for ~15% performance improvement#56

Merged
benoitc merged 1 commit intomainfrom
feature/optimize-process-ready-tasks
Mar 23, 2026
Merged

Optimize nif_process_ready_tasks for ~15% performance improvement#56
benoitc merged 1 commit intomainfrom
feature/optimize-process-ready-tasks

Conversation

@benoitc
Copy link
Copy Markdown
Owner

@benoitc benoitc commented Mar 23, 2026

Summary

  • Replace asyncio.iscoroutine() with PyCoro_CheckExact C API (~2-3us/task)
  • Use stack buffers for module/func strings instead of enif_alloc (~0.5-1us/task)
  • Cache asyncio.events module alongside cached_asyncio (~1-2us/call)
  • Pool ErlNifEnv allocations with 64-entry pool using enif_clear_env (~1-2us/task)

Target: reduce from ~53us to ~45-47us per call in submit_task flow.

- Replace asyncio.iscoroutine() with PyCoro_CheckExact C API (~2-3us/task)
- Use stack buffers for module/func strings instead of enif_alloc (~0.5-1us/task)
- Cache asyncio.events module alongside cached_asyncio (~1-2us/call)
- Pool ErlNifEnv allocations with 64-entry pool using enif_clear_env (~1-2us/task)
- Add mutex protection to env pool for thread safety

Target: reduce from ~53us to ~45-47us per call in submit_task flow.
@benoitc benoitc force-pushed the feature/optimize-process-ready-tasks branch from bf2a88b to c530ce2 Compare March 23, 2026 23:38
@benoitc benoitc merged commit 371fbe1 into main Mar 23, 2026
14 checks passed
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