Issue #28126: Replace Py_MEMCPY with memcpy(). Visual Studio can properly optimize memcpy().

This commit is contained in:
Christian Heimes 2016-09-13 20:22:02 +02:00
parent a4d9b17b1f
commit f051e43b22
14 changed files with 83 additions and 97 deletions

View File

@ -177,26 +177,9 @@ typedef int Py_ssize_clean_t;
#define Py_LOCAL_INLINE(type) static type
#endif
/* Py_MEMCPY can be used instead of memcpy in cases where the copied blocks
* are often very short. While most platforms have highly optimized code for
* large transfers, the setup costs for memcpy are often quite high. MEMCPY
* solves this by doing short copies "in line".
*/
#if defined(_MSC_VER)
#define Py_MEMCPY(target, source, length) do { \
size_t i_, n_ = (length); \
char *t_ = (void*) (target); \
const char *s_ = (void*) (source); \
if (n_ >= 16) \
memcpy(t_, s_, n_); \
else \
for (i_ = 0; i_ < n_; i_++) \
t_[i_] = s_[i_]; \
} while (0)
#else
/* Py_MEMCPY is kept for backwards compatibility,
* see https://bugs.python.org/issue28126 */
#define Py_MEMCPY memcpy
#endif
#include <stdlib.h>
@ -742,7 +725,7 @@ extern pid_t forkpty(int *, char *, struct termios *, struct winsize *);
#endif
#ifdef VA_LIST_IS_ARRAY
#define Py_VA_COPY(x, y) Py_MEMCPY((x), (y), sizeof(va_list))
#define Py_VA_COPY(x, y) memcpy((x), (y), sizeof(va_list))
#else
#ifdef __va_copy
#define Py_VA_COPY __va_copy

View File

@ -156,7 +156,7 @@ typedef uint8_t Py_UCS1;
Py_UNICODE_ISNUMERIC(ch))
#define Py_UNICODE_COPY(target, source, length) \
Py_MEMCPY((target), (source), (length)*sizeof(Py_UNICODE))
memcpy((target), (source), (length)*sizeof(Py_UNICODE))
#define Py_UNICODE_FILL(target, value, length) \
do {Py_ssize_t i_; Py_UNICODE *t_ = (target); Py_UNICODE v_ = (value);\

View File

@ -10,6 +10,9 @@ What's New in Python 3.6.0 beta 2
Core and Builtins
-----------------
- Issue #28126: Replace Py_MEMCPY with memcpy(). Visual Studio can properly
optimize memcpy().
- Issue #28120: Fix dict.pop() for splitted dictionary when trying to remove a
"pending key" (Not yet inserted in split-table). Patch by Xiang Zhang.

View File

@ -836,10 +836,10 @@ array_repeat(arrayobject *a, Py_ssize_t n)
memset(np->ob_item, a->ob_item[0], newbytes);
} else {
Py_ssize_t done = oldbytes;
Py_MEMCPY(np->ob_item, a->ob_item, oldbytes);
memcpy(np->ob_item, a->ob_item, oldbytes);
while (done < newbytes) {
Py_ssize_t ncopy = (done <= newbytes-done) ? done : newbytes-done;
Py_MEMCPY(np->ob_item+done, np->ob_item, ncopy);
memcpy(np->ob_item+done, np->ob_item, ncopy);
done += ncopy;
}
}

View File

