Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(11)

Side by Side Diff: Objects/bytesobject.c

Issue 26574: replace_interleave can be optimized for single character byte strings
Patch Set: Created 3 years, 11 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« Objects/bytearrayobject.c ('K') | « Objects/bytearrayobject.c ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* bytes object implementation */ 1 /* bytes object implementation */
2 2
3 #define PY_SSIZE_T_CLEAN 3 #define PY_SSIZE_T_CLEAN
4 4
5 #include "Python.h" 5 #include "Python.h"
6 6
7 #include "bytes_methods.h" 7 #include "bytes_methods.h"
8 #include "pystrhex.h" 8 #include "pystrhex.h"
9 #include <stddef.h> 9 #include <stddef.h>
10 10
(...skipping 2446 matching lines...) Expand 10 before | Expand all | Expand 10 after
2457 } 2457 }
2458 result_len = count * to_len + self_len; 2458 result_len = count * to_len + self_len;
2459 2459
2460 if (! (result = (PyBytesObject *) 2460 if (! (result = (PyBytesObject *)
2461 PyBytes_FromStringAndSize(NULL, result_len)) ) 2461 PyBytes_FromStringAndSize(NULL, result_len)) )
2462 return NULL; 2462 return NULL;
2463 2463
2464 self_s = PyBytes_AS_STRING(self); 2464 self_s = PyBytes_AS_STRING(self);
2465 result_s = PyBytes_AS_STRING(result); 2465 result_s = PyBytes_AS_STRING(result);
2466 2466
2467 /* TODO: special case single character, which doesn't need memcpy */ 2467 if (to_len > 1) {
2468 2468 /* Lay the first one down (guaranteed this will occur) */
2469 /* Lay the first one down (guaranteed this will occur) */
2470 Py_MEMCPY(result_s, to_s, to_len);
2471 result_s += to_len;
2472 count -= 1;
2473
2474 for (i=0; i<count; i++) {
2475 *result_s++ = *self_s++;
2476 Py_MEMCPY(result_s, to_s, to_len); 2469 Py_MEMCPY(result_s, to_s, to_len);
2477 result_s += to_len; 2470 result_s += to_len;
2471 count -= 1;
2472
2473 for (i = 0; i < count; i++) {
2474 *result_s++ = *self_s++;
2475 Py_MEMCPY(result_s, to_s, to_len);
2476 result_s += to_len;
2477 }
2478 } else {
haypo 2016/03/17 00:45:05 same comments
2479 result_s[0] = to_s[0];
2480 result_s += to_len;
2481 count -= 1;
2482 for (i = 0; i < count; i++) {
2483 *result_s++ = *self_s++;
2484 result_s[0] = to_s[0];
2485 result_s += to_len;
2486 }
2478 } 2487 }
2479
2480 /* Copy the rest of the original string */ 2488 /* Copy the rest of the original string */
2481 Py_MEMCPY(result_s, self_s, self_len-i); 2489 Py_MEMCPY(result_s, self_s, self_len-i);
2482
2483 return result; 2490 return result;
2484 } 2491 }
2485 2492
2486 /* Special case for deleting a single character */ 2493 /* Special case for deleting a single character */
2487 /* len(self)>=1, len(from)==1, to="", maxcount>=1 */ 2494 /* len(self)>=1, len(from)==1, to="", maxcount>=1 */
2488 Py_LOCAL(PyBytesObject *) 2495 Py_LOCAL(PyBytesObject *)
2489 replace_delete_single_character(PyBytesObject *self, 2496 replace_delete_single_character(PyBytesObject *self,
2490 char from_c, Py_ssize_t maxcount) 2497 char from_c, Py_ssize_t maxcount)
2491 { 2498 {
2492 char *self_s, *result_s; 2499 char *self_s, *result_s;
(...skipping 1682 matching lines...) Expand 10 before | Expand all | Expand 10 after
4175 4182
4176 str = _PyBytesWriter_Prepare(writer, str, size); 4183 str = _PyBytesWriter_Prepare(writer, str, size);
4177 if (str == NULL) 4184 if (str == NULL)
4178 return NULL; 4185 return NULL;
4179 4186
4180 Py_MEMCPY(str, bytes, size); 4187 Py_MEMCPY(str, bytes, size);
4181 str += size; 4188 str += size;
4182 4189
4183 return str; 4190 return str;
4184 } 4191 }
OLDNEW
« Objects/bytearrayobject.c ('K') | « Objects/bytearrayobject.c ('k') | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+