mirror of
https://github.com/python/cpython.git
synced 2025-08-24 02:35:59 +00:00
bpo-43179: Generalise alignment for optimised string routines (GH-24624)
* Remove m68k-specific hack from ascii_decode On m68k, alignments of primitives is more relaxed, with 4-byte and 8-byte types only requiring 2-byte alignment, thus using sizeof(size_t) does not work. Instead, use the portable alternative. Note that this is a minimal fix that only relaxes the assertion and the condition for when to use the optimised version remains overly strict. Such issues will be fixed tree-wide in the next commit. NB: In C11 we could use _Alignof(size_t) instead, but for compatibility we use autoconf. * Optimise string routines for architectures with non-natural alignment C only requires that sizeof(x) is a multiple of alignof(x), not that the two are equal. Thus anywhere where we optimise based on alignment we should be using alignof(x) not sizeof(x). This is more annoying than it would be in C11 where we could just use _Alignof(x) (and alignof(x) in C++11), but since we still require only C99 we must plumb the information all the way from autoconf through the various typedefs and defines.
This commit is contained in:
parent
cfa176685a
commit
dec0757549
8 changed files with 96 additions and 31 deletions
|
@ -5070,25 +5070,16 @@ static Py_ssize_t
|
|||
ascii_decode(const char *start, const char *end, Py_UCS1 *dest)
|
||||
{
|
||||
const char *p = start;
|
||||
const char *aligned_end = (const char *) _Py_ALIGN_DOWN(end, SIZEOF_SIZE_T);
|
||||
|
||||
/*
|
||||
* Issue #17237: m68k is a bit different from most architectures in
|
||||
* that objects do not use "natural alignment" - for example, int and
|
||||
* long are only aligned at 2-byte boundaries. Therefore the assert()
|
||||
* won't work; also, tests have shown that skipping the "optimised
|
||||
* version" will even speed up m68k.
|
||||
*/
|
||||
#if !defined(__m68k__)
|
||||
#if SIZEOF_SIZE_T <= SIZEOF_VOID_P
|
||||
assert(_Py_IS_ALIGNED(dest, SIZEOF_SIZE_T));
|
||||
if (_Py_IS_ALIGNED(p, SIZEOF_SIZE_T)) {
|
||||
assert(_Py_IS_ALIGNED(dest, ALIGNOF_SIZE_T));
|
||||
if (_Py_IS_ALIGNED(p, ALIGNOF_SIZE_T)) {
|
||||
/* Fast path, see in STRINGLIB(utf8_decode) for
|
||||
an explanation. */
|
||||
/* Help allocation */
|
||||
const char *_p = p;
|
||||
Py_UCS1 * q = dest;
|
||||
while (_p < aligned_end) {
|
||||
while (_p + SIZEOF_SIZE_T <= end) {
|
||||
size_t value = *(const size_t *) _p;
|
||||
if (value & ASCII_CHAR_MASK)
|
||||
break;
|
||||
|
@ -5104,15 +5095,14 @@ ascii_decode(const char *start, const char *end, Py_UCS1 *dest)
|
|||
}
|
||||
return p - start;
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
while (p < end) {
|
||||
/* Fast path, see in STRINGLIB(utf8_decode) in stringlib/codecs.h
|
||||
for an explanation. */
|
||||
if (_Py_IS_ALIGNED(p, SIZEOF_SIZE_T)) {
|
||||
if (_Py_IS_ALIGNED(p, ALIGNOF_SIZE_T)) {
|
||||
/* Help allocation */
|
||||
const char *_p = p;
|
||||
while (_p < aligned_end) {
|
||||
while (_p + SIZEOF_SIZE_T <= end) {
|
||||
size_t value = *(const size_t *) _p;
|
||||
if (value & ASCII_CHAR_MASK)
|
||||
break;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue