# language: Add \`LanguageName::new_static\` to reduce allocations \(\#44380\) · gitcafe/zed

[View on GitCafe](https://git.cafe/gitcafe/zed/commit/387059c6b2b72c8c3f868c932401fccfc635c95a)

Repository: [gitcafe/zed](https://git.cafe/gitcafe/zed)

Visibility: public

Requested revision: 387059c6b2b72c8c3f868c932401fccfc635c95a

Requested commit: 387059c6b2b72c8c3f868c932401fccfc635c95a

Commit: 387059c6b2b72c8c3f868c932401fccfc635c95a

Tree: 4a93b73535fb22fdb09b8340368313ef5dda9ae1

Author: tidely

Committer: GitHub

## Message

````
language: Add `LanguageName::new_static` to reduce allocations (#44380)

Implements a specialized constructor `LanguageName::new_static` for
`&'static str` which reduces allocations.

`LanguageName::new` always backs the underlying `SharedString` with an
owned `Arc<str>` even when a `&'static str` is passed. This makes us
allocate each time we create a new `LanguageName` no matter what.
Creating a specialized constructor for `&'static str` allows us to
essentially construct them for free.

Additional change:
Encourages using explicit constructors to avoid needless allocations.
Currently there were no instances of this trait being called where the
lifetime was not `'static` saving another 48 locations of allocation.

```rust
impl<'a> From<&'a str> for LanguageName {
    fn from(str: &'a str) -> Self {
        Self(SharedString::new(str))
    }
}

// to 

impl From<&'static str> for LanguageName {
    fn from(str: &'static str) -> Self {
        Self(SharedString::new_static(str))
    }
}

```

Release Notes:

- N/A
````

## Parents

- [4a382b27977e9d7dbc349553d045aa50f0635834](https://git.cafe/gitcafe/zed/commit/4a382b27977e9d7dbc349553d045aa50f0635834?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zed/tree/387059c6b2b72c8c3f868c932401fccfc635c95a?format=markdown)
