Skip to content
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

Optimize show(io::IO, m::Module) #42773

Merged
merged 1 commit into from
Oct 31, 2021
Merged

Conversation

Sacha0
Copy link
Member

@Sacha0 Sacha0 commented Oct 23, 2021

This method appeared as a hotspot in code that stringifies many types.

               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.8.0-DEV.702 (2021-10-10)
 _/ |\__'_|_|_|\__'_|  |  master/82257229c9 (fork: 1 commits, 12 days)
|__/                   |

julia> using BenchmarkTools

julia> module Foo
       module Bar
       module Baz
       module Qux
       end
       end
       end
       end
Main.Foo

julia> @benchmark show(devnull, Foo.Bar.Baz.Qux) # before replacement
BenchmarkTools.Trial: 10000 samples with 26 evaluations.
 Range (min … max):  889.500 ns …  59.991 μs  ┊ GC (min … max): 0.00% … 97.99%
 Time  (median):     978.615 ns               ┊ GC (median):    0.00%
 Time  (mean ± σ):     1.052 μs ± 994.931 ns  ┊ GC (mean ± σ):  1.56% ±  1.70%

   ▇▆█▂
  ▆████▅▅▆▅▇▅▃▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▁▂▂▂▂▂▂▂▂▂▂▂▁▂▂▁▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂ ▃
  890 ns           Histogram: frequency by time         2.41 μs <

 Memory estimate: 640 bytes, allocs estimate: 17.

julia> @eval Base begin
           function show(io::IO, m::Module)
               if Base.is_root_module(m)
                   print(io, nameof(m))
               else
                   print_fullname(io, m)
               end
           end
           function print_fullname(io::IO, m::Module)
               _print_fullname(io, parentmodule(m))
               print(io, nameof(m))
           end
           function _print_fullname(io::IO, m::Module)
               mp = parentmodule(m)
               if m === Main || m === Base || m === Core || mp === m
                   print(io, nameof(m))
                   print(io, '.')
               else
                   _print_fullname(io, mp)
                   print(io, nameof(m))
                   print(io, '.')
               end
           end
       end
_print_fullname (generic function with 1 method)

julia> @benchmark show(devnull, Foo.Bar.Baz.Qux) # after replacement
BenchmarkTools.Trial: 10000 samples with 991 evaluations.
 Range (min … max):  39.268 ns … 227.557 ns  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     44.397 ns               ┊ GC (median):    0.00%
 Time  (mean ± σ):   45.804 ns ±   7.879 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

  ▂▆▆▁▁▄█▅ ▂▇▄
  █████████████▆▅▆▅▅▅▄▃▃▃▃▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▁▁▂▂▁▂▂▂▂▂▂▂▂▂▂ ▄
  39.3 ns         Histogram: frequency by time         80.1 ns <

 Memory estimate: 0 bytes, allocs estimate: 0.

base/show.jl Outdated Show resolved Hide resolved
Copy link
Member

@NHDaly NHDaly left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM modulo the suggested fix. (Maybe we need a small unit test for this?)

show(io::IO, m::Module) allocates. This commit provides
an implementation that does not allocate, improving perf.
@Sacha0 Sacha0 force-pushed the sv-optimize-module-show branch from cb5bc66 to 97fb3a3 Compare October 29, 2021 21:55
@Sacha0 Sacha0 added backport 1.7 backport 1.6 Change should be backported to release-1.6 labels Oct 29, 2021
@Sacha0
Copy link
Member Author

Sacha0 commented Oct 29, 2021

Tagged for backport to 1.6 and 1.7 as a low-risk optimization :).

Copy link
Member

@NHDaly NHDaly left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@KristofferC KristofferC merged commit ca6b3ba into master Oct 31, 2021
@KristofferC KristofferC deleted the sv-optimize-module-show branch October 31, 2021 18:04
KristofferC pushed a commit that referenced this pull request Oct 31, 2021
show(io::IO, m::Module) allocates. This commit provides
an implementation that does not allocate, improving perf.

(cherry picked from commit ca6b3ba)
@Sacha0
Copy link
Member Author

Sacha0 commented Nov 1, 2021

Thanks for reviewing/merging gentlemen! :)

KristofferC pushed a commit that referenced this pull request Nov 7, 2021
show(io::IO, m::Module) allocates. This commit provides
an implementation that does not allocate, improving perf.

(cherry picked from commit ca6b3ba)
KristofferC pushed a commit that referenced this pull request Nov 11, 2021
show(io::IO, m::Module) allocates. This commit provides
an implementation that does not allocate, improving perf.

(cherry picked from commit ca6b3ba)
KristofferC pushed a commit that referenced this pull request Nov 12, 2021
show(io::IO, m::Module) allocates. This commit provides
an implementation that does not allocate, improving perf.

(cherry picked from commit ca6b3ba)
@KristofferC KristofferC removed backport 1.6 Change should be backported to release-1.6 backport 1.7 labels Nov 13, 2021
@gustaphe
Copy link

gustaphe commented Nov 16, 2021

Why not join(io, fullname(module), ".")?

@KristofferC
Copy link
Member

Why not join(io, fullname(module), ".")?

The PR here was for performance as shown in the first post. Did you benchmark that version and find that it performs the same as the one proposed here?

@gustaphe
Copy link

My bad, fullname of course allocates. I didn't realize this recursive method didn't use the fullname tuple.

(the join(io...) version is inbetween the two methods in the PR for speed)

LilithHafner pushed a commit to LilithHafner/julia that referenced this pull request Feb 22, 2022
show(io::IO, m::Module) allocates. This commit provides
an implementation that does not allocate, improving perf.
LilithHafner pushed a commit to LilithHafner/julia that referenced this pull request Mar 8, 2022
show(io::IO, m::Module) allocates. This commit provides
an implementation that does not allocate, improving perf.
staticfloat pushed a commit that referenced this pull request Dec 23, 2022
show(io::IO, m::Module) allocates. This commit provides
an implementation that does not allocate, improving perf.

(cherry picked from commit ca6b3ba)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants