diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-09-01-03-05-22.gh-issue-156748.B0Z47w.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-09-01-03-05-22.gh-issue-156748.B0Z47w.rst new file mode 100644 index 000000000000000..338258636602440 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-09-01-03-05-22.gh-issue-156748.B0Z47w.rst @@ -0,0 +1 @@ +Optimize refcount churn in _csv. diff --git a/Modules/_csv.c b/Modules/_csv.c index c640f2d36a84647..eb4bdc4af89e302 100644 --- a/Modules/_csv.c +++ b/Modules/_csv.c @@ -14,6 +14,7 @@ module instead. #endif #include "Python.h" +#include "pycore_list.h" // _PyList_AppendTakeRef() #include "pycore_pyatomic_ft_wrappers.h" #include // offsetof() @@ -710,11 +711,9 @@ parse_save_field(ReaderObj *self) } self->field_len = 0; } - if (PyList_Append(self->fields, field) < 0) { - Py_DECREF(field); + if (_PyList_AppendTakeRef((PyListObject *)self->fields, field) < 0) { return -1; } - Py_DECREF(field); return 0; }