New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[mypyc] Implement list insert primitive #9741
Conversation
8d1534b
to
032d073
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great performance improvement, left a few comments on function signatures.
mypyc/lib-rt/CPy.h
Outdated
@@ -323,6 +323,7 @@ bool CPyList_SetItem(PyObject *list, CPyTagged index, PyObject *value); | |||
PyObject *CPyList_PopLast(PyObject *obj); | |||
PyObject *CPyList_Pop(PyObject *obj, CPyTagged index); | |||
CPyTagged CPyList_Count(PyObject *obj, PyObject *value); | |||
CPyTagged CPyList_Insert(PyObject *list, CPyTagged index, PyObject *value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return int
here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
mypyc/lib-rt/list_ops.c
Outdated
@@ -108,6 +108,15 @@ CPyTagged CPyList_Count(PyObject *obj, PyObject *value) | |||
return list_count((PyListObject *)obj, value); | |||
} | |||
|
|||
CPyTagged CPyList_Insert(PyObject *list, CPyTagged index, PyObject *value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as above
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
mypyc/primitives/list_ops.py
Outdated
arg_types=[list_rprimitive, int_rprimitive, object_rprimitive], | ||
return_type=int_rprimitive, | ||
c_function_name='CPyList_Insert', | ||
error_kind=ERR_MAGIC) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since the function PyList_Insert
returns an int
here, it is most reasonable to define the op with ERR_NEG_INT
. See the append op in list_ops.py for an example
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
mypyc/primitives/list_ops.py
Outdated
method_op( | ||
name='insert', | ||
arg_types=[list_rprimitive, int_rprimitive, object_rprimitive], | ||
return_type=int_rprimitive, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the return type should be c_int_rprimitive
since it corresponds to the low-level int.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, that makes sense. Should be ok now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the update, looks great!
l.insert(2, 2) | ||
assert l == [0, 1, 2, 3] | ||
l.insert(4, 4) | ||
assert l == [0, 1, 2, 3, 4] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vsakkas This doesn't test the new primitive, since the code is in driver.py
, which doesn't get compiled. Would you mind updating the test case? Also, it would be good to check negative indexes and index underflow and overflow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Of course. I'll look into updating the tests in a new PR.
Description
Implements list insert primitive for improved performance.
Related ticket: mypyc/mypyc#644
Test Plan
Ensure that inserting at the start, middle and end of a list works as expected, check performance improvements.
Generated IR
The following script was used:
Master branch:
PR:
Performance
Master:
PR:
Update
The C helper function has resulted in a small decrease in the performance benefits: