# Why string interning when strings are immutable?

**URL:** https://discourse.julialang.org/t/why-string-interning-when-strings-are-immutable/123417
**Category:** New to Julia
**Tags:** question
**Created:** [December 3, 2024, 4:39pm UTC](https://discourse.julialang.org/t/why-string-interning-when-strings-are-immutable/123417 "2024-12-03T16:39:14Z")
**Posts on this page:** 1
**Showing post:** 2

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [December 3, 2024, 5:10pm UTC](https://discourse.julialang.org/t/why-string-interning-when-strings-are-immutable/123417/2 "2024-12-03T17:10:16Z")

</div>

> [@world-peace](#):
>
> if they are not, then why is this same optimization not applied to regular strings?

There is a lot of overhead to interning a string, which wouldn’t be acceptable for generic runtime string manipulations.

It makes sense for symbols that are used in metaprogramming, because usually you only have a relatively small number of symbols, and runtime overhead in generating expressions is less of a concern (since expression generation usually occurs only once, at compile time). Symbols are also used for tagging types and other “symbolic label” purposes unrelated to metaprogramming, for which interning is important for performance — e.g. if you have a `MyType{:foo}`, or are passing `:foo` as an enum-like symbolic constant to a function.

That being said, if you have a specialized application where string interning makes sense, but you still want to support generic string operations like searching (which don’t work for `Symbol`), you can use InternedStrings.jl — see also the discussion in [[ANN] InternedStrings.jl: Allocate strings once and reuse them](https://discourse.julialang.org/t/ann-internedstrings-jl-allocate-strings-once-and-reuse-them/7344)

---

_[View the full topic](https://discourse.julialang.org/t/why-string-interning-when-strings-are-immutable/123417)._
