This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: avoid needless pointers initialization in small tuple creation
Type: performance Stage:
Components: Interpreter Core Versions: Python 3.5
process
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: gregory.p.smith, jtaylor, pitrou, rhettinger, serhiy.storchaka, vstinner
Priority: low Keywords: patch

Created on 2014-04-03 18:39 by jtaylor, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
avoid-memset.patch jtaylor, 2014-04-03 18:39 review
Messages (6)
msg215461 - (view) Author: Julian Taylor (jtaylor) Date: 2014-04-03 18:39
attached a prototype patch that avoids the memset of ob_item in PyTuple_New which is not necessary for the BUILD_TUPLE bytecode and PyTuple_Pack as these overwrite every entry in ob_item anyway.
This improves small tuple creation by about 5%.

It does this by adding a new internal function that does not use the memset loop and wrapping that in PyTuple_New that does it. _Pack and ceval call the internal function.
The patch still needs cleanup I don't know where the signature for ceval.c would best go. Does the internal function need to be hidden from the DSO?

microbenchmark, compiled with gcc-4.8.2 on ubuntu 14.04 amd64, default configure options:

import timeit
print(min(timeit.repeat("(a,)", setup="a = 1; b = 1", repeat=5, number=10**7)))
print(min(timeit.repeat("(a, b)", setup="a = 1; b = 1", repeat=5, number=10**7)))

before:
0.45767
0.52926

after:
0.42652
0.50122

larger tuples do not profit much as the loading is more expensive in comparison.
msg215469 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-04-03 21:06
A 5% improvement on a micro-benchmark probably means 0% on real workloads. You could try to run the benchmarks suite at http://hg.python.org/benchmarks
msg240852 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2015-04-14 06:54
I think this looks promising.

Don't worry too much about the modest timing improvement.  For the most part, we should almost always take steps to eliminate work that is known to be unnecessary.  The timings serve as a guide but it would be easy for us to get trapped in local minimums on a particular machine or compiler.
msg240858 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2015-04-14 08:10
> avoid memset in small tuple creation

I don't understand this title because there is no call to memset() in the patch.

Can you try to modify PyTuple_New() to use memset() instead of a dummy loop?

Adding a _PyTuple_New() which doesn't initialize the memory doesn't seem safe to me.

You may try to allocate the tuple with PyObject_Calloc(), but when I tried on other types, it was slower than PyObject_Malloc() for sizes smaller than 1 MB.
msg240931 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2015-04-14 16:16
Running the https://hg.python.org/benchmarks suite against this change (edited to not have the warning about PyTupleObject* vs PyObject* types) I see no repeatably significant benefits and one that is consistently a few percent slower no matter how many times I run it: nbody.

### nbody ###
Min: 0.226902 -> 0.236582: 1.04x slower
Avg: 0.231973 -> 0.238949: 1.03x slower
Significant (t=-11.98)
Stddev: 0.00507 -> 0.00286: 1.7751x smaller

That seems odd to me, but _might_ be related to the additional call overhead. Regardless, I don't think this patch is really an optimization.
msg246523 - (view) Author: Julian Taylor (jtaylor) Date: 2015-07-09 20:05
right at best its probably too insignificant to really be worthwhile, closing.
History
Date User Action Args
2022-04-11 14:58:01adminsetgithub: 65347
2015-07-09 20:05:46jtaylorsetstatus: open -> closed

messages: + msg246523
2015-04-14 16:16:56gregory.p.smithsetpriority: normal -> low
title: avoid memset in small tuple creation -> avoid needless pointers initialization in small tuple creation
nosy: + gregory.p.smith

messages: + msg240931
2015-04-14 08:10:38vstinnersetnosy: + vstinner
messages: + msg240858
2015-04-14 06:54:25rhettingersetnosy: + rhettinger
messages: + msg240852
2015-04-13 19:01:18pitrousetnosy: + serhiy.storchaka
2014-04-03 21:06:15pitrousetnosy: + pitrou
messages: + msg215469
2014-04-03 18:39:32jtaylorcreate