Skip to content

JSON3.Array of JSON3.Array is really slow with some read operations #117

@evanfields

Description

@evanfields

Here's a small reproducing example, though I won't go so far as to say "minimal." I'm unsure whether or not this is an expected consequence of JSON3.Array's structure.

using JSON, JSON3
using BenchmarkTools

fake_data(n) = rand(n, n)
str = JSON.json(fake_data(1000))

json_listoflists = JSON.parse(str)
json3_listoflists = JSON3.read(str)

println("JSON style parsing: $(typeof(json_listoflists))")
println(@belapsed convert.(Vector{Float64}, $(json_listoflists)))
println("JSON3 parsing: $(typeof(json3_listoflists))")
println(@belapsed convert.(Vector{Float64}, $(json3_listoflists)))

output:

julia> include("timing.jl")
JSON style parsing: Array{Any,1}
0.020505509
JSON3 parsing: JSON3.Array{JSON3.Array,Base.CodeUnits{UInt8,String},Array{UInt64,1}}
3.517245224

So this broadcasted read operation over the JSON3.Array from JSON3.read is ~175x slower than the Array{Any, 1} from JSON.parse. Somehow the dimensionality seems to matter; with 1-dimensional data and no broadcast, the strongly typed JSON3.Array is much faster than a Vector{Any}:

using JSON, JSON3
using BenchmarkTools

fake_data(n) = rand(n)
str = JSON.json(fake_data(100_000))
json_list = JSON.parse(str)
json3_list = JSON3.read(str)

println("JSON style parsing: $(typeof(json_listoflists))")
println(@belapsed convert(Vector{Float64}, $(json_list)))
println("JSON3 parsing: $(typeof(json3_listoflists))")
println(@belapsed convert(Vector{Float64}, $(json3_list)))

output

julia> include("timing2.jl")
JSON style parsing: Array{Any,1}
0.002117856
JSON3 parsing: JSON3.Array{JSON3.Array,Base.CodeUnits{UInt8,String},Array{UInt64,1}}
0.000398982

Edit: the first version if this issue blamed the broadcast of convert., but seems like I can reproduce the slowness with an array of arrays and my own broadcast-free convert:

function myconvert(arrs)
    outputs = Vector{Float64}[]
    n = length(arrs)
    for i in 1:n
        push!(outputs, convert(Vector{Float64}, arrs[i]))
    end
    return outputs
end

println("JSON style parsing: $(typeof(json_listoflists))")
println(@belapsed myconvert(json_listoflists))
println("JSON3 parsing: $(typeof(json3_listoflists))")
println(@belapsed myconvert(json3_listoflists))

julia> include("timing3.jl")
JSON style parsing: Array{Any,1}
0.01995157
JSON3 parsing: JSON3.Array{JSON3.Array,Base.CodeUnits{UInt8,String},Array{UInt64,1}}
3.496515541

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions