Skip to content

Commit 2a59f11

Browse files
committed
Revert "fix: allocation handling for string in deserialize"
This reverts commit 4894e5e.
1 parent 79cb75c commit 2a59f11

5 files changed

Lines changed: 62 additions & 124 deletions

tuple/include/array_of_strings_sketch.hpp

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,11 @@
2828

2929
namespace datasketches {
3030

31-
template<typename Allocator>
32-
struct array_of_strings_types {
33-
using string_allocator = typename std::allocator_traits<Allocator>::template rebind_alloc<char>;
34-
using string_type = std::basic_string<char, std::char_traits<char>, string_allocator>;
35-
using array_allocator = typename std::allocator_traits<Allocator>::template rebind_alloc<string_type>;
36-
using array_of_strings = array<string_type, array_allocator>;
37-
};
38-
3931
// default update policy for an array of strings
40-
template<typename Allocator = std::allocator<char>>
32+
template<typename Allocator = std::allocator<std::string>>
4133
class default_array_of_strings_update_policy {
4234
public:
43-
using string_allocator = typename array_of_strings_types<Allocator>::string_allocator;
44-
using string_type = typename array_of_strings_types<Allocator>::string_type;
45-
using array_allocator = typename array_of_strings_types<Allocator>::array_allocator;
46-
using array_of_strings = typename array_of_strings_types<Allocator>::array_of_strings;
35+
using array_of_strings = array<std::string, Allocator>;
4736

4837
explicit default_array_of_strings_update_policy(const Allocator& allocator = Allocator());
4938

@@ -59,12 +48,9 @@ class default_array_of_strings_update_policy {
5948

6049
// serializer/deserializer for an array of strings
6150
// Requirements: all strings must be valid UTF-8 and array size must be <= 127.
62-
template<typename Allocator = std::allocator<char>>
51+
template<typename Allocator = std::allocator<std::string>>
6352
struct default_array_of_strings_serde {
64-
using string_allocator = typename array_of_strings_types<Allocator>::string_allocator;
65-
using string_type = typename array_of_strings_types<Allocator>::string_type;
66-
using array_allocator = typename array_of_strings_types<Allocator>::array_allocator;
67-
using array_of_strings = typename array_of_strings_types<Allocator>::array_of_strings;
53+
using array_of_strings = array<std::string, Allocator>;
6854
using summary_allocator = typename std::allocator_traits<Allocator>::template rebind_alloc<array_of_strings>;
6955

7056
explicit default_array_of_strings_serde(const Allocator& allocator = Allocator());
@@ -80,29 +66,27 @@ struct default_array_of_strings_serde {
8066
summary_allocator summary_allocator_;
8167
static void check_num_nodes(uint8_t num_nodes);
8268
static uint32_t compute_total_bytes(const array_of_strings& item);
83-
static void check_utf8(const string_type& value);
69+
static void check_utf8(const std::string& value);
8470
};
8571

8672
/**
8773
* Hashes an array of strings using ArrayOfStrings-compatible hashing.
8874
*/
89-
template<typename Allocator = std::allocator<char>>
90-
uint64_t hash_array_of_strings_key(const typename array_of_strings_types<Allocator>::array_of_strings& key);
75+
template<typename Allocator = std::allocator<std::string>>
76+
uint64_t hash_array_of_strings_key(const array<std::string, Allocator>& key);
9177

9278
/**
9379
* Extended class of compact_tuple_sketch for array of strings
9480
* Requirements: all strings must be valid UTF-8 and array size must be <= 127.
9581
*/
96-
template<typename Allocator = std::allocator<char>>
82+
template<typename Allocator = std::allocator<std::string>>
9783
class compact_array_of_strings_tuple_sketch:
9884
public compact_tuple_sketch<
99-
typename array_of_strings_types<Allocator>::array_of_strings,
100-
typename std::allocator_traits<Allocator>::template rebind_alloc<
101-
typename array_of_strings_types<Allocator>::array_of_strings
102-
>
85+
array<std::string, Allocator>,
86+
typename std::allocator_traits<Allocator>::template rebind_alloc<array<std::string, Allocator>>
10387
> {
10488
public:
105-
using array_of_strings = typename array_of_strings_types<Allocator>::array_of_strings;
89+
using array_of_strings = array<std::string, Allocator>;
10690
using summary_allocator = typename std::allocator_traits<Allocator>::template rebind_alloc<array_of_strings>;
10791
using Base = compact_tuple_sketch<array_of_strings, summary_allocator>;
10892
using vector_bytes = typename Base::vector_bytes;
@@ -149,15 +133,13 @@ class compact_array_of_strings_tuple_sketch:
149133
/**
150134
* Convenience alias for update_tuple_sketch for array of strings
151135
*/
152-
template<typename Allocator = std::allocator<char>,
136+
template<typename Allocator = std::allocator<std::string>,
153137
typename Policy = default_array_of_strings_update_policy<Allocator>>
154138
using update_array_of_strings_tuple_sketch = update_tuple_sketch<
155-
typename array_of_strings_types<Allocator>::array_of_strings,
156-
typename array_of_strings_types<Allocator>::array_of_strings,
139+
array<std::string, Allocator>,
140+
array<std::string, Allocator>,
157141
Policy,
158-
typename std::allocator_traits<Allocator>::template rebind_alloc<
159-
typename array_of_strings_types<Allocator>::array_of_strings
160-
>
142+
typename std::allocator_traits<Allocator>::template rebind_alloc<array<std::string, Allocator>>
161143
>;
162144

163145
/**
@@ -166,7 +148,7 @@ using update_array_of_strings_tuple_sketch = update_tuple_sketch<
166148
* @param ordered optional flag to specify if an ordered sketch should be produced
167149
* @return compact array of strings sketch
168150
*/
169-
template<typename Allocator = std::allocator<char>, typename Policy = default_array_of_strings_update_policy<Allocator>>
151+
template<typename Allocator = std::allocator<std::string>, typename Policy = default_array_of_strings_update_policy<Allocator>>
170152
compact_array_of_strings_tuple_sketch<Allocator> compact_array_of_strings_sketch(
171153
const update_array_of_strings_tuple_sketch<Allocator, Policy>& sketch, bool ordered = true);
172154

tuple/include/array_of_strings_sketch_impl.hpp

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,15 @@ default_array_of_strings_update_policy<Allocator>::default_array_of_strings_upda
3333

3434
template<typename Allocator>
3535
auto default_array_of_strings_update_policy<Allocator>::create() const -> array_of_strings {
36-
const string_type empty{string_allocator(allocator_)};
37-
return array_of_strings(0, empty, array_allocator(allocator_));
36+
return array_of_strings(0, "", allocator_);
3837
}
3938

4039
template<typename Allocator>
4140
void default_array_of_strings_update_policy<Allocator>::update(
4241
array_of_strings& array, const array_of_strings& input
4342
) const {
4443
const auto length = static_cast<size_t>(input.size());
45-
const string_type empty{string_allocator(allocator_)};
46-
array = array_of_strings(static_cast<uint8_t>(length), empty, array_allocator(allocator_));
44+
array = array_of_strings(static_cast<uint8_t>(length), "", allocator_);
4745
for (size_t i = 0; i < length; ++i) array[i] = input[i];
4846
}
4947

@@ -52,18 +50,16 @@ void default_array_of_strings_update_policy<Allocator>::update(
5250
array_of_strings& array, const array_of_strings* input
5351
) const {
5452
if (input == nullptr) {
55-
const string_type empty{string_allocator(allocator_)};
56-
array = array_of_strings(0, empty, array_allocator(allocator_));
53+
array = array_of_strings(0, "", allocator_);
5754
return;
5855
}
5956
const auto length = static_cast<size_t>(input->size());
60-
const string_type empty{string_allocator(allocator_)};
61-
array = array_of_strings(static_cast<uint8_t>(length), empty, array_allocator(allocator_));
57+
array = array_of_strings(static_cast<uint8_t>(length), "", allocator_);
6258
for (size_t i = 0; i < length; ++i) array[i] = (*input)[i];
6359
}
6460

6561
template<typename Allocator>
66-
uint64_t hash_array_of_strings_key(const typename array_of_strings_types<Allocator>::array_of_strings& key) {
62+
uint64_t hash_array_of_strings_key(const array<std::string, Allocator>& key) {
6763
// Matches Java Util.PRIME for ArrayOfStrings key hashing.
6864
static constexpr uint64_t STRING_ARR_HASH_SEED = 0x7A3CCA71ULL;
6965
XXHash64 hasher(STRING_ARR_HASH_SEED);
@@ -128,7 +124,7 @@ void default_array_of_strings_serde<Allocator>::serialize(
128124
const uint8_t num_nodes = static_cast<uint8_t>(items[i].size());
129125
write(os, total_bytes);
130126
write(os, num_nodes);
131-
const string_type* data = items[i].data();
127+
const std::string* data = items[i].data();
132128
for (uint8_t j = 0; j < num_nodes; ++j) {
133129
check_utf8(data[j]);
134130
const uint32_t length = static_cast<uint32_t>(data[j].size());
@@ -148,12 +144,11 @@ void default_array_of_strings_serde<Allocator>::deserialize(
148144
const uint8_t num_nodes = read<uint8_t>(is);
149145
if (!is) throw std::runtime_error("array_of_strings stream read failed");
150146
check_num_nodes(num_nodes);
151-
const string_type empty{string_allocator(allocator_)};
152-
array_of_strings array(num_nodes, empty, array_allocator(allocator_));
147+
array_of_strings array(num_nodes, "", allocator_);
153148
for (uint8_t j = 0; j < num_nodes; ++j) {
154149
const uint32_t length = read<uint32_t>(is);
155150
if (!is) throw std::runtime_error("array_of_strings stream read failed");
156-
string_type value(length, '\0', string_allocator(allocator_));
151+
std::string value(length, '\0');
157152
if (length != 0) {
158153
is.read(&value[0], length);
159154
if (!is) throw std::runtime_error("array_of_strings stream read failed");
@@ -179,7 +174,7 @@ size_t default_array_of_strings_serde<Allocator>::serialize(
179174
check_memory_size(bytes_written + total_bytes, capacity);
180175
bytes_written += copy_to_mem(total_bytes, ptr8 + bytes_written);
181176
bytes_written += copy_to_mem(num_nodes, ptr8 + bytes_written);
182-
const string_type* data = items[i].data();
177+
const std::string* data = items[i].data();
183178
for (uint8_t j = 0; j < num_nodes; ++j) {
184179
check_utf8(data[j]);
185180
const uint32_t length = static_cast<uint32_t>(data[j].size());
@@ -207,12 +202,11 @@ size_t default_array_of_strings_serde<Allocator>::deserialize(
207202
uint8_t num_nodes;
208203
bytes_read += copy_from_mem(ptr8 + bytes_read, num_nodes);
209204
check_num_nodes(num_nodes);
210-
const string_type empty{string_allocator(allocator_)};
211-
array_of_strings array(num_nodes, empty, array_allocator(allocator_));
205+
array_of_strings array(num_nodes, "", allocator_);
212206
for (uint8_t j = 0; j < num_nodes; ++j) {
213207
uint32_t length;
214208
bytes_read += copy_from_mem(ptr8 + bytes_read, length);
215-
string_type value(length, '\0', string_allocator(allocator_));
209+
std::string value(length, '\0');
216210
if (length != 0) {
217211
bytes_read += copy_from_mem(ptr8 + bytes_read, &value[0], length);
218212
}
@@ -242,15 +236,15 @@ uint32_t default_array_of_strings_serde<Allocator>::compute_total_bytes(const ar
242236
const auto count = item.size();
243237
check_num_nodes(static_cast<uint8_t>(count));
244238
size_t total = sizeof(uint32_t) + sizeof(uint8_t) + count * sizeof(uint32_t);
245-
const string_type* data = item.data();
239+
const std::string* data = item.data();
246240
for (uint32_t j = 0; j < count; ++j) {
247241
total += data[j].size();
248242
}
249243
return static_cast<uint32_t>(total);
250244
}
251245

252246
template<typename Allocator>
253-
void default_array_of_strings_serde<Allocator>::check_utf8(const string_type& value) {
247+
void default_array_of_strings_serde<Allocator>::check_utf8(const std::string& value) {
254248
if (!utf8::is_valid(value.begin(), value.end())) {
255249
throw std::runtime_error("array_of_strings contains invalid UTF-8");
256250
}

tuple/test/aos_sketch_deserialize_from_java_test.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,13 @@
1717
* under the License.
1818
*/
1919

20-
#include <algorithm>
2120
#include <catch2/catch.hpp>
2221
#include <fstream>
2322
#include <vector>
2423

2524
#include "array_of_strings_sketch.hpp"
2625

2726
namespace datasketches {
28-
using types = array_of_strings_types<std::allocator<char>>;
29-
using string_type = types::string_type;
30-
31-
static bool equals_string(const string_type& lhs, const std::string& rhs) {
32-
return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin());
33-
}
3427
// assume the binary sketches for this test have been generated by datasketches-java code
3528
// in the subdirectory called "java" in the root directory of this project
3629
static std::string testBinaryInputPath = std::string(TEST_BINARY_INPUT_PATH) + "../../java/";
@@ -200,7 +193,7 @@ namespace datasketches {
200193
if (entry.second.size() != expected.size()) continue;
201194
bool equal = true;
202195
for (size_t j = 0; j < expected.size(); ++j) {
203-
if (!equals_string(entry.second[j], expected[j])) {
196+
if (entry.second[j] != expected[j]) {
204197
equal = false;
205198
break;
206199
}
@@ -255,7 +248,7 @@ namespace datasketches {
255248
if (entry.second.size() != expected.size()) continue;
256249
bool equal = true;
257250
for (size_t j = 0; j < expected.size(); ++j) {
258-
if (!equals_string(entry.second[j], expected[j])) {
251+
if (entry.second[j] != expected[j]) {
259252
equal = false;
260253
break;
261254
}

tuple/test/aos_sketch_serialize_for_java.cpp

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,13 @@
2626
namespace datasketches {
2727

2828
using aos_sketch = update_array_of_strings_tuple_sketch<>;
29-
using types = array_of_strings_types<std::allocator<char>>;
30-
using array_of_strings = types::array_of_strings;
31-
using string_allocator = types::string_allocator;
32-
using string_type = types::string_type;
33-
using array_allocator = types::array_allocator;
29+
using array_of_strings = array<std::string>;
3430

3531
static array_of_strings make_array(std::initializer_list<std::string> items) {
36-
const string_type empty{string_allocator()};
37-
array_of_strings array(static_cast<uint8_t>(items.size()), empty, array_allocator());
32+
array_of_strings array(static_cast<uint8_t>(items.size()), "");
3833
size_t i = 0;
3934
for (const auto& item: items) {
40-
array[static_cast<uint8_t>(i)] = string_type(item.data(), item.size(), string_allocator());
35+
array[static_cast<uint8_t>(i)] = item;
4136
++i;
4237
}
4338
return array;
@@ -48,13 +43,10 @@ TEST_CASE("aos sketch generate one value", "[serialize_for_java]") {
4843
for (const unsigned n: n_arr) {
4944
auto sketch = aos_sketch::builder().build();
5045
for (unsigned i = 0; i < n; ++i) {
51-
const string_type empty{string_allocator()};
52-
array_of_strings key(1, empty, array_allocator());
53-
const std::string key_value = std::to_string(i);
54-
key[0] = string_type(key_value.data(), key_value.size(), string_allocator());
55-
array_of_strings value(1, empty, array_allocator());
56-
const std::string value_str = "value" + std::to_string(i);
57-
value[0] = string_type(value_str.data(), value_str.size(), string_allocator());
46+
array_of_strings key(1, "");
47+
key[0] = std::to_string(i);
48+
array_of_strings value(1, "");
49+
value[0] = "value" + std::to_string(i);
5850
sketch.update(hash_array_of_strings_key(key), value);
5951
}
6052
REQUIRE(sketch.is_empty() == (n == 0));
@@ -69,17 +61,12 @@ TEST_CASE("aos sketch generate three values", "[serialize_for_java]") {
6961
for (const unsigned n: n_arr) {
7062
auto sketch = aos_sketch::builder().build();
7163
for (unsigned i = 0; i < n; ++i) {
72-
const string_type empty{string_allocator()};
73-
array_of_strings key(1, empty, array_allocator());
74-
const std::string key_value = std::to_string(i);
75-
key[0] = string_type(key_value.data(), key_value.size(), string_allocator());
76-
array_of_strings value(3, empty, array_allocator());
77-
const std::string value_a = "a" + std::to_string(i);
78-
const std::string value_b = "b" + std::to_string(i);
79-
const std::string value_c = "c" + std::to_string(i);
80-
value[0] = string_type(value_a.data(), value_a.size(), string_allocator());
81-
value[1] = string_type(value_b.data(), value_b.size(), string_allocator());
82-
value[2] = string_type(value_c.data(), value_c.size(), string_allocator());
64+
array_of_strings key(1, "");
65+
key[0] = std::to_string(i);
66+
array_of_strings value(3, "");
67+
value[0] = "a" + std::to_string(i);
68+
value[1] = "b" + std::to_string(i);
69+
value[2] = "c" + std::to_string(i);
8370
sketch.update(hash_array_of_strings_key(key), value);
8471
}
8572
REQUIRE(sketch.is_empty() == (n == 0));
@@ -95,10 +82,9 @@ TEST_CASE("aos sketch generate non-empty no entries", "[serialize_for_java]") {
9582
.set_resize_factor(resize_factor::X8)
9683
.set_p(0.01f)
9784
.build();
98-
const string_type empty{string_allocator()};
99-
array_of_strings key(1, empty, array_allocator());
85+
array_of_strings key(1, "");
10086
key[0] = "key1";
101-
array_of_strings value(1, empty, array_allocator());
87+
array_of_strings value(1, "");
10288
value[0] = "value1";
10389
sketch.update(hash_array_of_strings_key(key), value);
10490
REQUIRE_FALSE(sketch.is_empty());
@@ -112,15 +98,11 @@ TEST_CASE("aos sketch generate multi key strings", "[serialize_for_java]") {
11298
for (const unsigned n: n_arr) {
11399
auto sketch = aos_sketch::builder().build();
114100
for (unsigned i = 0; i < n; ++i) {
115-
const string_type empty{string_allocator()};
116-
array_of_strings key(2, empty, array_allocator());
117-
const std::string key0 = "key" + std::to_string(i);
118-
const std::string key1 = "subkey" + std::to_string(i % 10);
119-
key[0] = string_type(key0.data(), key0.size(), string_allocator());
120-
key[1] = string_type(key1.data(), key1.size(), string_allocator());
121-
array_of_strings value(1, empty, array_allocator());
122-
const std::string value_str = "value" + std::to_string(i);
123-
value[0] = string_type(value_str.data(), value_str.size(), string_allocator());
101+
array_of_strings key(2, "");
102+
key[0] = "key" + std::to_string(i);
103+
key[1] = "subkey" + std::to_string(i % 10);
104+
array_of_strings value(1, "");
105+
value[0] = "value" + std::to_string(i);
124106
sketch.update(hash_array_of_strings_key(key), value);
125107
}
126108
REQUIRE(sketch.is_empty() == (n == 0));

0 commit comments

Comments
 (0)