Skip to content

Commit

Permalink
Use SharedRow in Jsonb::from_slice (#28350)
Browse files Browse the repository at this point in the history
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.
  <!-- Reference the design in the description. -->
- [ ] 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](MaterializeInc/cloud#5021)).
<!-- Ask in #team-cloud on Slack if you need help preparing the cloud
PR. -->
- [ ] 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):
- <!-- Add release notes here or explicitly state that there are no
user-facing behavior changes. -->

Signed-off-by: Moritz Hoffmann <[email protected]>
  • Loading branch information
antiguru authored Jul 18, 2024
1 parent 25f37b4 commit b3d67aa
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/repr/src/adt/jsonb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`].
///
Expand Down Expand Up @@ -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<Jsonb, anyhow::Error> {
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`.
Expand Down

0 comments on commit b3d67aa

Please sign in to comment.