Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(341)

Unified Diff: mojo/public/cpp/bindings/lib/serialization.h

Issue 2738643004: [Not for commit] Test performance impact if string validation checks whether it is utf8.
Patch Set: . Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | mojo/public/cpp/bindings/lib/string_serialization.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/public/cpp/bindings/lib/serialization.h
diff --git a/mojo/public/cpp/bindings/lib/serialization.h b/mojo/public/cpp/bindings/lib/serialization.h
index 359b02b77c25a55636560bb7d94f28e28a0bf702..409e07213d1b9b393bebf318d08501e825389305 100644
--- a/mojo/public/cpp/bindings/lib/serialization.h
+++ b/mojo/public/cpp/bindings/lib/serialization.h
@@ -7,6 +7,7 @@
#include <string.h>
+#include "base/strings/string_util.h"
#include "mojo/public/cpp/bindings/array_traits_carray.h"
#include "mojo/public/cpp/bindings/array_traits_stl.h"
#include "mojo/public/cpp/bindings/lib/array_serialization.h"
@@ -25,6 +26,77 @@
namespace mojo {
namespace internal {
+inline std::vector<uint8_t> SerializeString(const std::string& value) {
+ SerializationContext context;
+ size_t size = PrepareToSerialize<StringDataView>(value, &context);
+ DCHECK_EQ(size, Align(size));
+
+ std::vector<uint8_t> result(size);
+ if (size == 0)
+ return result;
+
+ void* result_buffer = &result.front();
+ // The serialization logic requires that the buffer is 8-byte aligned. If the
+ // result buffer is not properly aligned, we have to do an extra copy. In
+ // practice, this should never happen for std::vector.
+ bool need_copy = !IsAligned(result_buffer);
+
+ if (need_copy) {
+ // calloc sets the memory to all zero.
+ result_buffer = calloc(size, 1);
+ DCHECK(IsAligned(result_buffer));
+ }
+
+ Buffer buffer;
+ buffer.Initialize(result_buffer, size);
+ typename MojomTypeTraits<StringDataView>::Data* data = nullptr;
+ Serialize<StringDataView>(value, &buffer, &data, &context);
+
+ if (need_copy) {
+ memcpy(&result.front(), result_buffer, size);
+ free(result_buffer);
+ }
+
+ return result;
+}
+
+inline bool DeserializeString(bool utf8_check,
+ const std::vector<uint8_t>& input,
+ std::string* output) {
+ using DataType = MojomTypeTraits<StringDataView>::Data;
+
+ void* input_buffer =
+ input.size() == 0
+ ? nullptr
+ : const_cast<void*>(reinterpret_cast<const void*>(&input.front()));
+
+ // Please see comments in StructSerializeImpl.
+ bool need_copy = !IsAligned(input_buffer);
+
+ if (need_copy) {
+ input_buffer = malloc(input.size());
+ DCHECK(IsAligned(input_buffer));
+ memcpy(input_buffer, &input.front(), input.size());
+ }
+
+ ValidationContext validation_context(input_buffer, input.size(), 0, 0);
+ ContainerValidateParams params(0, false, nullptr);
+ bool result = false;
+ if (DataType::Validate(input_buffer, &validation_context, &params)) {
+ DataType* data = reinterpret_cast<DataType*>(input_buffer);
+ base::StringPiece string_piece(data->storage(), data->size());
+ if (!utf8_check || base::IsStringUTF8(string_piece)) {
+ SerializationContext context;
+ result = Deserialize<StringDataView>(data, output, &context);
+ }
+ }
+
+ if (need_copy)
+ free(input_buffer);
+
+ return result;
+}
+
template <typename MojomType, typename DataArrayType, typename UserType>
DataArrayType StructSerializeImpl(UserType* input) {
static_assert(BelongsTo<MojomType, MojomTypeCategory::STRUCT>::value,
« no previous file with comments | « no previous file | mojo/public/cpp/bindings/lib/string_serialization.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698