Skip to content

Name::from<&str> always heap-allocates which is a footgun #24465

@beicause

Description

@beicause

Bevy version and features

main

What went wrong

Name internally is Cow<'static, str>, but creating Name by "str".into is always heap-allocated even if &str has static lifetime:

fn from(name: &str) -> Self {
Name::new(name.to_owned())
}

The correct way is Name::new("str")

pub fn new(name: impl Into<Cow<'static, str>>) -> Self {
Self(HashedStr(Hashed::new(name.into())))
}

To avoid the unexpected allocation I suggest change impl From<&str> for Name to impl From<&'static str> for Name

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-UtilsUtility functions and typesC-BugAn unexpected or incorrect behaviorC-PerformanceA change motivated by improving speed, memory usage or compile timesD-StraightforwardSimple bug fixes and API improvements, docs, test and examplesS-Ready-For-ImplementationThis issue is ready for an implementation PR. Go for it!

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions