未验证 提交 9fb22293 编写于 作者: X xiongkun 提交者: GitHub

Eval frame speedup (#53969)

* [Dy2static-Fallback] add set_eval_frame function in pybind.
1. add set_eval_frame function in pybind.

* add unittest for eval frame hooker.

* [support py38]

* fix-GeneratorExit error in eval frame hooker

* support python == 3.9

* support 3.10

* fix some comments

* speed up eval frame for cache hitted code.

* code format

* fix unittest

---------
Co-authored-by: NSigureMo <sigure.qaq@gmail.com>
上级 9ded0692
...@@ -146,9 +146,19 @@ static PyObject *_custom_eval_frame(PyThreadState *tstate, ...@@ -146,9 +146,19 @@ static PyObject *_custom_eval_frame(PyThreadState *tstate,
// NOTE: Cache is not supported now // NOTE: Cache is not supported now
PyCodeObject *code = reinterpret_cast<PyCodeObject *>( PyCodeObject *code = reinterpret_cast<PyCodeObject *>(
PyObject_GetAttrString(result, "code")); PyObject_GetAttrString(result, "code"));
// Re-enable custom behavior PyObject *disable_eval_frame =
eval_frame_callback_set(callback); PyObject_GetAttrString(result, "disable_eval_frame");
return eval_custom_code(tstate, frame, code, throw_flag); if (disable_eval_frame != Py_True) {
// Re-enable custom behavior
eval_frame_callback_set(callback);
auto out = eval_custom_code(tstate, frame, code, throw_flag);
return out;
} else {
auto out = eval_custom_code(tstate, frame, code, throw_flag);
// Re-enable custom behavior
eval_frame_callback_set(callback);
return out;
}
} else { } else {
// Re-enable custom behavior // Re-enable custom behavior
eval_frame_callback_set(callback); eval_frame_callback_set(callback);
......
...@@ -27,18 +27,22 @@ class TestEvalFrame(unittest.TestCase): ...@@ -27,18 +27,22 @@ class TestEvalFrame(unittest.TestCase):
pass pass
def test_eval_frame(self): def test_eval_frame(self):
CustomCode = collections.namedtuple("CustomCode", ["code"]) CustomCode = collections.namedtuple(
"CustomCode", ["code", "disable_eval_frame"]
)
def mul(a, b): def mul(a, b):
return a * b return a * b
code = CustomCode(mul.__code__) code = CustomCode(mul.__code__, True)
def callback(frame_obj): def callback(frame_obj):
# Do your callback function here and return a object with `.code` # Do your callback function here and return a object with `.code`
if frame_obj.f_code.co_name == "add": if frame_obj.f_code.co_name == "add":
return code return code
return CustomCode(code=frame_obj.f_code) # do nothing. return CustomCode(
code=frame_obj.f_code, disable_eval_frame=True
) # do nothing.
def add(a, b): def add(a, b):
return a + b return a + b
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册