python / python/cpython

Extract common bytecode retrieval logic from `_PyFrame_GetBytecode` and `_PyFrame_SafeGetLasti`

Đang mở
#142,929 3 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

interpreter-core type-refactor
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

Both _PyFrame_GetBytecode and _PyFrame_SafeGetLasti inline functions currently duplicate the same logic for retrieving the bytecode, differing only in whether they call _PyFrame_GetCode or _PyFrame_SafeGetCode.

static inline _Py_CODEUNIT *
_PyFrame_GetBytecode(_PyInterpreterFrame *f)
{
#ifdef Py_GIL_DISABLED
    PyCodeObject *co = _PyFrame_GetCode(f);
    _PyCodeArray *tlbc = _PyCode_GetTLBCArray(co);
    assert(f->tlbc_index >= 0 && f->tlbc_index < tlbc->size);
    return (_Py_CODEUNIT *)tlbc->entries[f->tlbc_index];
#else
    return _PyCode_CODE(_PyFrame_GetCode(f));
#endif
}

static inline int
_PyFrame_SafeGetLasti(struct _PyInterpreterFrame *f)
{
    // Code based on _PyFrame_GetBytecode() but replace _PyFrame_GetCode()
    // with _PyFrame_SafeGetCode().
    PyCodeObject *co = _PyFrame_SafeGetCode(f);
    if (co == NULL) {
        return -1;
    }

    _Py_CODEUNIT *bytecode;
#ifdef Py_GIL_DISABLED
    _PyCodeArray *tlbc = _PyCode_GetTLBCArray(co);
    assert(f->tlbc_index >= 0 && f->tlbc_index < tlbc->size);
    bytecode = (_Py_CODEUNIT *)tlbc->entries[f->tlbc_index];
#else
    bytecode = _PyCode_CODE(co);
#endif

    return (int)(f->instr_ptr - bytecode) * sizeof(_Py_CODEUNIT);
}

However, I think we should extract the bytecode retrieving logic into another helper, let's say, _PyFrame_GetBytecodeFromCodeObject, as:

static inline _Py_CODEUNIT *
_PyFrame_GetBytecodeFromCodeObject(_PyInterpreterFrame *f, PyCodeObject *co)
{
#ifdef Py_GIL_DISABLED
     _PyCodeArray *tlbc = _PyCode_GetTLBCArray(co);
    assert(f->tlbc_index >= 0 && f->tlbc_index < tlbc->size);
    return (_Py_CODEUNIT *)tlbc->entries[f->tlbc_index];
#else
    return _PyCode_CODE(co);
#endif
}

So we can use that helper as:

static inline _Py_CODEUNIT *
_PyFrame_GetBytecode(_PyInterpreterFrame *f)
{
    PyCodeObject *co = _PyFrame_GetCode(f);
    return _PyFrame_GetBytecodeFromCodeObject(f, co);
}

static inline int
_PyFrame_SafeGetLasti(_PyInterpreterFrame *f)
{
    PyCodeObject *co = _PyFrame_SafGetCode(f);
    if(co == NULL){
        return -1;
    }

    _Py_CODEUNIT *bytecode = _PyFrame_GetBytecodeFromCodeObject(f, co);
    return (int)(f->insert_ptr - bytecode) * sizeof(_Py_CODEUNIT);
}

However, we can use different types of helpers such as inline helper with a function pointer or a macro wrapper. My point here is not about the implementation of the helper itself, but about extracting the bytecode retrieving logic into another helper regardless of the helper name and the method used to implement that helper.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu bằng cách tìm _PyFrame_GetBytecode và _PyFrame_SafeGetLasti, sau đó so sánh các hàm gọi chúng với các đường dẫn _PyFrame_GetCode và _PyFrame_SafeGetCode hiện có. Trích xuất logic dùng chung để lấy bytecode trong khi vẫn bảo toàn hành vi khi GIL được bật và khi GIL bị tắt, đồng thời xác minh rằng các bài kiểm thử CPython liên quan đều đạt.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
c
Lĩnh vực
backend
Loại issue
Tái cấu trúc
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
35/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.