From 11200600c6645d7f220d1ae501667d08f1e6adac Mon Sep 17 00:00:00 2001 From: Jacob Quinn Date: Thu, 19 Nov 2026 11:03:15 -0700 Subject: [PATCH 1/2] Parse one-dimensional arrays straight into a typed vector The array parser copied each element through a growable byte buffer and boxed every value into a Vector{Any} before narrowing it to the element type. One-dimensional arrays without NULLs, escapes or nested arrays, the common case, now go straight into a Vector{T}: each element is one String and the same scalar parser as before. Any other literal still goes through the general parser, so results are unchanged. Co-Authored-By: Claude Opus 5.5 --- src/array_parsing.jl | 44 ++++++++++++++++++++++++++++++++++++++++++++ test/runtests.jl | 15 +++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/src/array_parsing.jl b/src/array_parsing.jl index eabde12..6b8fb5d 100644 --- a/src/array_parsing.jl +++ b/src/array_parsing.jl @@ -175,6 +175,48 @@ function parse_array_inner(code::Base.CodeUnits{UInt8, String}, pos::Ref{Int}, i return coerce_array(values, inner_type) end +# The common case, a one-dimensional array without NULLs or escapes, read +# straight into a `Vector{T}`. `parse_array_inner` copies each token through a +# growable buffer and boxes each value into a `Vector{Any}` first. Elements are +# split and parsed exactly as there; returns `nothing` at the first nested +# array, escape, NULL or value that is not a `T`, and the caller then parses +# the whole literal with `parse_array_inner`. +function parse_flat_array(str::String, open::Int, inner_type::Type{T}) where {T} + code = codeunits(str) + n = length(code) + dest = T[] + i = open + 1 + while i <= n + while i <= n && is_ws(code[i]) + i += 1 + end + i > n && break + c = code[i] + c == BRACE_CLOSE && break + c == BRACE_OPEN && return nothing + quoted = c == QUOTE + quoted && (i += 1) + j = i + while j <= n + b = code[j] + b == BACKSLASH && return nothing + (quoted ? b == QUOTE : (b == COMMA || b == BRACE_CLOSE)) && break + j += 1 + end + token = GC.@preserve str unsafe_string(pointer(str, i), j - i) + # a quoted element ends past its closing quote + i = quoted && j <= n ? j + 1 : j + value = parse_scalar(token, inner_type, quoted) + (value === missing || value isa AbstractVector || !(value isa T)) && return nothing + push!(dest, value) + while i <= n && is_ws(code[i]) + i += 1 + end + i <= n && code[i] == COMMA && (i += 1) + end + return dest +end + const EQUALS = UInt8('=') # PostgreSQL prefixes an array literal with explicit dimensions whenever a @@ -217,6 +259,8 @@ function parse_array(str::String, inner_type::Type{T}) where {T} end c = code[pos[]] if c == BRACE_OPEN + flat = parse_flat_array(str, pos[], inner_type) + flat === nothing || return flat return parse_array_inner(code, pos, inner_type) end value = parse_scalar(str, inner_type, true) diff --git a/test/runtests.jl b/test/runtests.jl index 2711a38..25c92fe 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -963,6 +963,21 @@ include("trim_compile_tests.jl") @test Postgres.API.parse_array_by_oid("{/var/log/x[1].txt,b}", 25, registry) == ["/var/log/x[1].txt", "b"] @test Postgres.API.ArrayParsing.parse_array("{1,2}", Int64) isa Vector{Int64} + # one-dimensional arrays without NULLs or escapes are read straight into + # a typed vector; any other literal gives what the general parser gives + let parse = Postgres.API.ArrayParsing.parse_array + @test parse("{ 1 , \"2\",3 }", Int32) == Int32[1, 2, 3] + @test parse("{12.00,-7}", Int64) == [12, -7] + @test parse("{}", Float64) == Float64[] + @test parse("{NaN,-Infinity,1.5}", Float64) isa Vector{Float64} + @test isequal(parse("{1,NULL}", Int32), Union{Missing, Int32}[1, missing]) + @test parse("{\"NULL\",NULL}", String) isa Vector{Union{Missing, String}} + @test parse("{\"a\\\"b\",c d,\"\"}", String) == ["a\"b", "c d", ""] + @test parse("{{1,2},{3,4}}", Int32) == [Int32[1, 2], Int32[3, 4]] + @test parse("{1,{2}}", Int32) == Any[Int32(1), Int32[2]] + @test parse("{2024-01-02,2024-02-29}", Date) == [Date(2024, 1, 2), Date(2024, 2, 29)] + @test_throws ArgumentError parse("{1,x}", Int32) + end rng = MersenneTwister(0x5097) for _ in 1:200 From d8a355ff9f8fa099db2605653cc0eeaf64cbf2d7 Mon Sep 17 00:00:00 2001 From: Jacob Quinn Date: Thu, 19 Nov 2026 15:40:54 -0700 Subject: [PATCH 2/2] Send NULL, escaped and nested array literals to the general parser up front The flat array reader parsed elements until it met a NULL, a backslash or a nested array, then threw that work away and the general parser started over. Literals with a NULL or an escape allocated more than on main and ran up to 1.2x slower. needs_general_parser now decides before any element is read, with a check for `{{` and memchr scans for a backslash and the bytes NULL. Those literals are parsed once and allocate exactly what they do on main. The flat path's own backslash and NULL checks are gone, since neither can occur once the scan passes. Co-Authored-By: Claude Opus 5.5 --- src/array_parsing.jl | 26 +++++++++++++++++++++----- test/runtests.jl | 14 ++++++++++++++ 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/array_parsing.jl b/src/array_parsing.jl index 6b8fb5d..b7bae05 100644 --- a/src/array_parsing.jl +++ b/src/array_parsing.jl @@ -175,14 +175,31 @@ function parse_array_inner(code::Base.CodeUnits{UInt8, String}, pos::Ref{Int}, i return coerce_array(values, inner_type) end +# Whether the literal is one `parse_flat_array` gives up on, told without +# reading any element: a nested array (the server writes `{{`), a backslash +# escape, or the bytes `NULL`. Memchr scans keep this cheap. A quoted "NULL" or +# a word containing it also counts; the general parser reads those the same way. +function needs_general_parser(code::Base.CodeUnits{UInt8, String}, open::Int) + open < length(code) && code[open + 1] == BRACE_OPEN && return true + findfirst(==(BACKSLASH), code) !== nothing && return true + i = findfirst(==(UInt8('N')), code) + while i !== nothing + i + 3 <= length(code) && code[i + 1] == UInt8('U') && code[i + 2] == UInt8('L') && code[i + 3] == UInt8('L') && return true + i = findnext(==(UInt8('N')), code, i + 1) + end + return false +end + # The common case, a one-dimensional array without NULLs or escapes, read # straight into a `Vector{T}`. `parse_array_inner` copies each token through a # growable buffer and boxes each value into a `Vector{Any}` first. Elements are -# split and parsed exactly as there; returns `nothing` at the first nested -# array, escape, NULL or value that is not a `T`, and the caller then parses -# the whole literal with `parse_array_inner`. +# split and parsed exactly as there. Returns `nothing` when +# `needs_general_parser` says so, so such a literal is parsed once, or at a +# later nested array or a value that is not a `T`; the caller then parses the +# whole literal with `parse_array_inner`. function parse_flat_array(str::String, open::Int, inner_type::Type{T}) where {T} code = codeunits(str) + needs_general_parser(code, open) && return nothing n = length(code) dest = T[] i = open + 1 @@ -199,7 +216,6 @@ function parse_flat_array(str::String, open::Int, inner_type::Type{T}) where {T} j = i while j <= n b = code[j] - b == BACKSLASH && return nothing (quoted ? b == QUOTE : (b == COMMA || b == BRACE_CLOSE)) && break j += 1 end @@ -207,7 +223,7 @@ function parse_flat_array(str::String, open::Int, inner_type::Type{T}) where {T} # a quoted element ends past its closing quote i = quoted && j <= n ? j + 1 : j value = parse_scalar(token, inner_type, quoted) - (value === missing || value isa AbstractVector || !(value isa T)) && return nothing + value isa T || return nothing push!(dest, value) while i <= n && is_ws(code[i]) i += 1 diff --git a/test/runtests.jl b/test/runtests.jl index f3aa71d..be6d184 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -492,6 +492,13 @@ function random_array_string(rng::AbstractRNG) return String(rand(rng, alphabet, rand(rng, 0:12))) end +# allocations of a call after a warm-up call; measured inside a function so +# `@allocations` does not force-compile the enclosing testset +function warm_allocations(f, s::String, ::Type{T}) where {T} + f(s, T) + return @allocations f(s, T) +end + include("timestamps.jl") include("decimals.jl") include("result_decoding.jl") @@ -977,6 +984,13 @@ include("trim_compile_tests.jl") @test parse("{1,{2}}", Int32) == Any[Int32(1), Int32[2]] @test parse("{2024-01-02,2024-02-29}", Date) == [Date(2024, 1, 2), Date(2024, 2, 29)] @test_throws ArgumentError parse("{1,x}", Int32) + @test parse("{aNULLb,\"NULL\"}", String)::Vector{String} == ["aNULLb", "NULL"] + # a literal with a NULL, an escape or a nested array costs no more than + # the general parser alone: the fast path turns it away before reading + general(s, ::Type{T}) where {T} = Postgres.API.ArrayParsing.parse_array_inner(codeunits(s), Ref(1), T) + for (s, T) in (("{1,2,3,4,NULL}", Int32), ("{abc,\"x\\\\y\"}", String), ("{{1,2},{3,4}}", Int32)) + @test warm_allocations(parse, s, T) <= warm_allocations(general, s, T) + end end @test Postgres.API.parse_value(1005, "{1,-2}", registry) == Int16[1, -2] @test Postgres.API.parse_value(1021, "{1.5,NaN}", registry) isa Vector{Float32}