JuliaIO / JuliaIO/TranscodingStreams.jl

Poor performance when reading primitive types that aren't UInt8?

Open
#106 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
90
Forks
25
PR merge metrics
No merged PRs in 30d

Description

I am trying to read some data in a looping way, reading a mix of primitive types. Here's a simple MWE where I am just reading Int32s:

import TranscodingStreams

function writer()
    open(joinpath(homedir(), "test.dat"), write=true) do io
        write(io, rand(Int32, 1_000_000))
    end
end

function reader()
    open(TranscodingStreams.NoopStream, joinpath(homedir(), "test.dat")) do io
        for i in 1:1_000_000
            read(io, Int32)
            # read(io, UInt8)
        end
    end
end

writer()
reader()

julia> @time reader()
  0.025751 seconds (1.00 M allocations: 15.276 MiB)

This produces an allocation per read call, which becomes very slow in a tight loop! Reading 4 million UInt8s (see commented out line) has effectively zero allocations in the loop.

Is this expected behavior? Is my use-case uncommon? It does not seem to be specific to the NoopStream, since I first encountered it with the CodecZstd.ZsdtDecompressorStream.

Thanks.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the allocation difference in the reported reader() MWE using TranscodingStreams.NoopStream, comparing read(io, Int32) with read(io, UInt8). Trace the read entry points involved in primitive-type reads and confirm that the completed work removes the per-call allocation without regressing the existing UInt8 behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.