From e86c59201d7f805787d62f12beb5dd2a6c83f3c5 Mon Sep 17 00:00:00 2001 From: Jacob Quinn Date: Thu, 19 Nov 2026 11:02:12 -0700 Subject: [PATCH 1/3] Read protocol messages without a heap allocation per message Every message header read its Int32 length through Base's generic `read(io, Int32)`, which allocates a `Ref`, and every DataRow body was read into its own `Vector{UInt8}`. Fixed-width reads now load straight from the BufferedConn buffer, and the rows of one result are copied into shared 64 KiB chunks. A chunk is never overwritten, so rows a consumer keeps (a `Vector{Any}` target keeps the DataRow) stay valid. Co-Authored-By: Claude Opus 5.5 --- src/api/API.jl | 33 +++++++++++++++++++++++++++------ src/api/buffered.jl | 14 ++++++++++++++ test/execute_fake_server.jl | 10 ++++++++++ test/runtests.jl | 15 +++++++++++++++ 4 files changed, 66 insertions(+), 6 deletions(-) diff --git a/src/api/API.jl b/src/api/API.jl index 4cbb5ca..07cb095 100644 --- a/src/api/API.jl +++ b/src/api/API.jl @@ -1037,28 +1037,35 @@ end # buffer (instead of incrementally from the socket) means a failure while # converting values — a bad cast, a user lift throwing — aborts at a message # boundary, so the caller can drain the rest of the result and keep the -# connection usable. +# connection usable. The body is `buf[start:start+len-1]`; the rows of one +# result share `buf`. struct DataRow buf::Vector{UInt8} + start::Int + len::Int names::Vector{Symbol} typeIds::Vector{Int} type_registry::Dict{Int, TypeInfo} end +DataRow(buf::Vector{UInt8}, names::Vector{Symbol}, typeIds::Vector{Int}, type_registry::Dict{Int, TypeInfo}) = + DataRow(buf, 1, length(buf), names, typeIds, type_registry) + # (style types + behavior interface live in types.jl, included before this point) function StructUtils.applyeach(::AbstractPostgresStyle, f, dr::DataRow) buf = dr.buf - nbuf = length(buf) + # index of the body's last byte + nbuf = dr.start + dr.len - 1 GC.@preserve buf begin - nbuf >= 2 || throw(Error("truncated DataRow message from server")) - ncols = Int(ntoh(unsafe_load(Ptr{Int16}(pointer(buf))))) + dr.len >= 2 || throw(Error("truncated DataRow message from server")) + ncols = Int(ntoh(unsafe_load(Ptr{Int16}(pointer(buf, dr.start))))) # the protocol mandates one value per described column; anything else # (including a negative count, which is signed on the wire) would leave # the caller's row partly unfilled — an UndefRefError downstream (ncols == length(dr.names) && ncols == length(dr.typeIds)) || throw(Error("DataRow column count does not match the row description")) - pos = 3 + pos = dr.start + 2 for i = 1:ncols # column lengths come off the wire: validate each against the # message actually received before reading the value @@ -1134,8 +1141,15 @@ function rows_affected_from_command_tag(tag::String) end end +# Row bodies are copied into shared chunks rather than one allocation per row. +# A chunk is never overwritten, so a row a consumer keeps (a `Vector{Any}` +# target keeps the DataRow itself) stays valid. +const ROW_CHUNK_SIZE = 1 << 16 + function StructUtils.applyeach(::AbstractPostgresStyle, f, e::Exec) nrows = 0 + chunk = UInt8[] + used = 0 server_error = nothing consumer_error = nothing copy_in_statement = false @@ -1152,7 +1166,14 @@ function StructUtils.applyeach(::AbstractPostgresStyle, f, e::Exec) elseif mt == UInt8('D') nrows += 1 if consumer_error === nothing - row = DataRow(read(e.socket, len), e.names, e.typeIds, e.type_registry) + if used + len > length(chunk) + # zeroed, so showing a kept DataRow never prints uninitialized memory + chunk = zeros(UInt8, max(ROW_CHUNK_SIZE, len)) + used = 0 + end + GC.@preserve chunk unsafe_read(e.socket, pointer(chunk, used + 1), len) + row = DataRow(chunk, used + 1, len, e.names, e.typeIds, e.type_registry) + used += len try f(nrows, row) catch err diff --git a/src/api/buffered.jl b/src/api/buffered.jl index fd07d6f..270c9c9 100644 --- a/src/api/buffered.jl +++ b/src/api/buffered.jl @@ -39,6 +39,20 @@ function Base.read(io::BufferedConn, ::Type{UInt8}) return b end +# Message headers carry an Int32 length, so this runs once per protocol +# message. Base's `read(io, T)` reads through a heap-allocated `Ref`. +Base.read(io::BufferedConn, T::Union{Type{Int16}, Type{UInt16}, Type{Int32}, Type{UInt32}}) = read_fixed(io, T) + +function read_fixed(io::BufferedConn, ::Type{T}) where {T} + while bytesavailable(io) < sizeof(T) + fill_buffer!(io) || throw(EOFError()) + end + buf = io.buffer + x = GC.@preserve buf unsafe_load(Ptr{T}(pointer(buf, io.pos))) + io.pos += sizeof(T) + return x +end + function Base.unsafe_read(io::BufferedConn, p::Ptr{UInt8}, n::UInt) remaining = Int(n) while remaining > 0 diff --git a/test/execute_fake_server.jl b/test/execute_fake_server.jl index 1c4b28b..db25ad9 100644 --- a/test/execute_fake_server.jl +++ b/test/execute_fake_server.jl @@ -39,6 +39,16 @@ function test_execute_fake_server() close(io) end end + @testset "a message header split across transport reads" begin + msg = pgmsg('d', UInt8[1, 2, 3]) + with_fake_server((sock, n) -> foreach(b -> (send(sock, [b]); sleep(0.02)), msg)) do port, accepted + io = Postgres.API.BufferedConn(Postgres.API.connectsocket("127.0.0.1", port, Int64(0))) + @test Postgres.API.readheader(io) == (UInt8('d'), 3) + @test Postgres.API.readbody(io, 3) == UInt8[1, 2, 3] + @test_throws EOFError read(io, Int32) + close(io) + end + end # the replies the server gets out before the connection is lost partial_replies = ( "before ParseComplete" => UInt8[], diff --git a/test/runtests.jl b/test/runtests.jl index 2711a38..e908c66 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1352,6 +1352,21 @@ include("trim_compile_tests.jl") update_res = DBInterface.execute(conn, "UPDATE command_tag_test SET id = id + 1") @test Postgres.command_tag(update_res) == "UPDATE 2" @test Postgres.rows_affected(update_res) == 2 + # rows of one result share read buffers; rows of every size, + # one larger than a buffer, and rows a consumer keeps (a + # Vector{Any} target keeps each DataRow) must read back intact + sizes_sql = "SELECT g AS n, repeat(chr(65 + g % 26), CASE WHEN g = 1500 THEN 200000 ELSE g * 37 % 5000 END) AS s FROM generate_series(1, 3000) g" + text_of(n) = repeat(Char(65 + n % 26), n == 1500 ? 200000 : n * 37 % 5000) + typed_rows = DBInterface.execute(conn, sizes_sql, (), Vector{@NamedTuple{n::Int32, s::String}}) + @test all(r -> r.s == text_of(r.n), typed_rows) + kept = DBInterface.execute(conn, sizes_sql, (), Vector{Any}) + DBInterface.execute(conn, sizes_sql) + kept_values = map(kept) do row + values = Any[] + StructUtils.applyeach(Postgres.PostgresStyle(), (_, v) -> push!(values, v), row) + values + end + @test all(v -> v[2] == text_of(v[1]), kept_values) end @testset "Type Parsing" begin id, expected = setup_types(conn) From 5df615cdcea3a63b664457fb33131f2cb9e6a4d8 Mon Sep 17 00:00:00 2001 From: Jacob Quinn Date: Thu, 19 Nov 2026 12:32:21 -0700 Subject: [PATCH 2/3] Keep one buffer per DataRow Copying the rows of a result into shared chunks cut allocations, but in untyped reads that keep a large text result Julia 1.12's collector then ran 50-100% more collections, several of them full, and those reads got slower. Each DataRow again gets its own buffer; only the allocation-free fixed-width reads remain. Co-Authored-By: Claude Opus 5.5 --- src/api/API.jl | 33 ++++++--------------------------- test/runtests.jl | 15 --------------- 2 files changed, 6 insertions(+), 42 deletions(-) diff --git a/src/api/API.jl b/src/api/API.jl index 07cb095..4cbb5ca 100644 --- a/src/api/API.jl +++ b/src/api/API.jl @@ -1037,35 +1037,28 @@ end # buffer (instead of incrementally from the socket) means a failure while # converting values — a bad cast, a user lift throwing — aborts at a message # boundary, so the caller can drain the rest of the result and keep the -# connection usable. The body is `buf[start:start+len-1]`; the rows of one -# result share `buf`. +# connection usable. struct DataRow buf::Vector{UInt8} - start::Int - len::Int names::Vector{Symbol} typeIds::Vector{Int} type_registry::Dict{Int, TypeInfo} end -DataRow(buf::Vector{UInt8}, names::Vector{Symbol}, typeIds::Vector{Int}, type_registry::Dict{Int, TypeInfo}) = - DataRow(buf, 1, length(buf), names, typeIds, type_registry) - # (style types + behavior interface live in types.jl, included before this point) function StructUtils.applyeach(::AbstractPostgresStyle, f, dr::DataRow) buf = dr.buf - # index of the body's last byte - nbuf = dr.start + dr.len - 1 + nbuf = length(buf) GC.@preserve buf begin - dr.len >= 2 || throw(Error("truncated DataRow message from server")) - ncols = Int(ntoh(unsafe_load(Ptr{Int16}(pointer(buf, dr.start))))) + nbuf >= 2 || throw(Error("truncated DataRow message from server")) + ncols = Int(ntoh(unsafe_load(Ptr{Int16}(pointer(buf))))) # the protocol mandates one value per described column; anything else # (including a negative count, which is signed on the wire) would leave # the caller's row partly unfilled — an UndefRefError downstream (ncols == length(dr.names) && ncols == length(dr.typeIds)) || throw(Error("DataRow column count does not match the row description")) - pos = dr.start + 2 + pos = 3 for i = 1:ncols # column lengths come off the wire: validate each against the # message actually received before reading the value @@ -1141,15 +1134,8 @@ function rows_affected_from_command_tag(tag::String) end end -# Row bodies are copied into shared chunks rather than one allocation per row. -# A chunk is never overwritten, so a row a consumer keeps (a `Vector{Any}` -# target keeps the DataRow itself) stays valid. -const ROW_CHUNK_SIZE = 1 << 16 - function StructUtils.applyeach(::AbstractPostgresStyle, f, e::Exec) nrows = 0 - chunk = UInt8[] - used = 0 server_error = nothing consumer_error = nothing copy_in_statement = false @@ -1166,14 +1152,7 @@ function StructUtils.applyeach(::AbstractPostgresStyle, f, e::Exec) elseif mt == UInt8('D') nrows += 1 if consumer_error === nothing - if used + len > length(chunk) - # zeroed, so showing a kept DataRow never prints uninitialized memory - chunk = zeros(UInt8, max(ROW_CHUNK_SIZE, len)) - used = 0 - end - GC.@preserve chunk unsafe_read(e.socket, pointer(chunk, used + 1), len) - row = DataRow(chunk, used + 1, len, e.names, e.typeIds, e.type_registry) - used += len + row = DataRow(read(e.socket, len), e.names, e.typeIds, e.type_registry) try f(nrows, row) catch err diff --git a/test/runtests.jl b/test/runtests.jl index e908c66..2711a38 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1352,21 +1352,6 @@ include("trim_compile_tests.jl") update_res = DBInterface.execute(conn, "UPDATE command_tag_test SET id = id + 1") @test Postgres.command_tag(update_res) == "UPDATE 2" @test Postgres.rows_affected(update_res) == 2 - # rows of one result share read buffers; rows of every size, - # one larger than a buffer, and rows a consumer keeps (a - # Vector{Any} target keeps each DataRow) must read back intact - sizes_sql = "SELECT g AS n, repeat(chr(65 + g % 26), CASE WHEN g = 1500 THEN 200000 ELSE g * 37 % 5000 END) AS s FROM generate_series(1, 3000) g" - text_of(n) = repeat(Char(65 + n % 26), n == 1500 ? 200000 : n * 37 % 5000) - typed_rows = DBInterface.execute(conn, sizes_sql, (), Vector{@NamedTuple{n::Int32, s::String}}) - @test all(r -> r.s == text_of(r.n), typed_rows) - kept = DBInterface.execute(conn, sizes_sql, (), Vector{Any}) - DBInterface.execute(conn, sizes_sql) - kept_values = map(kept) do row - values = Any[] - StructUtils.applyeach(Postgres.PostgresStyle(), (_, v) -> push!(values, v), row) - values - end - @test all(v -> v[2] == text_of(v[1]), kept_values) end @testset "Type Parsing" begin id, expected = setup_types(conn) From 7b9d291004392a6f1e84f944a088ddf0b2d50c72 Mon Sep 17 00:00:00 2001 From: Jacob Quinn Date: Thu, 19 Nov 2026 12:47:56 -0700 Subject: [PATCH 3/3] Remove BufferedConn's unsafe_read Its only callers were the fixed-width reads, which now load from the buffer directly; message bodies go through readbytes!. Co-Authored-By: Claude Opus 5.5 --- src/api/buffered.jl | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/src/api/buffered.jl b/src/api/buffered.jl index 270c9c9..34e0f40 100644 --- a/src/api/buffered.jl +++ b/src/api/buffered.jl @@ -53,26 +53,6 @@ function read_fixed(io::BufferedConn, ::Type{T}) where {T} return x end -function Base.unsafe_read(io::BufferedConn, p::Ptr{UInt8}, n::UInt) - remaining = Int(n) - while remaining > 0 - if bytesavailable(io) == 0 - # a remainder larger than the scratch buffer goes straight to `p` - if remaining >= length(io.scratch) && !io.ended - unsafe_read(io.transport, p, UInt(remaining)) - return nothing - end - fill_buffer!(io) || throw(EOFError()) - end - k = min(remaining, bytesavailable(io)) - GC.@preserve io unsafe_copyto!(p, pointer(io.buffer, io.pos), k) - io.pos += k - p += k - remaining -= k - end - return nothing -end - function Base.readbytes!(io::BufferedConn, buf::AbstractVector{UInt8}, nb::Integer=length(buf); all::Bool=true) n = Int(nb) n >= 0 || throw(ArgumentError("nb must be nonnegative"))