Uh oh!
There was an error while loading. Please reload this page.
Change LineDecoder to match stdlib splitlines, resulting in significant speed up - #2423
Conversation
08ad3ce to
50c6811Comparelovelydinosaur
commented
Nov 2, 2022
A solution using the built-in |
giannitedesco
commented
Nov 3, 2022
Actually requests is using splitlines, too. So should I just switch to that and update the failing tests? |
lovelydinosaur
commented
Nov 3, 2022
Switching to a |
Haha, yes, the first implementation I did with |
9cbd511 to
27b880eCompareUh oh!
There was an error while loading. Please reload this page.
27b880e to
ee687aeComparegiannitedesco
commented
Nov 21, 2022
@florimondmanca@tomchristie is this acceptable? I suppose the benefit of this API is that if you do something like |
ee687ae to
042ab35Comparelovelydinosaur
commented
Nov 21, 2022
No. I don't believe resolving this issue should require the tests to change. |
Hrm. I'd not appreciated that we have different behaviour from (Our output includes trailing |
Right, I feel like there are 3 options to what the behaviour could be:
1 and 3 can be done efficiently and simply with Preservation of the existing semantics could be done with a regex perhaps, but it's tricky to get right. Or maybe a straight python implementation that just doesn't start from the beginning every time, it wouldn't be as fast as the ones which call to C code, but it could at least be linear-time? Lemme know which direction you want to take it and I can give it a go.. |
lovelydinosaur
commented
Nov 30, 2022
I'd suggest that we've got the current behaviour wrong, and that we need a breaking API change here. |
ofek
commented
Dec 7, 2022
Yes imo |
After reading my comment pointing at a behavior change, it’s true that I would rather be expecting iter_lines() to return lines without newline separators at the end. This is what splitlines() does, but also other languages like Rust’s str.lines() (AdventOfCode season…), or JS when doing What are the ways this change could impact existing code? I don’t see many. If code is currently using iter_lines() and calling trim() on the result, they shouldn’t be impacted, only those trim calls would become unnecessary. People can’t be relying on the current behavior to reconstruct the original content either, so there shouldn’t be breakage on this use case either. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Handle text ending in `\r` more gracefully. Return as much content as possible.
lovelydinosaur
commented
Jan 10, 2023
Noticed that this implementation wouldn't return until Implementation update in d3c6a8e. Test change in 46cad89... decoder=LineDecoder()
assertdecoder.decode("") == []
# This will now return `["a", "", "b"]` and *only* buffer the last portion.assertdecoder.decode("a\r\rb\rc\r") == ["a", "", "b"] assertdecoder.flush() == ["c"] |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
cdeler
commented
Jan 13, 2023
LGTM (with a small style suggestion which can be easily ignored) |
zanieb
commented
Jan 13, 2023
Can we also update this pull request title to note the change in behavior since it's no longer just a change in algo complexity? |
Co-authored-by: cdeler <serj.krotov@gmail.com>
lovelydinosaur
commented
Mar 16, 2023
Okay, I think we can merge this in now. |
ofek
commented
Mar 16, 2023
This is very exciting, thanks! |
…nt speed up (encode#2423) * Replace quadratic algo in LineDecoder Leading to enormous speedups when doing things such as Response(...).iter_lines() as described on issue #2422 * Update httpx/_decoders.py * Update _decoders.py Handle text ending in `\r` more gracefully. Return as much content as possible. * Update test_decoders.py * Update _decoders.py * Update _decoders.py * Update _decoders.py * Update httpx/_decoders.py Co-authored-by: cdeler <serj.krotov@gmail.com> * Update _decoders.py --------- Co-authored-by: Tom Christie <tom@tomchristie.com> Co-authored-by: cdeler <serj.krotov@gmail.com>
Closes #2422
Leading to enormous speedups when doing things such as Response(...).iter_lines()