Skip to content

lazy_static uses spinlocks in my with-std crate #150

Open
@RalfJung

Description

@RalfJung

I have a tiny crate (really just a crate to test miri) where I noticed that after a cargo update, lazy_static depends on spin. Or, at least, spin gets compiled. (If you want to reproduce, get that folder and do cargo update; I couldn't land the updated version yet due to other problems.)

Isn't that a problem? The fact that spin_no_std is a feature means that if any create enables it, then globally all lazy statics will use a spin lock. Given that there is no way to know what kind of computation is done in the initializer, spinning instead of properly putting waiting threads to sleep seems like a huge waste of CPU cycles.

I am not entirely sure what exactly is enabling the feature in my case, the reverse dependency graph of spin is not very big:

spin v0.5.0
└── lazy_static v1.3.0
    ├── c2-chacha v0.2.2
    │   └── rand_chacha v0.2.0
    │       └── rand v0.7.0
    │           [dev-dependencies]
    │           └── cargo-miri-test v0.1.0 (/home/r/src/rust/miri/test-cargo-miri)
    └── getrandom v0.1.5
        ├── rand v0.7.0 (*)
        └── rand_core v0.5.0
            ├── rand v0.7.0 (*)
            ├── rand_chacha v0.2.0 (*)
            ├── rand_hc v0.2.0
            │   [dev-dependencies]
            │   └── rand v0.7.0 (*)
            └── rand_pcg v0.2.0
                └── rand v0.7.0 (*)
                [dev-dependencies]
                └── rand v0.7.0 (*)

@Mark-Simulacrum on Zulip suggested that Cargo ignores cfg restrictions for feature flags, and thus getrandom is likely the trigger here. Cc @newpavlov

FWIW, the fact that there is a negated term ("no") in the feature also indicates this is probably a bad approach, see the feature name guidelines. Cargo features are additive; creates can only ask for a feature to be enabled but never ask for a feature to be disabled.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions