Skip to content

const std::lazy::Lazy is a performance footgun when "shared" between threads #82842

Closed
@mejrs

Description

@mejrs

I have a regex that I only want to initialize once, which is why I wrapped it in a lazy_static. However when using the unstable lazy api (see #74465) instead, sharing it between threads causes the initialization function to run every time.

Playground

Note that if static is used rather than const, it (correctly) fails to compile as it's not Sync.

const SyncLazy has the same issue, but not static SyncLazy, which is the correct thing to use here.

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