# gpui_shared_string: Avoid intermediate string allocation when deserializing a \`SharedString\` \(\#65089\) · gitcafe/zed

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

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

Visibility: public

Requested revision: 9dd6993e2e6e26baaf0fdfcb152cb823dba1135d

Requested commit: 9dd6993e2e6e26baaf0fdfcb152cb823dba1135d

Commit: 9dd6993e2e6e26baaf0fdfcb152cb823dba1135d

Tree: fb70ded8eb6138b0a2deff9ecf899c802371ced6

Author: Finn Evers

Committer: GitHub

## Message

```
gpui_shared_string: Avoid intermediate string allocation when deserializing a `SharedString` (#65089)

## Summary

This PR improves how we deserialize SharedStrings. Since we moved to
`SmolStr` under the hood some time ago, which takes a string by `impl
AsRef<str>` in its constructor, we can get rid here of allocating a
string where possible in favor of using a stringvisitor during
deserialization. This provides us with some decent improvements.

## Testing

The benchmark results are the following:

| Case | `from_str` before | `from_str` after | Δ | 
|---|---:|---:|---:|
| short (≤23 B, inline) | 23.1 µs | 11.6 µs | −50% 
| short, escaped | 33.2 µs | 22.4 µs | −32% | 
| long (~66 B, heap) | 46.9 µs | 31.4 µs | −33% | 
| long, escaped | 307.6 µs | 267.6 µs | −13% | 

## Self-review

- [X] I've reviewed my diff for quality, security, reliability, and
performance.
- [-] UI changes follow the
[checklist](https://zed.dev/docs/development/ui-checklist).
- [-] Tests cover the new or changed behavior.

Release Notes:

- N/A
- [GPUI] Improved memory usage and performance when deserializing
SharedStrings.
```

## Parents

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

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