Skip to content

Commit ca607f5

Browse files
authored
format_char_str: Avoid costly decoding if possible (#32018)
The `format_char_str` function formats a string as a fixed-length string, potentially cutting off overhanging white space. It needs to decode the string to find the position of the `length` character, which is a slow operation. This change introduces an optimization where if a string certainly isn't longer than the length in characters, we bail out early. The reasoning for this is simple: a string of length $n$ can have at most $n$ characters, so the length serves as an upper bound for the number of characters. Related: MaterializeInc/database-issues#9125 ### 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. --> - [ ] If this PR includes major [user-facing behavior changes](https://github.com/MaterializeInc/materialize/blob/main/doc/developer/guide-changes.md#what-changes-require-a-release-note), I have pinged the relevant PM to schedule a changelog post. Signed-off-by: Moritz Hoffmann <[email protected]>
1 parent 67a02b2 commit ca607f5

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

src/repr/src/adt/char.rs

+6
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,12 @@ fn format_char_str(
141141
// does.
142142
Some(l) => {
143143
let l = usize::cast_from(l.into_u32());
144+
// The number of chars in a string is always less or equal to the length of the string.
145+
// Hence, if the string is shorter than the length, we do not have to check for
146+
// the maximum length.
147+
if s.len() < l {
148+
return Ok(white_space.process_str(s, Some(l)));
149+
}
144150
match s.char_indices().nth(l) {
145151
None => white_space.process_str(s, Some(l)),
146152
Some((idx, _)) => {

0 commit comments

Comments
 (0)