From d669f460d6a9091fb6b745538033845b2a3d2d46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 17 Nov 2026 11:04:31 +0100 Subject: [PATCH 1/2] Avoid binary concatenation when binaries are the head of the list --- lib/mint/http2.ex | 36 ++++++++++++++++++++++++----------- test/mint/http2/conn_test.exs | 7 ++++--- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/lib/mint/http2.ex b/lib/mint/http2.ex index 9ad856e0..5e72607f 100644 --- a/lib/mint/http2.ex +++ b/lib/mint/http2.ex @@ -1515,7 +1515,6 @@ defmodule Mint.HTTP2 do defp headers_to_encoded_frames(conn, stream_id, hbf, enabled_flags) do if IO.iodata_length(hbf) > conn.server_settings.max_frame_size do hbf - |> IO.iodata_to_binary() |> split_payload_in_chunks(conn.server_settings.max_frame_size) |> split_hbf_to_encoded_frames(stream_id, enabled_flags) else @@ -1571,9 +1570,7 @@ defmodule Mint.HTTP2 do # on the max frame size. data_size > conn.server_settings.max_frame_size -> {chunks, last_chunk} = - data - |> IO.iodata_to_binary() - |> split_payload_in_chunks(conn.server_settings.max_frame_size) + split_payload_in_chunks(data, conn.server_settings.max_frame_size) {encoded_chunks, conn} = Enum.map_reduce(chunks, conn, fn chunk, acc -> @@ -1606,18 +1603,35 @@ defmodule Mint.HTTP2 do {conn, Frame.encode(frame)} end - defp split_payload_in_chunks(binary, chunk_size), - do: split_payload_in_chunks(binary, chunk_size, []) + # Splits iodata into chunks of exactly chunk_size bytes plus a non-empty last + # chunk. While the head of the list is a binary, we split it into sub-binaries + # to avoid copying the payload. Anything else is converted to a binary. + defp split_payload_in_chunks(iodata, chunk_size), + do: split_payload_in_chunks(iodata, chunk_size, chunk_size, [], []) - defp split_payload_in_chunks(chunk, chunk_size, acc) when byte_size(chunk) <= chunk_size do - {Enum.reverse(acc), chunk} + defp split_payload_in_chunks([], _size, _left, chunk, chunks), + do: {Enum.reverse(chunks), Enum.reverse(chunk)} + + defp split_payload_in_chunks([binary | rest], size, left, chunk, chunks) + when byte_size(binary) <= left do + split_payload_in_chunks(rest, size, left - byte_size(binary), [binary | chunk], chunks) + end + + # The current chunk is full and there is more data, so we start a new one. + defp split_payload_in_chunks([binary | _] = iodata, size, 0, chunk, chunks) + when is_binary(binary) do + split_payload_in_chunks(iodata, size, size, [], [Enum.reverse(chunk) | chunks]) end - defp split_payload_in_chunks(binary, chunk_size, acc) do - {chunk, rest} = :erlang.split_binary(binary, chunk_size) - split_payload_in_chunks(rest, chunk_size, [chunk | acc]) + defp split_payload_in_chunks([binary | rest], size, left, chunk, chunks) + when is_binary(binary) do + {part, binary} = :erlang.split_binary(binary, left) + split_payload_in_chunks([binary | rest], size, 0, [part | chunk], chunks) end + defp split_payload_in_chunks(iodata, size, left, chunk, chunks), + do: split_payload_in_chunks([IO.iodata_to_binary(iodata)], size, left, chunk, chunks) + defp send_ping(conn, payload) do frame = Frame.ping(stream_id: 0, opaque_data: payload) conn = send!(conn, Frame.encode(frame)) diff --git a/test/mint/http2/conn_test.exs b/test/mint/http2/conn_test.exs index 0cc4ae4c..76a8cc29 100644 --- a/test/mint/http2/conn_test.exs +++ b/test/mint/http2/conn_test.exs @@ -3425,7 +3425,8 @@ defmodule Mint.HTTP2Test do %{conn: conn} do max_frame_size = HTTP2.get_server_setting(conn, :max_frame_size) - body = :binary.copy(<<0>>, max_frame_size + 1) + # The nested list is converted to a binary which is then split across frames. + body = [:binary.copy(<<0>>, max_frame_size - 1), [<<1>>, <<2>>]] {conn, _ref} = open_request(conn, body) assert_recv_frames [ @@ -3435,10 +3436,10 @@ defmodule Mint.HTTP2Test do ] assert flags1 == set_flags(:data, []) - assert data1 == :binary.copy(<<0>>, max_frame_size) + assert data1 == :binary.copy(<<0>>, max_frame_size - 1) <> <<1>> assert flags2 == set_flags(:data, [:end_stream]) - assert data2 == <<0>> + assert data2 == <<2>> assert HTTP2.open?(conn) end From 110d1b4cde356b05446efc8fb92f7b1977d75f34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 17 Nov 2026 11:29:08 +0100 Subject: [PATCH 2/2] Use iolist_to_iovec --- lib/mint/http2.ex | 4 ++-- test/mint/http2/conn_test.exs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/mint/http2.ex b/lib/mint/http2.ex index 5e72607f..d27ce53e 100644 --- a/lib/mint/http2.ex +++ b/lib/mint/http2.ex @@ -1605,7 +1605,7 @@ defmodule Mint.HTTP2 do # Splits iodata into chunks of exactly chunk_size bytes plus a non-empty last # chunk. While the head of the list is a binary, we split it into sub-binaries - # to avoid copying the payload. Anything else is converted to a binary. + # to avoid copying the payload. Anything else is converted to an iovec. defp split_payload_in_chunks(iodata, chunk_size), do: split_payload_in_chunks(iodata, chunk_size, chunk_size, [], []) @@ -1630,7 +1630,7 @@ defmodule Mint.HTTP2 do end defp split_payload_in_chunks(iodata, size, left, chunk, chunks), - do: split_payload_in_chunks([IO.iodata_to_binary(iodata)], size, left, chunk, chunks) + do: split_payload_in_chunks(:erlang.iolist_to_iovec(iodata), size, left, chunk, chunks) defp send_ping(conn, payload) do frame = Frame.ping(stream_id: 0, opaque_data: payload) diff --git a/test/mint/http2/conn_test.exs b/test/mint/http2/conn_test.exs index 76a8cc29..7a43955b 100644 --- a/test/mint/http2/conn_test.exs +++ b/test/mint/http2/conn_test.exs @@ -3425,7 +3425,7 @@ defmodule Mint.HTTP2Test do %{conn: conn} do max_frame_size = HTTP2.get_server_setting(conn, :max_frame_size) - # The nested list is converted to a binary which is then split across frames. + # The nested list is converted to an iovec which is then split across frames. body = [:binary.copy(<<0>>, max_frame_size - 1), [<<1>>, <<2>>]] {conn, _ref} = open_request(conn, body)