-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Improve implementation of rstrip and lstrip #22496
Conversation
`chomp` uses `SubString` so `chomp!` is not needed to ensure that copying of big string is not performed.
`chomp!` was only used in `readchomp` and I propose to replace it with `chomp` there. As `chomp!` was not exported I understand that it can be removed without deprecation.
In this way copying of string is avoided, and in `strip` currently the string is actually copied twice. A basic benchmark is given below: ``` julia> using BenchmarkTools julia> s = " "^10*"x"^1000*" "^10; julia> @benchmark strip(s) # current definition BenchmarkTools.Trial: memory estimate: 2.13 KiB allocs estimate: 2 -------------- minimum time: 502.056 ns (0.00% GC) median time: 537.579 ns (0.00% GC) mean time: 631.383 ns (5.74% GC) maximum time: 6.003 μs (0.00% GC) -------------- samples: 10000 evals/sample: 197 julia> @benchmark strip(s) # with SubString definition BenchmarkTools.Trial: memory estimate: 64 bytes allocs estimate: 2 -------------- minimum time: 334.062 ns (0.00% GC) median time: 339.819 ns (0.00% GC) mean time: 362.150 ns (1.00% GC) maximum time: 8.236 μs (93.19% GC) -------------- samples: 10000 evals/sample: 243 ```
The change to |
BTW, the test failure is due to master being broken (fix in #22506). |
It looks like out of all registered packages, Compat might be the only one that is using |
I have updated the PR to keep |
base/io.jl
Outdated
""" | ||
readchomp(x) = chomp!(readstring(x)) | ||
readchomp(x) = chomp(readstring(x)) |
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.
Leave this out too since that's unrelated to the other change. Either we should remove chomp!
and stop using it everywhere, or we should keep the code as-is.
OK. Leaving only |
Perhaps deserves a NEWS-entry? |
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.
There's actually a trailing space in NEWS.md, which explains the CI failure. Otherwise, looks good.
should be squashed, on merge if not before |
* change chomp! to chomp in readchomp `chomp` uses `SubString` so `chomp!` is not needed to ensure that copying of big string is not performed. * Remove chomp! function `chomp!` was only used in `readchomp` and I propose to replace it with `chomp` there. As `chomp!` was not exported I understand that it can be removed without deprecation. * Make rstrip and lstrip use SubString In this way copying of string is avoided, and in `strip` currently the string is actually copied twice. A basic benchmark is given below: ``` julia> using BenchmarkTools julia> s = " "^10*"x"^1000*" "^10; julia> @benchmark strip(s) # current definition BenchmarkTools.Trial: memory estimate: 2.13 KiB allocs estimate: 2 -------------- minimum time: 502.056 ns (0.00% GC) median time: 537.579 ns (0.00% GC) mean time: 631.383 ns (5.74% GC) maximum time: 6.003 μs (0.00% GC) -------------- samples: 10000 evals/sample: 197 julia> @benchmark strip(s) # with SubString definition BenchmarkTools.Trial: memory estimate: 64 bytes allocs estimate: 2 -------------- minimum time: 334.062 ns (0.00% GC) median time: 339.819 ns (0.00% GC) mean time: 362.150 ns (1.00% GC) maximum time: 8.236 μs (93.19% GC) -------------- samples: 10000 evals/sample: 243 ``` * Correct strip tests * Bring back chomp! * reverse removal of chomp! * Add NEWS entry for lstrip and rstrip. * remove trailing whitespace in NEWS.md
strip on 1.0 returns a substring now (see JuliaLang/julia#22496). It wasn't ever very necessary anyway.
I propose to consider the following:
lstrip
andrstrip
to useSubString
instead of allocating a new string;chomp!
without deprecation period; it is not exported and falls back tochomp
anyway;readchomp
to usechomp
instead ofchomp!
.