proxy/http: avoid spurious vector reallocations

This tweaks the rows-to-JSON rendering logic in order to avoid
allocating 0-sized temporary vectors and later growing them
to insert elements.
As the exact size is known in advance, both vectors can be built
with an exact capacity upfront. This will avoid further vector
growing/reallocation in the rendering hotpath.

Signed-off-by: Luca BRUNO <lucab@lucabruno.net>
This commit is contained in:
Luca BRUNO
2024-07-09 10:43:42 +02:00
committed by Conrad Ludgate
parent 8b15864f59
commit c196cf6ac1

View File

@@ -838,8 +838,9 @@ async fn query_to_json<T: GenericClient>(
"finished reading rows"
);
let mut fields = vec![];
let mut columns = vec![];
let columns_len = row_stream.columns().len();
let mut fields = Vec::with_capacity(columns_len);
let mut columns = Vec::with_capacity(columns_len);
for c in row_stream.columns() {
fields.push(json!({