# Reference String Based on Instance?

**URL:** https://discourse.julialang.org/t/reference-string-based-on-instance/9368
**Category:** Internals & Design
**Created:** [February 27, 2018, 3:48pm UTC](https://discourse.julialang.org/t/reference-string-based-on-instance/9368 "2018-02-27T15:48:04Z")
**Posts on this page:** 1
**Showing post:** 4

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [February 27, 2018, 7:13pm UTC](https://discourse.julialang.org/t/reference-string-based-on-instance/9368/4 "2018-02-27T19:13:00Z")

</div>

At what point would you do the dictionary lookup? Any time a string object is created? That would be quite expensive. Interned objects like symbols are never garbage collected so interning all strings unconditionally would mean that we never free the storage for any string object which would be fairly impractical.

There is a package that provides an opt-in interned string type:

> [@\[ANN\] InternedStrings.jl: Allocate strings once and reuse them](https://discourse.julialang.org/t/ann-internedstrings-jl-allocate-strings-once-and-reuse-them/7344):
>
> This has been bothering me since I first started using julia 3 years ago. Julia has immutable strings, that are not interned. Late at night about a week ago I worked out how to solve it. Its not actually that hard. This package solves that, and it does so without breaking garbage collection. The full explanation and motivational rant is in the readme. [https://github.com/oxinabox/InternedStrings.jl](https://github.com/oxinabox/InternedStrings.jl) If someone wants to check the math there, and make a PR, I’ld appreciate it. My math says t…

> **[GitHub - JuliaString/InternedStrings.jl: Fully transparent string interning...](https://github.com/JuliaString/InternedStrings.jl)**
>
> Fully transparent string interning functionality, with proper garbage collection - GitHub - JuliaString/InternedStrings.jl: Fully transparent string interning functionality, with proper garbage col...

One possibility would be to automatically intern all sufficiently short strings. However, since a pointer to such an interned string is already 16 bytes on most systems, that solution is strictly dominated by storing short (\< 16-byte) strings inline. I mentioned that [previously](https://discourse.julialang.org/t/progress-towards-faster-sortperm-for-strings/8505/4) which led @xiaodai to prototype the idea here:

> **[GitHub - JuliaString/ShortStrings.jl: A fast implementation of short strings...](https://github.com/JuliaString/ShortStrings.jl)**
>
> A fast implementation of short strings of fixed size. Great for sorting and group-by operations - GitHub - JuliaString/ShortStrings.jl: A fast implementation of short strings of fixed size. Great f...

---

_[View the full topic](https://discourse.julialang.org/t/reference-string-based-on-instance/9368)._
