# Why is my own simplified IO type slower than Base.IOBuffer?

**URL:** <https://discourse.julialang.org/t/why-is-my-own-simplified-io-type-slower-than-base-iobuffer/95308>\
**Category:** Performance\
**Tags:** io, speed-optimization\
**Created:** [February 28, 2023, 5:12am UTC](https://discourse.julialang.org/t/why-is-my-own-simplified-io-type-slower-than-base-iobuffer/95308 "2023-02-28T05:12:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![cadaverous](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cadaverous/32/47289_2.png) [@cadaverous](https://discourse.julialang.org/u/cadaverous)\
**Post date:** [February 28, 2023, 5:12am UTC](https://discourse.julialang.org/t/why-is-my-own-simplified-io-type-slower-than-base-iobuffer/95308/1 "2023-02-28T05:12:32Z")

</div>

I am trying to extend Base to include an IO buffer type, similar to DevNull, which just throws away all data written to it, but keeps track of a pointer, in order to find the serialized size of an object. (as suggested in a response to one of my previous questions on here)

```julia
mutable struct BlackHoleBuffer <: IO
    ptr::Int
end

@inline function Base.write(to::BlackHoleBuffer, x::UInt8)
    to.ptr += 1
    return sizeof(UInt8)
end

```

This is basically just a stripped down version of `IOBuffer`, so I expected this to be fast to write to, especially for large arrays, but as you can see, writing to `BlackHoleBuffer` is about 3 times _slower_ than writing to an `IOBuffer`. Any hints as to why this might be the case?

```julia
julia> using BenchmarkTools

julia> A = rand(1000000);

julia> @btime begin
       buf = IOBuffer()
       write(buf, A)
       end
  3.286 ms (5 allocations: 7.63 MiB)
8000000

julia> @btime begin
       buf = BlackHoleBuffer(0)
       write(buf, A)
       end
  10.226 ms (2 allocations: 32 bytes)
8000000

```

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [February 28, 2023, 6:35am UTC](https://discourse.julialang.org/t/why-is-my-own-simplified-io-type-slower-than-base-iobuffer/95308/2 "2023-02-28T06:35:09Z")

</div>

It’s likely due to having to fall back to “writing” singular bytes, instead of being able to write out the whole object. I’d type this as

```julia
write(io::BlackholeBuffer, _::T) where T
     size = sizeof(T) 
     io.ptr += size
     return size
end

```

This will run into some ambiguities, due to writing to IO having the fallbacks mentioned above. You’ll want to define these (e.g. for `Base.BitInteger` etc) for your type explicitly (see [here](https://github.com/JuliaLang/julia/issues/47385)).
