# String optimisation in Julia

**URL:** https://discourse.julialang.org/t/string-optimisation-in-julia/119301
**Category:** General Usage
**Tags:** performance, strings, io
**Created:** [September 11, 2024, 10:56pm UTC](https://discourse.julialang.org/t/string-optimisation-in-julia/119301 "2024-09-11T22:56:05Z")
**Posts on this page:** 2
**Page:** 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: [September 21, 2024, 2:41pm UTC](https://discourse.julialang.org/t/string-optimisation-in-julia/119301/21 "2024-09-21T14:41:43Z")

</div>

> [@nhz2](#):
>
> This is about 10x faster than the original for me:

> <https://github.com/JuliaLang/julia/issues/55835>
>
> Related to a discussion on \[Discourse\](https://discourse.julialang.org/t/string-…optimisation-in-julia/119301/10?u=gdalle), I noticed that \`show(io, number)\` defaults to \`write(io, string(number))\` or similar (see \[here\](https://github.com/JuliaLang/julia/blob/master/base/show.jl#L1244-L1246) for integers and \[here\](https://github.com/JuliaLang/julia/blob/master/base/ryu/Ryu.jl#L112-L120) for floats). This involves allocating a temporary buffer. However, if \`io\` is an \`IOBuffer\`, we should be able to use the \`IOBuffer\` itself as the buffer to write into, saving us an allocation.
> 
> It seems fairly straightforward to fix this, albeit tedious. Basically some refactoring of the existing \`string(n)\` methods a function that takes a pre-allocated buffer, which can be a view into the IOBuffer or a new \`StringVector\` as needed.

---

<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: [September 21, 2024, 2:42pm UTC](https://discourse.julialang.org/t/string-optimisation-in-julia/119301/22 "2024-09-21T14:42:29Z")

</div>

> [@stevengj](#):
>
> As for `write(io, string(n))` vs. `print(io, n)`, I’m not sure what is going on (if there is really a difference?), because `print(io, n)` calls [`show(io, n)` which calls `write(io, string(n))`](https://github.com/JuliaLang/julia/blob/master/base/show.jl#L1244) already?

Possibly due to:

> <https://github.com/JuliaLang/julia/issues/55834>
>
> I notice a missing optimization: \`try foo(); finally; end\` is not optimized to \`…foo()\`. For example, look at \`@code\_native foo(3)\` for \`foo(x) = try; return x+1; finally; end\` — there is a whole bunch of overhead (\`\_ijl\_excstack\_state\`, \`\_ijl\_enter\_handler\`, \`\_sigsetjmp\`, …) compared to \`x+1\`.
> 
> I noticed this because (related to #55754) I was looking at \`print(io, x)\` performance on \`IOBuffer\`s. The fallback for \`print(io, x)\` is \[defined as\](https://github.com/JuliaLang/julia/blob/911e02558d0c145a192facd28808b68e157aa5af/base/strings/io.jl#L32-L40):
> \`\`\`jl
> function print(io::IO, x)
> lock(io)
> try
> show(io, x)
> finally
> unlock(io)
> end
> return nothing
> end
> \`\`\`
> but the \[default \`lock(io)\` and \`unlock(io)\` methods\](https://github.com/JuliaLang/julia/blob/911e02558d0c145a192facd28808b68e157aa5af/base/io.jl#L26-L27) do \`nothing\`. So, on an \`IOBuffer\` at least, this whole function \*should\* be optimized to just \`(show(io, x); nothing)\`, but it isn't.

[Previous page](https://discourse.julialang.org/t/string-optimisation-in-julia/119301.md?page=1)
