Skip to content

Commit e6e4c3d

Browse files
committed
Re-order functions in MultiThreadedCacches.jl
Put `show()` functions at the end
1 parent 492a4f8 commit e6e4c3d

File tree

1 file changed

+36
-37
lines changed

1 file changed

+36
-37
lines changed

src/MultiThreadedCaches.jl

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -87,43 +87,6 @@ function init_cache!(cache::MultiThreadedCache{K,V}) where {K,V}
8787
return cache
8888
end
8989

90-
function Base.show(io::IO, cache::MultiThreadedCache{K,V}) where {K,V}
91-
# Contention optimization: don't hold the lock while printing, since that could block
92-
# for an arbitrarily long time. Instead, print the data to an intermediate buffer first.
93-
# Note that this has the same CPU complexity, since printing is already O(n).
94-
iobuf = IOBuffer()
95-
let io = IOContext(iobuf, io)
96-
Base.@lock cache.base_cache_lock begin
97-
_oneline_show(io, cache)
98-
end
99-
end
100-
# Now print the data without holding the lock.
101-
seekstart(iobuf)
102-
write(io, read(iobuf))
103-
return nothing
104-
end
105-
_oneline_show(io::IO, cache::MultiThreadedCache{K,V}) where {K,V} =
106-
print(io, "$(MultiThreadedCache{K,V})(", cache.base_cache, ")")
107-
108-
function Base.show(io::IO, mime::MIME"text/plain", cache::MultiThreadedCache{K,V}) where {K,V}
109-
# Contention optimization: don't hold the lock while printing. See above for more info.
110-
iobuf = IOBuffer()
111-
let io = IOContext(iobuf, io)
112-
Base.@lock cache.base_cache_lock begin
113-
if isempty(cache.base_cache)
114-
_oneline_show(io, cache)
115-
else
116-
print(io, "$(MultiThreadedCache): ")
117-
Base.show(io, mime, cache.base_cache)
118-
end
119-
end
120-
end
121-
# Now print the data without holding the lock.
122-
seekstart(iobuf)
123-
write(io, read(iobuf))
124-
return nothing
125-
end
126-
12790
# Based upon the thread-safe Global RNG implementation in the Random stdlib:
12891
# https://github.com/JuliaLang/julia/blob/e4fcdf5b04fd9751ce48b0afc700330475b42443/stdlib/Random/src/RNGs.jl#L369-L385
12992
# Get or lazily construct the per-thread cache when first requested.
@@ -262,5 +225,41 @@ function Base.get!(func::Base.Callable, cache::MultiThreadedCache{K,V}, key) whe
262225
end
263226
end
264227

228+
function Base.show(io::IO, cache::MultiThreadedCache{K,V}) where {K,V}
229+
# Contention optimization: don't hold the lock while printing, since that could block
230+
# for an arbitrarily long time. Instead, print the data to an intermediate buffer first.
231+
# Note that this has the same CPU complexity, since printing is already O(n).
232+
iobuf = IOBuffer()
233+
let io = IOContext(iobuf, io)
234+
Base.@lock cache.base_cache_lock begin
235+
_oneline_show(io, cache)
236+
end
237+
end
238+
# Now print the data without holding the lock.
239+
seekstart(iobuf)
240+
write(io, read(iobuf))
241+
return nothing
242+
end
243+
_oneline_show(io::IO, cache::MultiThreadedCache{K,V}) where {K,V} =
244+
print(io, "$(MultiThreadedCache{K,V})(", cache.base_cache, ")")
245+
246+
function Base.show(io::IO, mime::MIME"text/plain", cache::MultiThreadedCache{K,V}) where {K,V}
247+
# Contention optimization: don't hold the lock while printing. See above for more info.
248+
iobuf = IOBuffer()
249+
let io = IOContext(iobuf, io)
250+
Base.@lock cache.base_cache_lock begin
251+
if isempty(cache.base_cache)
252+
_oneline_show(io, cache)
253+
else
254+
print(io, "$(MultiThreadedCache): ")
255+
Base.show(io, mime, cache.base_cache)
256+
end
257+
end
258+
end
259+
# Now print the data without holding the lock.
260+
seekstart(iobuf)
261+
write(io, read(iobuf))
262+
return nothing
263+
end
265264

266265
end # module

0 commit comments

Comments
 (0)