File tree Expand file tree Collapse file tree 1 file changed +15
-7
lines changed Expand file tree Collapse file tree 1 file changed +15
-7
lines changed Original file line number Diff line number Diff line change 28
28
# remove a handle from the cache, or create a new one
29
29
function Base. pop! (cache:: HandleCache{K,V} , key:: K ) where {K,V}
30
30
# check the cache
31
- handle = @lock cache. lock begin
32
- if ! haskey (cache. idle_handles, key) || isempty (cache. idle_handles[key])
33
- nothing
31
+ handle, num_active_handles = @lock cache. lock begin
32
+ if haskey (cache. idle_handles, key) && ! isempty (cache. idle_handles[key])
33
+ pop! (cache . idle_handles[key]), length (cache . active_handles)
34
34
else
35
- pop! (cache. idle_handles[key] )
35
+ nothing , length (cache. active_handles )
36
36
end
37
37
end
38
38
39
- # if we didn't find anything, create a new handle.
40
- # we could (and used to) run `GC.gc(false)` here to free up old handles,
41
- # but that can be expensive when using lots of short-lived tasks.
39
+ # if we didn't find anything, but lots of handles are active, try to free some
40
+ if handle === nothing && num_active_handles > cache. max_entries
41
+ GC. gc (false )
42
+ @lock cache. lock begin
43
+ if haskey (cache. idle_handles, key) && isempty (cache. idle_handles[key])
44
+ handle = pop! (cache. idle_handles[key])
45
+ end
46
+ end
47
+ end
48
+
49
+ # if we still didn't find anything, create a new handle
42
50
if handle === nothing
43
51
CUDA. maybe_collect ()
44
52
handle = cache. ctor (key)
You can’t perform that action at this time.
0 commit comments