Skip to content

Commit

Permalink
Makes IntrusiveLinkedListSynchronized mutable to avoid allocation on …
Browse files Browse the repository at this point in the history
…poptask (JuliaLang#50802)

Currently `poptask` is allocating every time it calls into
`jl_task_get_next` due to `StickyWorkqueue`
(`IntrusiveLinkedListSynchronized`) being immutable and requiring an
allocation to be used as `Any` on the `ccall`.

The allocations can be seen in the following snippet:
```
function work()
    done = 0.0
    l = Threads.SpinLock()
    Threads.@sync for _ in 1:(Threads.nthreads() / 2)
        Threads.@Spawn begin
            v = 1.0
            for i in 1:(1<<33)
                v *= 1.0001
                if i % (1 << 13) == 0
                    yield()
                end
            end
            Base.@lock_nofail l done += v
        end
    end
    return done
end

julia> @time work()
 15.758794 seconds (5.03 M allocations: 153.523 MiB, 0.35% gc time)
```

Which after the change becomes:
```
julia> @time work()
 15.907513 seconds (67 allocations: 4.719 KiB)
```
  • Loading branch information
andrebsguedes authored and RAI CI (GitHub Action Automation) committed Oct 20, 2023
1 parent 2ebc890 commit 747fb2f
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion base/task.jl
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ end

## scheduler and work queue

struct IntrusiveLinkedListSynchronized{T}
mutable struct IntrusiveLinkedListSynchronized{T}
queue::IntrusiveLinkedList{T}
lock::Threads.SpinLock
IntrusiveLinkedListSynchronized{T}() where {T} = new(IntrusiveLinkedList{T}(), Threads.SpinLock())
Expand Down

0 comments on commit 747fb2f

Please sign in to comment.