@ -64,14 +64,14 @@
#define ENTRY_READ_PDATA(TABLE, ENTRY, DATA_SIZE, PDATA) \
do { \
assert((DATA_SIZE) == (TABLE)->data_size); \
Py_MEMCPY((PDATA), _Py_HASHTABLE_ENTRY_PDATA(TABLE, (ENTRY)), \
memcpy((PDATA), _Py_HASHTABLE_ENTRY_PDATA(TABLE, (ENTRY)), \
(DATA_SIZE)); \
} while (0)
#define ENTRY_WRITE_PDATA(TABLE, ENTRY, DATA_SIZE, PDATA) \
do { \
assert((DATA_SIZE) == (TABLE)->data_size); \
Py_MEMCPY((void *)_Py_HASHTABLE_ENTRY_PDATA((TABLE), (ENTRY)), \
memcpy((void *)_Py_HASHTABLE_ENTRY_PDATA((TABLE), (ENTRY)), \
(PDATA), (DATA_SIZE)); \
} while (0)
@ -337,7 +337,7 @@ _Py_hashtable_set(_Py_hashtable_t *ht, size_t key_size, const void *pkey,
}
entry->key_hash = key_hash;
Py_MEMCPY((void *)_Py_HASHTABLE_ENTRY_PKEY(entry), pkey, ht->key_size);
memcpy((void *)_Py_HASHTABLE_ENTRY_PKEY(entry), pkey, ht->key_size);
if (data)
ENTRY_WRITE_PDATA(ht, entry, data_size, data);

View File

@ -43,26 +43,26 @@ typedef struct {
#define _Py_HASHTABLE_READ_KEY(TABLE, PKEY, DST_KEY) \
do { \
assert(sizeof(DST_KEY) == (TABLE)->key_size); \
Py_MEMCPY(&(DST_KEY), (PKEY), sizeof(DST_KEY)); \
memcpy(&(DST_KEY), (PKEY), sizeof(DST_KEY)); \
} while (0)
#define _Py_HASHTABLE_ENTRY_READ_KEY(TABLE, ENTRY, KEY) \
do { \
assert(sizeof(KEY) == (TABLE)->key_size); \
Py_MEMCPY(&(KEY), _Py_HASHTABLE_ENTRY_PKEY(ENTRY), sizeof(KEY)); \
memcpy(&(KEY), _Py_HASHTABLE_ENTRY_PKEY(ENTRY), sizeof(KEY)); \
} while (0)
#define _Py_HASHTABLE_ENTRY_READ_DATA(TABLE, ENTRY, DATA) \
do { \
assert(sizeof(DATA) == (TABLE)->data_size); \
Py_MEMCPY(&(DATA), _Py_HASHTABLE_ENTRY_PDATA(TABLE, (ENTRY)), \
memcpy(&(DATA), _Py_HASHTABLE_ENTRY_PDATA(TABLE, (ENTRY)), \
sizeof(DATA)); \
} while (0)
#define _Py_HASHTABLE_ENTRY_WRITE_DATA(TABLE, ENTRY, DATA) \
do { \
assert(sizeof(DATA) == (TABLE)->data_size); \
Py_MEMCPY((void *)_Py_HASHTABLE_ENTRY_PDATA((TABLE), (ENTRY)), \
memcpy((void *)_Py_HASHTABLE_ENTRY_PDATA((TABLE), (ENTRY)), \
&(DATA), sizeof(DATA)); \
} while (0)

View File

@ -721,9 +721,9 @@ save_unconsumed_input(compobject *self, Py_buffer *data, int err)
new_data = PyBytes_FromStringAndSize(NULL, new_size);
if (new_data == NULL)
return -1;
Py_MEMCPY(PyBytes_AS_STRING(new_data),
memcpy(PyBytes_AS_STRING(new_data),
PyBytes_AS_STRING(self->unused_data), old_size);
Py_MEMCPY(PyBytes_AS_STRING(new_data) + old_size,
memcpy(PyBytes_AS_STRING(new_data) + old_size,
self->zst.next_in, left_size);
Py_SETREF(self->unused_data, new_data);
self->zst.avail_in = 0;

View File

@ -2353,7 +2353,7 @@ _PyObject_Call_Prepend(PyObject *func,
/* use borrowed references */
stack[0] = obj;
Py_MEMCPY(&stack[1],
memcpy(&stack[1],
&PyTuple_GET_ITEM(args, 0),
argcount * sizeof(PyObject *));
@ -2428,7 +2428,7 @@ _PyStack_UnpackDict(PyObject **args, Py_ssize_t nargs, PyObject *kwargs,
}
/* Copy position arguments (borrowed references) */
Py_MEMCPY(stack, args, nargs * sizeof(stack[0]));
memcpy(stack, args, nargs * sizeof(stack[0]));
kwstack = stack + nargs;
pos = i = 0;

View File

@ -120,7 +120,7 @@ PyBytes_FromStringAndSize(const char *str, Py_ssize_t size)
if (str == NULL)
return (PyObject *) op;
Py_MEMCPY(op->ob_sval, str, size);
memcpy(op->ob_sval, str, size);
/* share short strings */
if (size == 1) {
characters[*str & UCHAR_MAX] = op;
@ -163,7 +163,7 @@ PyBytes_FromString(const char *str)
return PyErr_NoMemory();
(void)PyObject_INIT_VAR(op, &PyBytes_Type, size);
op->ob_shash = -1;
Py_MEMCPY(op->ob_sval, str, size+1);
memcpy(op->ob_sval, str, size+1);
/* share short strings */
if (size == 0) {
nullstring = op;
@ -437,7 +437,7 @@ formatfloat(PyObject *v, int flags, int prec, int type,
str = _PyBytesWriter_Prepare(writer, str, len);
if (str == NULL)
return NULL;
Py_MEMCPY(str, p, len);
memcpy(str, p, len);
PyMem_Free(p);
str += len;
return str;
@ -626,7 +626,7 @@ _PyBytes_FormatEx(const char *format, Py_ssize_t format_len,
len = format_len - (fmt - format);
assert(len != 0);
Py_MEMCPY(res, fmt, len);
memcpy(res, fmt, len);
res += len;
fmt += len;
fmtcnt -= (len - 1);
@ -1009,7 +1009,7 @@ _PyBytes_FormatEx(const char *format, Py_ssize_t format_len,
}
/* Copy bytes */
Py_MEMCPY(res, pbuf, len);
memcpy(res, pbuf, len);
res += len;
/* Pad right with the fill character if needed */
@ -1473,12 +1473,12 @@ bytes_repeat(PyBytesObject *a, Py_ssize_t n)
}
i = 0;
if (i < size) {
Py_MEMCPY(op->ob_sval, a->ob_sval, Py_SIZE(a));
memcpy(op->ob_sval, a->ob_sval, Py_SIZE(a));
i = Py_SIZE(a);
}
while (i < size) {
j = (i <= size-i) ? i : size-i;
Py_MEMCPY(op->ob_sval+i, op->ob_sval, j);
memcpy(op->ob_sval+i, op->ob_sval, j);
i += j;
}
return (PyObject *) op;
@ -2765,7 +2765,7 @@ bytes_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
n = PyBytes_GET_SIZE(tmp);
pnew = type->tp_alloc(type, n);
if (pnew != NULL) {
Py_MEMCPY(PyBytes_AS_STRING(pnew),
memcpy(PyBytes_AS_STRING(pnew),
PyBytes_AS_STRING(tmp), n+1);
((PyBytesObject *)pnew)->ob_shash =
((PyBytesObject *)tmp)->ob_shash;
@ -3237,7 +3237,7 @@ _PyBytesWriter_Resize(_PyBytesWriter *writer, void *str, Py_ssize_t size)
dest = PyByteArray_AS_STRING(writer->buffer);
else
dest = PyBytes_AS_STRING(writer->buffer);
Py_MEMCPY(dest,
memcpy(dest,
writer->small_buffer,
pos);
}
@ -3372,7 +3372,7 @@ _PyBytesWriter_WriteBytes(_PyBytesWriter *writer, void *ptr,
if (str == NULL)
return NULL;
Py_MEMCPY(str, bytes, size);
memcpy(str, bytes, size);
str += size;
return str;

View File

@ -107,7 +107,7 @@ STRINGLIB(bytes_join)(PyObject *sep, PyObject *iterable)
for (i = 0; i < nbufs; i++) {
Py_ssize_t n = buffers[i].len;
char *q = buffers[i].buf;
Py_MEMCPY(p, q, n);
memcpy(p, q, n);
p += n;
}
goto done;
@ -116,12 +116,12 @@ STRINGLIB(bytes_join)(PyObject *sep, PyObject *iterable)
Py_ssize_t n;
char *q;
if (i) {
Py_MEMCPY(p, sepstr, seplen);
memcpy(p, sepstr, seplen);
p += seplen;
}
n = buffers[i].len;
q = buffers[i].buf;
Py_MEMCPY(p, q, n);
memcpy(p, q, n);
p += n;
}
goto done;

View File

@ -108,7 +108,7 @@ pad(PyObject *self, Py_ssize_t left, Py_ssize_t right, char fill)
if (u) {
if (left)
memset(STRINGLIB_STR(u), fill, left);
Py_MEMCPY(STRINGLIB_STR(u) + left,
memcpy(STRINGLIB_STR(u) + left,
STRINGLIB_STR(self),
STRINGLIB_LEN(self));
if (right)
@ -275,13 +275,13 @@ stringlib_replace_interleave(PyObject *self,
if (to_len > 1) {
/* Lay the first one down (guaranteed this will occur) */
Py_MEMCPY(result_s, to_s, to_len);
memcpy(result_s, to_s, to_len);
result_s += to_len;
count -= 1;
for (i = 0; i < count; i++) {
*result_s++ = *self_s++;
Py_MEMCPY(result_s, to_s, to_len);
memcpy(result_s, to_s, to_len);
result_s += to_len;
}
}
@ -297,7 +297,7 @@ stringlib_replace_interleave(PyObject *self,
}
/* Copy the rest of the original string */
Py_MEMCPY(result_s, self_s, self_len - i);
memcpy(result_s, self_s, self_len - i);
return result;
}
@ -337,11 +337,11 @@ stringlib_replace_delete_single_character(PyObject *self,
next = findchar(start, end - start, from_c);
if (next == NULL)
break;
Py_MEMCPY(result_s, start, next - start);
memcpy(result_s, start, next - start);
result_s += (next - start);
start = next + 1;
}
Py_MEMCPY(result_s, start, end - start);
memcpy(result_s, start, end - start);
return result;
}
@ -390,12 +390,12 @@ stringlib_replace_delete_substring(PyObject *self,
break;
next = start + offset;
Py_MEMCPY(result_s, start, next - start);
memcpy(result_s, start, next - start);
result_s += (next - start);
start = next + from_len;
}
Py_MEMCPY(result_s, start, end - start);
memcpy(result_s, start, end - start);
return result;
}
@ -427,7 +427,7 @@ stringlib_replace_single_character_in_place(PyObject *self,
return NULL;
}
result_s = STRINGLIB_STR(result);
Py_MEMCPY(result_s, self_s, self_len);
memcpy(result_s, self_s, self_len);
/* change everything in-place, starting with this one */
start = result_s + (next - self_s);
@ -477,11 +477,11 @@ stringlib_replace_substring_in_place(PyObject *self,
return NULL;
}
result_s = STRINGLIB_STR(result);
Py_MEMCPY(result_s, self_s, self_len);
memcpy(result_s, self_s, self_len);
/* change everything in-place, starting with this one */
start = result_s + offset;
Py_MEMCPY(start, to_s, from_len);
memcpy(start, to_s, from_len);
start += from_len;
end = result_s + self_len;
@ -491,7 +491,7 @@ stringlib_replace_substring_in_place(PyObject *self,
0);
if (offset == -1)
break;
Py_MEMCPY(start + offset, to_s, from_len);
memcpy(start + offset, to_s, from_len);
start += offset + from_len;
}
@ -544,20 +544,20 @@ stringlib_replace_single_character(PyObject *self,
if (next == start) {
/* replace with the 'to' */
Py_MEMCPY(result_s, to_s, to_len);
memcpy(result_s, to_s, to_len);
result_s += to_len;
start += 1;
} else {
/* copy the unchanged old then the 'to' */
Py_MEMCPY(result_s, start, next - start);
memcpy(result_s, start, next - start);
result_s += (next - start);
Py_MEMCPY(result_s, to_s, to_len);
memcpy(result_s, to_s, to_len);
result_s += to_len;
start = next + 1;
}
}
/* Copy the remainder of the remaining bytes */
Py_MEMCPY(result_s, start, end - start);
memcpy(result_s, start, end - start);
return result;
}
@ -613,20 +613,20 @@ stringlib_replace_substring(PyObject *self,
next = start + offset;
if (next == start) {
/* replace with the 'to' */
Py_MEMCPY(result_s, to_s, to_len);
memcpy(result_s, to_s, to_len);
result_s += to_len;
start += from_len;
} else {
/* copy the unchanged old then the 'to' */
Py_MEMCPY(result_s, start, next - start);
memcpy(result_s, start, next - start);
result_s += (next - start);
Py_MEMCPY(result_s, to_s, to_len);
memcpy(result_s, to_s, to_len);
result_s += to_len;
start = next + from_len;
}
}
/* Copy the remainder of the remaining bytes */
Py_MEMCPY(result_s, start, end - start);
memcpy(result_s, start, end - start);
return result;
}

View File

@ -1048,7 +1048,7 @@ resize_copy(PyObject *unicode, Py_ssize_t length)
return NULL;
copy_length = _PyUnicode_WSTR_LENGTH(unicode);
copy_length = Py_MIN(copy_length, length);
Py_MEMCPY(_PyUnicode_WSTR(w), _PyUnicode_WSTR(unicode),
memcpy(_PyUnicode_WSTR(w), _PyUnicode_WSTR(unicode),
copy_length * sizeof(wchar_t));
return w;
}
@ -1435,7 +1435,7 @@ _copy_characters(PyObject *to, Py_ssize_t to_start,
if (max_char >= 128)
return -1;
}
Py_MEMCPY((char*)to_data + to_kind * to_start,
memcpy((char*)to_data + to_kind * to_start,
(char*)from_data + from_kind * from_start,
to_kind * how_many);
}
@ -2024,7 +2024,7 @@ PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size)
break;
case PyUnicode_2BYTE_KIND:
#if Py_UNICODE_SIZE == 2
Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2);
memcpy(PyUnicode_2BYTE_DATA(unicode), u, size * 2);
#else
_PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2,
u, u + size, PyUnicode_2BYTE_DATA(unicode));
@ -2037,7 +2037,7 @@ PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size)
unicode_convert_wchar_to_ucs4(u, u + size, unicode);
#else
assert(num_surrogates == 0);
Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4);
memcpy(PyUnicode_4BYTE_DATA(unicode), u, size * 4);
#endif
break;
default:
@ -2348,7 +2348,7 @@ _PyUnicode_Copy(PyObject *unicode)
return NULL;
assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode));
Py_MEMCPY(PyUnicode_DATA(copy), PyUnicode_DATA(unicode),
memcpy(PyUnicode_DATA(copy), PyUnicode_DATA(unicode),
length * PyUnicode_KIND(unicode));
assert(_PyUnicode_CheckConsistency(copy, 1));
return copy;
@ -2454,7 +2454,7 @@ as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize,
}
else {
assert(kind == PyUnicode_4BYTE_KIND);
Py_MEMCPY(target, data, len * sizeof(Py_UCS4));
memcpy(target, data, len * sizeof(Py_UCS4));
}
if (copy_null)
target[len] = 0;
@ -2963,7 +2963,7 @@ unicode_aswidechar(PyObject *unicode,
size = res + 1;
else
res = size;
Py_MEMCPY(w, wstr, size * sizeof(wchar_t));
memcpy(w, wstr, size * sizeof(wchar_t));
return res;
}
else
@ -3986,7 +3986,7 @@ PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize)
return NULL;
}
_PyUnicode_UTF8_LENGTH(unicode) = PyBytes_GET_SIZE(bytes);
Py_MEMCPY(_PyUnicode_UTF8(unicode),
memcpy(_PyUnicode_UTF8(unicode),
PyBytes_AS_STRING(bytes),
_PyUnicode_UTF8_LENGTH(unicode) + 1);
Py_DECREF(bytes);
@ -5473,7 +5473,7 @@ _PyUnicode_EncodeUTF32(PyObject *str,
}
if (PyBytes_Check(rep)) {
Py_MEMCPY(out, PyBytes_AS_STRING(rep), repsize);
memcpy(out, PyBytes_AS_STRING(rep), repsize);
out += moreunits;
} else /* rep is unicode */ {
assert(PyUnicode_KIND(rep) == PyUnicode_1BYTE_KIND);
@ -5825,7 +5825,7 @@ _PyUnicode_EncodeUTF16(PyObject *str,
}
if (PyBytes_Check(rep)) {
Py_MEMCPY(out, PyBytes_AS_STRING(rep), repsize);
memcpy(out, PyBytes_AS_STRING(rep), repsize);
out += moreunits;
} else /* rep is unicode */ {
assert(PyUnicode_KIND(rep) == PyUnicode_1BYTE_KIND);
@ -10012,7 +10012,7 @@ _PyUnicode_JoinArray(PyObject *separator, PyObject **items, Py_ssize_t seqlen)
/* Copy item, and maybe the separator. */
if (i && seplen != 0) {
Py_MEMCPY(res_data,
memcpy(res_data,
sep_data,
kind * seplen);
res_data += kind * seplen;
@ -10020,7 +10020,7 @@ _PyUnicode_JoinArray(PyObject *separator, PyObject **items, Py_ssize_t seqlen)
itemlen = PyUnicode_GET_LENGTH(item);
if (itemlen != 0) {
Py_MEMCPY(res_data,
memcpy(res_data,
PyUnicode_DATA(item),
kind * itemlen);
res_data += kind * itemlen;
@ -12396,11 +12396,11 @@ unicode_repeat(PyObject *str, Py_ssize_t len)
Py_ssize_t done = PyUnicode_GET_LENGTH(str);
const Py_ssize_t char_size = PyUnicode_KIND(str);
char *to = (char *) PyUnicode_DATA(u);
Py_MEMCPY(to, PyUnicode_DATA(str),
memcpy(to, PyUnicode_DATA(str),
PyUnicode_GET_LENGTH(str) * char_size);
while (done < nchars) {
n = (done <= nchars-done) ? done : nchars-done;
Py_MEMCPY(to + (done * char_size), to, n * char_size);
memcpy(to + (done * char_size), to, n * char_size);
done += n;
}
}
@ -13531,7 +13531,7 @@ _PyUnicodeWriter_WriteASCIIString(_PyUnicodeWriter *writer,
const Py_UCS1 *str = (const Py_UCS1 *)ascii;
Py_UCS1 *data = writer->data;
Py_MEMCPY(data + writer->pos, str, len);
memcpy(data + writer->pos, str, len);
break;
}
case PyUnicode_2BYTE_KIND:
@ -14928,7 +14928,7 @@ unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
_PyUnicode_WSTR(self) = (wchar_t *)data;
}
Py_MEMCPY(data, PyUnicode_DATA(unicode),
memcpy(data, PyUnicode_DATA(unicode),
kind * (length + 1));
assert(_PyUnicode_CheckConsistency(self, 1));
#ifdef Py_DEBUG

View File

@ -130,7 +130,7 @@ w_string(const char *s, Py_ssize_t n, WFILE *p)
m = p->end - p->ptr;
if (p->fp != NULL) {
if (n <= m) {
Py_MEMCPY(p->ptr, s, n);
memcpy(p->ptr, s, n);
p->ptr += n;
}
else {
@ -140,7 +140,7 @@ w_string(const char *s, Py_ssize_t n, WFILE *p)
}
else {
if (n <= m || w_reserve(p, n - m)) {
Py_MEMCPY(p->ptr, s, n);
memcpy(p->ptr, s, n);
p->ptr += n;
}
}

View File

@ -396,7 +396,7 @@ siphash24(const void *src, Py_ssize_t src_sz) {
case 7: pt[6] = m[6];
case 6: pt[5] = m[5];
case 5: pt[4] = m[4];
case 4: Py_MEMCPY(pt, m, sizeof(uint32_t)); break;
case 4: memcpy(pt, m, sizeof(uint32_t)); break;
case 3: pt[2] = m[2];
case 2: pt[1] = m[1];
case 1: pt[0] = m[0];