-
-
Notifications
You must be signed in to change notification settings - Fork 30.6k
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
gh-125196: Use PyUnicodeWriter for repr(list) #125202
Conversation
Replace the private _PyUnicodeWriter with the public PyUnicodeWriter.
Benchmark: import pyperf
runner = pyperf.Runner()
runner.bench_func('repr([])', repr, [])
runner.bench_func('repr([1,2,3])', repr, [1, 2, 3])
list3 = ['abcdef']*10
runner.bench_func('repr(list3)', repr, list3)
list4 = ['abcdef']*50
runner.bench_func('repr(list4)', repr, list4) Result, Python built with
In the worst case, it's 24 ns slower, 534 ns => 558 ns: 1.04x slower. @serhiy-storchaka @pitrou: Do you think that it's an acceptable slowdown, under 10% slower on a microbenchmark? |
|
||
error: | ||
_PyUnicodeWriter_Dealloc(&writer); | ||
if (writer != NULL) { | ||
PyUnicodeWriter_Discard(writer); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should not PyUnicodeWriter_Discard(NULL)
be no-op?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I modified PyUnicodeWriter_Discard(writer)
to do nothing if writer is NULL.
Objects/listobject.c
Outdated
if (i > 0) { | ||
if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0) | ||
if (PyUnicodeWriter_WriteUTF8(writer, ", ", 2) < 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is faster, PyUnicodeWriter_WriteUTF8(writer, ", ", 2)
or two PyUnicodeWriter_WriteChar()
?
This is an idle question. Even if the latter is marginally faster, it may still be more preferable to use the former. But if the difference is significant, this may be a question for optimization.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is faster, PyUnicodeWriter_WriteUTF8(writer, ", ", 2) or two PyUnicodeWriter_WriteChar()?
Two PyUnicodeWriter_WriteChar() calls is faster:
+----------------+-------------+-----------------------+
| Benchmark | change_utf8 | change_2char |
+================+=============+=======================+
| repr([]) | 50.2 ns | 54.0 ns: 1.07x slower |
+----------------+-------------+-----------------------+
| repr(list3) | 825 ns | 802 ns: 1.03x faster |
+----------------+-------------+-----------------------+
| repr(list4) | 2.89 us | 2.66 us: 1.09x faster |
+----------------+-------------+-----------------------+
| Geometric mean | (ref) | 1.01x faster |
+----------------+-------------+-----------------------+
Definitely! |
Combined with a minor change, PR gh-125214, it's even faster :-) |
Well, than integers are not suitable for this microbenchmark, you should use something else. |
Objects/unicodeobject.c
Outdated
@@ -13428,6 +13428,9 @@ PyUnicodeWriter_Create(Py_ssize_t length) | |||
|
|||
void PyUnicodeWriter_Discard(PyUnicodeWriter *writer) | |||
{ | |||
if (writer == NULL) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be documented. Please do this in a separate PR.
Also, I think it is worth to try to optimize PyUnicodeWriter_WriteUTF8()
for short ASCII strings.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be documented. Please do this in a separate PR.
Oh ok. I reverted the change.
This reverts commit 8b33b0e.
Updated benchmark results with CPU isolation:
Now it's only slower for
Only |
I merged, thanks for reviews. There is apparently room for performance improvement. Let's use this change as a starting point. |
Replace the private _PyUnicodeWriter with the public PyUnicodeWriter.