New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add memcmp into unicode_compare for optimizing comparisons #57488
Comments
In discussions of memcmp performance, (http://www.picklingtools.com/study.pdf) With unicode and Python 3.3 (and anyPython 3.x) there are a Basically, if the unicode strings are "1 byte kind", then memcmp can be used almost as is. If the unicode strings are the same kind, they can at least use memcmp to compare for equality or inequality. There is also a minor optimization laying in unicode_compare: if you These 3 minor optimizations can make unicode_compare faster. |
This is a potential patch: There is a test as well, testing 1 and 2 byte kinds. I have run it through the python tests and have added no new breakages |
I would be nice to have a third path for inegality with kind1==kind2, something like: else if (kind1 == PyUnicode_2BYTE_KIND && kind2 == PyUnicode_2BYTE_KIND) Inegality comparaisons are used to sort Unicode lists for example. |
Can you please try to write a short benchmark script? (or just run a benchmark using ./python -m timeit) |
Here's a test demonstrating the memcmp optimization effect: a = []
b = []
c = []
d = []
for x in range(0,1000) :
a.append("the quick brown fox"+str(x))
b.append("the wuick brown fox"+str(x))
c.append("the quick brown fox"+str(x))
d.append("the wuick brown fox"+str(x))
count = 0
for x in range(0,200000) :
if a==c : count += 1
if a==c : count += 2
if a==d : count += 3
if b==c : count += 5
if b==d : count += 7
if c==d : count += 11
print(count) # plain Python 3.3 no memcmp, no UCS specialization % time ./python ~/PickTest5/Python/string_test3.py % setenv CFLAGS -fno-builtin-memcmp # (reconfigure and remake) % time ./python ~/PickTest5/Python/string_test3.py ------------------------------------------------------------------------- % time ./python ~/PickTest5/Python/string_test3.py % setenv CFLAGS -fno-builtin-memcmp # (reconfigure and remake) % time ./python ~/PickTest5/Python/string_test3.py --------------------------------------------------------------------- Note we only really see the effect if we make sure that gcc I am currently working with Bob Arendt (a colleague who works |
Wouldn't it be better to have it enabled by default in their gcc? |
Added branches for specializing for UCS2 and UCS4 types |
I'd rather infer the opposite: given how GCC generates code, this patch |
I concur with Martin. |
Some more information: Bob Arendt and I have been playing with the Fedora Core .spec file Our conjecture is that the -m32 or -mtune=atom automatically turns the builtin memcmp off, but we're not sure (we're still playing with it). However, perhaps Fedora builds on a more generic machine: that generic build keeps the 'rep cmbsb'? Frustrating. |
I agree with Martin. This patch would be very nice if there wasn't the |
Ok, closing the issue. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: