Skip to content

Commit f2c1bb7

Browse files
authored
New internal bounded string append APIs (#1816)
To efficiently support JSON serialization with bounded length, this is a new internal string API that can express bounded UTF-8-preserving append operations directly. This is a prerequisite for work on CDRIVER-3775 (structured logs) and CDRIVER-4814 (performance of large document serialization). Now a single buffer can be shared by all nesting levels of a complex document. Previously, all nesting levels of a BSON document would allocate their own string append buffer, and truncation happened as a separate step on the way out of these nested levels. Several other temporary string buffers were eliminated, including the temporary buffer that was needed for every printf. Now mcommon_string_append_printf writes directly to the destination, growing it as needed. * test the utf8-oblivious behavior of bson_string_truncate * document the utf8-oblivious behavior of bson_string_truncate * new internal mcommon_string, mcommon_string_append, mcommon_json_append APIs * some tests for UTF-8 preserving string truncation * unit test for printf truncation between utf-8 code points * fix for missing TXT string length validation
1 parent a12a612 commit f2c1bb7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+3233
-2096
lines changed

src/common/src/common-bits-private.h

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2009-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "common-prelude.h"
18+
19+
#ifndef MONGO_C_DRIVER_COMMON_BITS_PRIVATE_H
20+
#define MONGO_C_DRIVER_COMMON_BITS_PRIVATE_H
21+
22+
#include <bson/bson.h>
23+
24+
25+
// Round up to the next power of two uint32_t value. Saturates on overflow.
26+
static BSON_INLINE uint32_t
27+
mcommon_next_power_of_two_u32 (uint32_t v)
28+
{
29+
if (v == 0) {
30+
return 1;
31+
}
32+
33+
// https://graphics.stanford.edu/%7Eseander/bithacks.html#RoundUpPowerOf2
34+
v--;
35+
v |= v >> 1;
36+
v |= v >> 2;
37+
v |= v >> 4;
38+
v |= v >> 8;
39+
v |= v >> 16;
40+
v++;
41+
42+
if (v == 0) {
43+
return UINT32_MAX;
44+
} else {
45+
return v;
46+
}
47+
}
48+
49+
50+
#endif /* MONGO_C_DRIVER_COMMON_BITS_PRIVATE_H */

0 commit comments

Comments
 (0)