Skip to content

Commit 9323ee1

Browse files
committed
Nospecialize ::Channel functions that don't depend on {T}.
- Mark most of the functions on Channels nospecialize when they don't depend on the data type in the Channel, `T`. - Add precompile statement for `Base.close(::Channel, ::Exception)`
1 parent 3b993a9 commit 9323ee1

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

base/channels.jl

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,9 @@ end
164164

165165
closed_exception() = InvalidStateException("Channel is closed.", :closed)
166166

167-
isbuffered(c::Channel) = c.sz_max==0 ? false : true
167+
@nospecialize isbuffered(c::Channel) = c.sz_max==0 ? false : true
168168

169-
function check_channel_state(c::Channel)
169+
@nospecialize function check_channel_state(c::Channel)
170170
if !isopen(c)
171171
# if the monotonic load succeed, now do an acquire fence
172172
(@atomic :acquire c.state) === :open && concurrency_violation()
@@ -183,8 +183,8 @@ Close a channel. An exception (optionally given by `excp`), is thrown by:
183183
* [`put!`](@ref) on a closed channel.
184184
* [`take!`](@ref) and [`fetch`](@ref) on an empty, closed channel.
185185
"""
186-
close(c::Channel) = close(c, closed_exception()) # nospecialize on default arg seems to confuse makedocs
187-
function close(c::Channel, @nospecialize(excp::Exception))
186+
close(@nospecialize(c::Channel)) = close(c, closed_exception()) # nospecialize on default arg seems to confuse makedocs
187+
@nospecialize function close(c::Channel, excp::Exception)
188188
lock(c)
189189
try
190190
c.excp = excp
@@ -197,7 +197,7 @@ function close(c::Channel, @nospecialize(excp::Exception))
197197
end
198198
nothing
199199
end
200-
isopen(c::Channel) = ((@atomic :monotonic c.state) === :open)
200+
@nospecialize isopen(c::Channel) = ((@atomic :monotonic c.state) === :open)
201201

202202
"""
203203
bind(chnl::Channel, task::Task)
@@ -251,7 +251,7 @@ Stacktrace:
251251
[...]
252252
```
253253
"""
254-
function bind(c::Channel, task::Task)
254+
@nospecialize function bind(c::Channel, task::Task)
255255
T = Task(() -> close_chnl_on_taskdone(task, c))
256256
_wait2(task, T)
257257
return c
@@ -283,7 +283,7 @@ function channeled_tasks(n::Int, funcs...; ctypes=fill(Any,n), csizes=fill(0,n))
283283
return (chnls, tasks)
284284
end
285285

286-
function close_chnl_on_taskdone(t::Task, c::Channel)
286+
@nospecialize function close_chnl_on_taskdone(t::Task, c::Channel)
287287
isopen(c) || return
288288
lock(c)
289289
try
@@ -323,7 +323,7 @@ function put!(c::Channel{T}, v) where T
323323
end
324324

325325
# Atomically update channel n_avail, *assuming* we hold the channel lock.
326-
function _increment_n_avail(c, inc)
326+
@nospecialize function _increment_n_avail(c, inc)
327327
# We hold the channel lock so it's safe to non-atomically read and
328328
# increment c.n_avail_items
329329
newlen = c.n_avail_items + inc
@@ -516,17 +516,17 @@ true
516516
```
517517
518518
"""
519-
isready(c::Channel) = n_avail(c) > 0
520-
isempty(c::Channel) = n_avail(c) == 0
521-
function n_avail(c::Channel)
519+
@nospecialize isready(c::Channel) = n_avail(c) > 0
520+
@nospecialize isempty(c::Channel) = n_avail(c) == 0
521+
@nospecialize function n_avail(c::Channel)
522522
# Lock-free equivalent to `length(c.data) + length(c.cond_put.waitq)`
523523
@atomic :monotonic c.n_avail_items
524524
end
525525

526-
lock(c::Channel) = lock(c.cond_take)
527-
lock(f, c::Channel) = lock(f, c.cond_take)
528-
unlock(c::Channel) = unlock(c.cond_take)
529-
trylock(c::Channel) = trylock(c.cond_take)
526+
@nospecialize lock(c::Channel) = lock(c.cond_take)
527+
lock(f, @nospecialize(c::Channel)) = lock(f, c.cond_take)
528+
@nospecialize unlock(c::Channel) = unlock(c.cond_take)
529+
@nospecialize trylock(c::Channel) = trylock(c.cond_take)
530530

531531
"""
532532
wait(c::Channel)
@@ -552,7 +552,7 @@ julia> istaskdone(task) # task is now unblocked
552552
true
553553
```
554554
"""
555-
function wait(c::Channel)
555+
@nospecialize function wait(c::Channel)
556556
isready(c) && return
557557
lock(c)
558558
try

contrib/generate_precompile.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ precompile(Tuple{typeof(isassigned), Core.SimpleVector, Int})
4949
precompile(Tuple{typeof(getindex), Core.SimpleVector, Int})
5050
precompile(Tuple{typeof(Base.Experimental.register_error_hint), Any, Type})
5151
precompile(Tuple{typeof(Base.display_error), Base.ExceptionStack})
52+
precompile(Tuple{typeof(Base.close), Base.Channel, Exception})
5253
precompile(Tuple{Core.kwftype(typeof(Type)), NamedTuple{(:sizehint,), Tuple{Int}}, Type{IOBuffer}})
5354
precompile(Base.CoreLogging.current_logger_for_env, (Base.CoreLogging.LogLevel, String, Module))
5455
precompile(Base.CoreLogging.current_logger_for_env, (Base.CoreLogging.LogLevel, Symbol, Module))

0 commit comments

Comments
 (0)