[mypyc] Implement list insert primitive - #9741
Conversation
8d1534b to
032d073
Compare
TH3CHARLie
left a comment
There was a problem hiding this comment.
Great performance improvement, left a few comments on function signatures.
| 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); |
| return list_count((PyListObject *)obj, value); | ||
| } | ||
|
|
||
| CPyTagged CPyList_Insert(PyObject *list, CPyTagged index, PyObject *value) |
| 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.
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
| method_op( | ||
| name='insert', | ||
| arg_types=[list_rprimitive, int_rprimitive, object_rprimitive], | ||
| return_type=int_rprimitive, |
There was a problem hiding this comment.
the return type should be c_int_rprimitive since it corresponds to the low-level int.
There was a problem hiding this comment.
Ah, that makes sense. Should be ok now.
TH3CHARLie
left a comment
There was a problem hiding this comment.
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.
@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.
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: