From b3d67aa1c794aa556afa0a6d112c1772ece058c7 Mon Sep 17 00:00:00 2001 From: Moritz Hoffmann Date: Thu, 18 Jul 2024 12:35:11 -0400 Subject: [PATCH] Use SharedRow in Jsonb::from_slice (#28350) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This uses the shared row to decode jsonb data instead of allocating a new row. The effect is that we do not need to reallocate a row as we're decoding. We don't know the size of the decoded row beforehand, so using the shared row in the limit avoids reallocating. ### Checklist - [ ] This PR has adequate test coverage / QA involvement has been duly considered. ([trigger-ci for additional test/nightly runs](https://trigger-ci.dev.materialize.com/)) - [ ] This PR has an associated up-to-date [design doc](https://github.com/MaterializeInc/materialize/blob/main/doc/developer/design/README.md), is a design doc ([template](https://github.com/MaterializeInc/materialize/blob/main/doc/developer/design/00000000_template.md)), or is sufficiently small to not require a design. - [ ] If this PR evolves [an existing `$T ⇔ Proto$T` mapping](https://github.com/MaterializeInc/materialize/blob/main/doc/developer/command-and-response-binary-encoding.md) (possibly in a backwards-incompatible way), then it is tagged with a `T-proto` label. - [ ] If this PR will require changes to cloud orchestration or tests, there is a companion cloud PR to account for those changes that is tagged with the release-blocker label ([example](https://github.com/MaterializeInc/cloud/pull/5021)). - [ ] This PR includes the following [user-facing behavior changes](https://github.com/MaterializeInc/materialize/blob/main/doc/developer/guide-changes.md#what-changes-require-a-release-note): - Signed-off-by: Moritz Hoffmann --- src/repr/src/adt/jsonb.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/repr/src/adt/jsonb.rs b/src/repr/src/adt/jsonb.rs index 3299e14d8b7e0..405bf593091dd 100644 --- a/src/repr/src/adt/jsonb.rs +++ b/src/repr/src/adt/jsonb.rs @@ -85,7 +85,7 @@ use serde::ser::{Serialize, SerializeMap, SerializeSeq, SerializeStruct, Seriali use crate::adt::jsonb::vec_stack::VecStack; use crate::adt::numeric::Numeric; -use crate::{strconv, Datum, Row, RowPacker}; +use crate::{strconv, Datum, Row, RowPacker, SharedRow}; /// An owned JSON value backed by a [`Row`]. /// @@ -115,9 +115,13 @@ impl Jsonb { /// Errors if the slice is not valid JSON or if any of the contained /// integers cannot be represented exactly as an [`f64`]. pub fn from_slice(buf: &[u8]) -> Result { - let mut row = Row::default(); - JsonbPacker::new(&mut row.packer()).pack_slice(buf)?; - Ok(Jsonb { row }) + let binding = SharedRow::get(); + let mut row_builder = binding.borrow_mut(); + let mut packer = row_builder.packer(); + JsonbPacker::new(&mut packer).pack_slice(buf)?; + Ok(Jsonb { + row: row_builder.clone(), + }) } /// Constructs a [`JsonbRef`] that references the JSON in this `Jsonb`.