| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
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 gcc -O3: +----------------+---------+-----------------------+ | Benchmark | ref | change | +================+=========+=======================+ | repr([]) | 104 ns | 98.1 ns: 1.06x faster | +----------------+---------+-----------------------+ | repr([1,2,3]) | 534 ns | 558 ns: 1.04x slower | +----------------+---------+-----------------------+ | repr(list3) | 1.53 us | 1.59 us: 1.04x slower | +----------------+---------+-----------------------+ | repr(list4) | 5.36 us | 5.53 us: 1.03x slower | +----------------+---------+-----------------------+ | Geometric mean | (ref) | 1.01x slower | +----------------+---------+-----------------------+ 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? |
Sorry, something went wrong.
| error: | ||
| _PyUnicodeWriter_Dealloc(&writer); | ||
| if (writer != NULL) { | ||
| PyUnicodeWriter_Discard(writer); |
There was a problem hiding this comment.
Should not PyUnicodeWriter_Discard(NULL) be no-op?
Sorry, something went wrong.
There was a problem hiding this comment.
Ok, I modified PyUnicodeWriter_Discard(writer) to do nothing if writer is NULL.
Sorry, something went wrong.
| for (Py_ssize_t i = 0; i < Py_SIZE(v); ++i) { | ||
| if (i > 0) { | ||
| if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0) | ||
| if (PyUnicodeWriter_WriteUTF8(writer, ", ", 2) < 0) { |
There was a problem hiding this comment.
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.
Sorry, something went wrong.
There was a problem hiding this comment.
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 | +----------------+-------------+-----------------------+
Sorry, something went wrong.
Definitely! |
Sorry, something went wrong.
Combined with a minor change, PR gh-125214, it's even faster :-) |
Sorry, something went wrong.
Well, than integers are not suitable for this microbenchmark, you should use something else. |
Sorry, something went wrong.
|
|
||
| void PyUnicodeWriter_Discard(PyUnicodeWriter *writer) | ||
| { | ||
| if (writer == NULL) { |
There was a problem hiding this comment.
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.
Sorry, something went wrong.
There was a problem hiding this comment.
This should be documented. Please do this in a separate PR.
Oh ok. I reverted the change.
Sorry, something went wrong.
This reverts commit 8b33b0e.
|
Updated benchmark results with CPU isolation: vstinner@mona$ python3 -m pyperf compare_to --table ref.json change.json +----------------+---------+-----------------------+ | Benchmark | ref | change | +================+=========+=======================+ | repr([]) | 57.2 ns | 52.2 ns: 1.09x faster | +----------------+---------+-----------------------+ | repr([1,2,3]) | 283 ns | 293 ns: 1.03x slower | +----------------+---------+-----------------------+ | repr(list3) | 827 ns | 807 ns: 1.02x faster | +----------------+---------+-----------------------+ | repr(list4) | 2.80 us | 2.74 us: 1.02x faster | +----------------+---------+-----------------------+ | Geometric mean | (ref) | 1.03x faster | +----------------+---------+-----------------------+ Now it's only slower for repr([1,2,3]), but this row will be made way faster using gh-125214.
Only repr([1,2,3]) row uses integers, list3 and list4 use strings. |
Sorry, something went wrong.
|
I merged, thanks for reviews. There is apparently room for performance improvement. Let's use this change as a starting point. |
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
Replace the private _PyUnicodeWriter with the public PyUnicodeWriter.