Skip to content

case where immutable struct is 30% slower than mutable (and much slower on 1.6-dev) #38936

@clarkevans

Description

@clarkevans

To slow this script down by 30%, remove mutable in front of struct Bypass

mutable struct Bypass{T} content::T end
Base.print(io::IO, x::Bypass) = print(io, x.content)
struct Reprint content::Function end
Base.print(io::IO, t::Reprint) = t.content(io)

function printable()
    parts = (Bypass("<span>"), "data", Bypass("</span"))
    Reprint() do io::IO
        for x in parts
             print(io, x)
        end
    end
end

harness() = begin
   io = IOBuffer()
   ob = printable()
   print(io, ob)
end

using BenchmarkTools
println(@benchmark harness())
Julia Version 1.5.3
Commit 788b2c77c1 (2020-11-09 13:37 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Celeron(R) CPU J3455 @ 1.50GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-9.0.1 (ORCJIT, goldmont)

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions