Antoine Pitrou <pitrou@free.fr> added the comment:

+    if (len == 0) {
+        if (PyByteArray_CheckExact(self)) {
+            Py_INCREF(self);
+            return (PyObject *)self;
+        }
+        return PyByteArray_FromStringAndSize(NULL, 0);
+    }

This looks like a dubious micro-optimization. If len == 0,
all loops will exit early anyway (same for similar snippets in bytesobject.c and unicodeobject.c).


You are right about those lines in particular; 'dubious' as you say. 
Though, the point overall was that the general implementation was noticeably
faster (regardless of those smaller changes). 

However, I do think that the single check for len == 0 saves time particularly
from do_xstrip where we call _getbuffer().


+    if (i == 0 && j == len && PyByteArray_CheckExact(self)) {
+        Py_INCREF(self);
+        return (PyObject *)self;
+    }

bytearray objects are mutable, so you can't return the same object without breaking expected semantics. e.g.:


I see. I must have been trigger happy.


> diff -r b5ccdf7c032a Python/bltinmodule.c
> Binary file Python/bltinmodule.c has changed

Uh, what's this? bltinmodule.c shouldn't be considered a binary file.
It probably means you added a NUL byte in it by mistake.

I didn't touch that file at all. I'm not sure what that is. :-P

> Looking at just how similar these 3 implementations are I feel that we
> could also unify/generalize them into one generic helper implementation
> and leave the object marshaling up to the type specific methods.

You could put it in Objects/stringlib.

I figured. I will when I am able to get to it.


- John