Open eendebakpt opened 1 year ago
If we do this, we should just define a nice, readable macro for it in pymacro.h
and stop reinventing the wheel everywhere. We do similar moves, for example, all over bytecodes.c
.
If we do this, we should just define a nice, readable macro for it in
pymacro.h
and stop reinventing the wheel everywhere. We do similar moves, for example, all overbytecodes.c
.
@brandtbucher Do you mean bytesarray.c
or bytesobject.c
? In those I can find index checks, but not in bytecodes.c
.
Putting a macro in pymacro.h
seems like a good idea. A real macro, or an inline function then? Such a change would make the code more consistent, but it would also mean many (very small) changes in the codebase.
I think Brandt is referring to things like
Py_ssize_t signed_magnitude = Py_SIZE(sub);
DEOPT_IF(((size_t)signed_magnitude) > 1, BINARY_SUBSCR);
which I found at line 386 in Python/bytecodes.c.
That is indeed a similar trick! I found 3 occurrences in bytecodes.c
, the code then changes from:
DEOPT_IF(((size_t)signed_magnitude) > 1, BINARY_SUBSCR);
to
DEOPT_IF(invalid_index(signed_magnitude, 2), BINARY_SUBSCR);
Does this actually make a difference in practice? Can't the compiler already do this optimization?
Does this actually make a difference in practice? Can't the compiler already do this optimization?
The PR is created is more about consistency than optimization. With optimizations on there is a difference in the generated code (an extra test %rsi,%rsi
, see details below), but I do not think it makes a large difference (perhaps only in the tuple/list indexing or the usage in bytecodes.c
)
In
listobject.c
there is an optimization to check whether an index is valid (e.g.0 <= index < N
) using a single comparison. The same optimization is not used in other files such astupleobject.c
(except for _collectionsmodule.c)I created a branch with the idea for the
tupleobject.c
since that might be the most performance critical:https://github.com/python/cpython/compare/main...eendebakpt:cpython:list_tuple
I could not measure significant performance improvements on my system, but perhaps there are other systems/compilers where there is a measurable difference.