Skip to content

Implement PaddedSpinLock, which avoids false sharing.#55944

Merged
IanButterworth merged 13 commits intoJuliaLang:masterfrom
kuszmaul:master
May 11, 2025
Merged

Implement PaddedSpinLock, which avoids false sharing.#55944
IanButterworth merged 13 commits intoJuliaLang:masterfrom
kuszmaul:master

Conversation

@kuszmaul
Copy link
Copy Markdown
Contributor

@kuszmaul kuszmaul commented Sep 30, 2024

Implemented PaddedSpinLock to help avoid false sharing.

Defines a new AbstractSpinLock with SpinLock <: AbstractSpinLock and
PaddedSpinLock <: AbstractSpinLock.

Fixes #55942.

Defines a new `AbstractSpinLock` with `SpinLock <: AbstractSpinLock` and
`PaddedSpinLock <: AbstractSpinLock`.

Fixes 55942.
@nsajko nsajko added parallelism Parallel or distributed computation feature Indicates new feature / enhancement requests labels Sep 30, 2024
Comment thread base/locks-mt.jl Outdated
@nsajko nsajko added needs docs Documentation for this change is required needs news A NEWS entry is required for this change labels Sep 30, 2024
@nsajko
Copy link
Copy Markdown
Member

nsajko commented Sep 30, 2024

Explanations for the labels, given that this is your first PR:

Marking as "needs docs" because the doc string should be included into the documentation document (HTML/PDF).

Marking as "needs news" because there's no news entry added, see NEWS.md.

Comment thread NEWS.md Outdated
Comment thread NEWS.md Outdated
kuszmaul and others added 2 commits September 30, 2024 15:58
Co-authored-by: Neven Sajko <s@purelymail.com>
Co-authored-by: Neven Sajko <s@purelymail.com>
@nsajko nsajko removed the needs news A NEWS entry is required for this change label Sep 30, 2024
@nsajko
Copy link
Copy Markdown
Member

nsajko commented Sep 30, 2024

The doc strings may be included into the built docs by adding the lines for the new types around here:

Base.Threads.SpinLock

Comment thread test/threads_exec.jl
end
end

if threadpoolsize() > 1
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

AFAIK it's a good idea to wrap new tests into a @testset.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

@topolarity
Copy link
Copy Markdown
Member

Why introduce a new PaddedSpinLock rather than improve the implementation of SpinLock?

@oscardssmith
Copy link
Copy Markdown
Member

If you are including the SpinLock in a struct, you often will want to use the other fields of the struct as padding.

@kuszmaul
Copy link
Copy Markdown
Contributor Author

kuszmaul commented Oct 1, 2024

Why introduce a new PaddedSpinLock rather than improve the implementation of SpinLock?

The unpadded SpinLock may be useful in some situations: It's quite a bit smaller than the padded one. I could imagine, for example, someone putting a SpinLock on every vertex of a graph, and being sensitive to the size of the SpinLock. In that case, a little false sharing might be OK. I didn't want to break anyone's code, so I made a new lock..

@kuszmaul
Copy link
Copy Markdown
Contributor Author

kuszmaul commented Oct 1, 2024

If you are including the SpinLock in a struct, you often will want to use the other fields of the struct as padding.

I'm not sure whether that's true.. As I understand it (and I could be wrong) a SpInLock is a mutable struct, so when you include it in another struct, the other fields of the containing struct don't act as padding. All that's stored in the containing struct is a pointer to the SpinLock, and if two such SpinLock's end up on the same cache line, you'll get false sharing. This kind of thing is more likely when using, for example, JEMalloc than some other malloc implementations, since JEMalloc will pack 8-byte objects together on the same cacheline or page.

@nsajko
Copy link
Copy Markdown
Member

nsajko commented Oct 1, 2024

Probably want to add a public statement for the new types. Around here:

export SpinLock

@kuszmaul
Copy link
Copy Markdown
Contributor Author

kuszmaul commented Oct 1, 2024

Thanks for the various suggestions and the help with process.

Copy link
Copy Markdown
Contributor

@nickrobinson251 nickrobinson251 left a comment

Choose a reason for hiding this comment

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

a small docs suggestion

remember to add the new type to the online documentation by adding it here:

Base.Threads.SpinLock

and either mark it public or export it

Comment thread base/locks-mt.jl
Comment thread base/locks-mt.jl
@gbaraldi
Copy link
Copy Markdown
Member

Pending review comments LGTM!

@gbaraldi gbaraldi added merge me PR is reviewed. Merge when all tests are passing and removed needs docs Documentation for this change is required labels Nov 11, 2024
@ViralBShah
Copy link
Copy Markdown
Member

The test failures look real.

@ViralBShah ViralBShah removed the merge me PR is reviewed. Merge when all tests are passing label Nov 12, 2024
Comment thread test/threads_exec.jl Outdated
@adienes adienes added the performance Must go faster label May 5, 2025
ViralBShah and others added 2 commits May 5, 2025 11:19
Co-authored-by: adienes <51664769+adienes@users.noreply.github.com>
@adienes adienes added the merge me PR is reviewed. Merge when all tests are passing label May 11, 2025
@IanButterworth IanButterworth merged commit 1b1b5d5 into JuliaLang:master May 11, 2025
8 checks passed
@ViralBShah
Copy link
Copy Markdown
Member

Should this be backported?

@oscardssmith
Copy link
Copy Markdown
Member

no. it's a feature

@DilumAluthge DilumAluthge removed the merge me PR is reviewed. Merge when all tests are passing label May 12, 2025
charleskawczynski pushed a commit to charleskawczynski/julia that referenced this pull request May 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature Indicates new feature / enhancement requests parallelism Parallel or distributed computation performance Must go faster

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SpinLocks suffer from false sharing

10 participants