MagicStack / MagicStack/uvloop
Wiriting cdef extension classes for `Future` objects for speeding up `_SyncSocketReaderFuture` and `_SyncSocketWriterFuture`
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Cython
- Star
- 11.9k
- Fork
- 615
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Mô tả
I have been brainstorming ways of going about optimizing some objects in uvloop and winloop's code and I think I have an idea for making these
objects run slightly faster and I was wondering if I could make the same optimizations on uvloop's end when I am done implementing this on winloop's side. My main objective involves wrapping _SyncSocketReaderFuture and _SyncSocketWriterFuture to a faster object such as a cdef extension so that these can be more optimized as well as possibly looking into getting rid of aio_Future if proven to be faster since programmers should rely on what asyncio.isfuture does and not isinstance(fut, asyncio.Future) which should be discouraged allowing for new optimizations to be introduced.
The other workaround idea was asking cpython for a CAPI Capsule which I wrote an issue for already but I think if this idea is proven successful we can skip that entirely and use this instead.
# My concept is still a little incomplete so take this with a grain of salt.
cimport cython
from .loop cimport Loop # Ignore for now I am just needing this since I am working with cyright in vscode to help with my thinking process.
import sys
from cpython.exc cimport PyErr_SetString, PyErr_SetObject
from cpython.ref cimport Py_CLEAR
from cpython.object cimport PyObject
from cpython.list cimport PyList_GET_SIZE, PyList_Append
from cpython.tuple cimport PyTuple_GET_ITEM
from cpython.contextvars cimport PyContext_CopyCurrent
from asyncio import format_helpers
include "includes/stdlib.pxi"
# In CPython these are enums so lets make some enums
# to get off from strings for speed.
cdef enum fut_state:
STATE_PENDING = 0
STATE_CANCELLED = 1
STATE_FINISHED = 2
cdef extern from "Python.h":
"""
#define PyObject_isNULL(obj) obj == NULL
#define DoTheEvilTrick(obj, to) \\
to = obj; obj = NULL
"""
# Checks to see if PyObject* is null by as a normal object
# without recasting itself.
bint PyObject_isNULL(object obj)
# Don't let cython toy with refs...
void DoTheEvilTrick(object obj, object to)
int PyException_SetTraceback(object ex, object tb) except -1
object PyException_GetContext(object ex)
void PyException_SetContext(object ex, object ctx)
object PyException_GetCause(object ex)
void PyException_SetCause(object ex, object cause)
object PyException_GetTraceback(object ex)
# Here is a comptable version of asyncio.Future forked over from
# python 3.15 which is currently in research mode but
# we can make changes if needed.
# I'll mix a bit of CPython in so that we can be speedy.
cdef class Future:
"""This class is *almost* compatible with concurrent.futures.Future.
Differences:
- This class is not thread-safe.
- result() and exception() do not take a timeout argument and
raise an exception when the future isn't done yet.
- Callbacks registered with add_done_callback() are always called
via the event loop's call_soon().
- This class is not compatible with the wait() and as_completed()
methods in the concurrent.futures package.
"""
cdef:
# obviously since we don't want this exposed for external editing.
Loop fut_loop
# Skipping fut_context0 and fut_callback0 to prevent
# complexity we can optimize further later...
list fut_callbacks
object fut_exception
object fut_exception_tb
object fut_result
object fut_source_tb
object fut_cancel_msg
object fut_cancelled_exc
object fut_awaited_by
fut_state fut_state
bint fut_awaited_by_is_set
bint fut_is_task
unsigned int fut_log_tb
unsigned int fut_blocking
Hướng dẫn đóng góp
Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Đề xuất này nhắm đến _SyncSocketReaderFuture và _SyncSocketWriterFuture, đồng thời đề xuất thay thế hoặc loại bỏ aio_Future bằng một lớp mở rộng Cython. Không có tệp repository, test, benchmark hay tiêu chí chấp nhận cụ thể nào được nêu, vì vậy hãy bắt đầu bằng cách xác định các triển khai Future đó và các nơi sử dụng chúng qua asyncio.isfuture. Công việc được xem là hoàn tất khi chứng minh được tốc độ tăng lên trong khi vẫn giữ nguyên hành vi Future như mong đợi, nhưng issue không định nghĩa cách đo lường điều đó.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- python
- Lĩnh vực
- networking, performance
- Loại issue
- Tái cấu trúc
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Cần làm rõ
- Mức phù hợp với người mới
- 20/100