-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
Improve method cache efficiency #67036
Comments
The method cache is currently very small. Attached patch bumps the size a bit and improves the hash computation formula. |
It's not easy to get stable benchmark runs, but here is an example: Report on Linux fsol 3.16.0-24-generic #32-Ubuntu SMP Tue Oct 28 13:07:32 UTC 2014 x86_64 x86_64 ### 2to3 ### ### formatted_logging ### ### mako_v2 ### ### richards ### ### simple_logging ### The following not significant results are hidden, use -v to show them: |
Current hashing algorithm takes middle bits, proposed code takes low bits. Doesn't this make the hash worse? |
FYI method cache optimization was added in bpo-1700288. |
The low bits of the unicode hash should be as good as the middle bits. |
In addition, the tp_version_tag evolves incrementally, so the low bits should be better when the same name is looked up on different types. |
Thanks for adding the instrumentation. Otherwise, it would have been difficult to tell whether the xor hashing had a significant deleterious effect on collisions. Also, +1 on bumping up the cache size. |
New changeset 97dc64adb6fe by Antoine Pitrou in branch 'default': |
Now pushed. Thanks for the comments! |
I'm reopening this and assigning it to benjamin as the 2.7 release manager. This change is valuable to apply to 2.7.x as well. It is very simple and is a clear performance improvement for realistic workloads. No API change. When you profile Python 2.7 applications today, the _PyType_Lookup function shows up in the ~3% of all CPU cycles range. This reduces that for a small memory tradeoff. We're raising our cache exponent to be even larger than the 12 in this patch at work as we've got some huge applications. Regardless, 12 is a much better default than the existing 9. |
New changeset 6357d851029d by Antoine Pitrou in branch '2.7': |
I suppose we've backported scarier things. |
It appears this change broke all FreeBSD builders (9: gcc, 10/11: clang) for the 2.7 branch with: ==== koobs-freebsd-current (clang 3.7.x) cc -pthread -c -fno-strict-aliasing -OPT:Olimit=0 -g -O2 -g -O0 -Wall -Wstrict-prototypes -I. -IInclude -I./Include -DPy_BUILD_CORE -o Objects/typeobject.o Objects/typeobject.c
Objects/typeobject.c:2568:44: error: no member named 'hash' in 'PyStringObject'
assert(((PyStringObject *)(name))->hash != -1);
~~~~~~~~~~~~~~~~~~~~~~~~~~ ^
/usr/include/assert.h:54:21: note: expanded from macro 'assert'
#define assert(e) ((e) ? (void)0 : __assert(__func__, __FILE__, \
^
1 error generated.
*** Error code 1 ==== koobs-freebsd10 (clang 3.4.x) cc -pthread -c -fno-strict-aliasing -OPT:Olimit=0 -g -O2 -g -O0 -Wall -Wstrict-prototypes -I. -IInclude -I./Include -fPIC -DPy_BUILD_CORE -o Objects/unicodeobject.o Objects/unicodeobject.c Objects/typeobject.c:2568:18: error: use of undeclared identifier 'PyASCIIObject'
assert(((PyASCIIObject *)(name))->hash != -1);
^
/usr/include/assert.h:54:21: note: expanded from macro 'assert'
#define assert(e) ((e) ? (void)0 : __assert(__func__, __FILE__, \
^
Objects/typeobject.c:2568:33: error: expected expression
assert(((PyASCIIObject *)(name))->hash != -1);
^
/usr/include/assert.h:54:21: note: expanded from macro 'assert'
#define assert(e) ((e) ? (void)0 : __assert(__func__, __FILE__, \
^
--- Objects/unicodectype.o cc -pthread -c -fno-strict-aliasing -OPT:Olimit=0 -g -O2 -g -O0 -Wall -Wstrict-prototypes -I. -IInclude -I./Include -fPIC -DPy_BUILD_CORE -o Objects/unicodectype.o Objects/unicodectype.c ==== koobs-freebsd9 (gcc 4.2.1 + patches) gcc -pthread -c -fno-strict-aliasing -g -O2 -g -O0 -Wall -Wstrict-prototypes -I. -IInclude -I./Include -DPy_BUILD_CORE -o Objects/unicodeobject.o Objects/unicodeobject.c |
New changeset 04424651f76c by Benjamin Peterson in branch '2.7': |
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: