• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #include "tensorflow/core/lib/strings/proto_serialization.h"
16 
17 #include <cstring>
18 #include "absl/memory/memory.h"
19 #include "absl/strings/string_view.h"
20 #include "tensorflow/core/lib/gtl/inlined_vector.h"
21 #include "tensorflow/core/lib/hash/hash.h"
22 #include "tensorflow/core/platform/logging.h"
23 #include "tensorflow/core/platform/macros.h"
24 
25 namespace tensorflow {
26 namespace {
27 static const int kInlinedBufferSize = 256;
28 }  // namespace
29 
SerializeToStringDeterministic(const protobuf::MessageLite & msg,string * result)30 bool SerializeToStringDeterministic(const protobuf::MessageLite& msg,
31                                     string* result) {
32   const size_t size = msg.ByteSizeLong();
33   DCHECK_LE(size, static_cast<size_t>(INT_MAX));
34   *result = string(size, '\0');
35   return SerializeToBufferDeterministic(msg, const_cast<char*>(result->data()),
36                                         result->size());
37 }
38 
SerializeToBufferDeterministic(const protobuf::MessageLite & msg,char * buffer,size_t size)39 bool SerializeToBufferDeterministic(const protobuf::MessageLite& msg,
40                                     char* buffer, size_t size) {
41   DCHECK(msg.ByteSizeLong() == size && size <= static_cast<size_t>(INT_MAX));
42   protobuf::io::ArrayOutputStream array_stream(buffer, size);
43   protobuf::io::CodedOutputStream output_stream(&array_stream);
44   output_stream.SetSerializationDeterministic(true);
45   msg.SerializeWithCachedSizes(&output_stream);
46   return !output_stream.HadError() && size == output_stream.ByteCount();
47 }
48 
AreSerializedProtosEqual(const protobuf::MessageLite & x,const protobuf::MessageLite & y)49 bool AreSerializedProtosEqual(const protobuf::MessageLite& x,
50                               const protobuf::MessageLite& y) {
51   const size_t size = x.ByteSizeLong();
52   if (size != y.ByteSizeLong()) return false;
53   if (size == 0) return true;
54   gtl::InlinedVector<char, kInlinedBufferSize> x_serialized(size);
55   bool success_x = SerializeToBufferDeterministic(x, x_serialized.data(), size);
56   DCHECK(success_x);
57   gtl::InlinedVector<char, kInlinedBufferSize> y_serialized(size);
58   bool success_y = SerializeToBufferDeterministic(y, y_serialized.data(), size);
59   DCHECK(success_y);
60   return memcmp(x_serialized.data(), y_serialized.data(), size) == 0;
61 }
62 
DeterministicProtoHash64(const protobuf::MessageLite & proto,uint64 seed)63 uint64 DeterministicProtoHash64(const protobuf::MessageLite& proto,
64                                 uint64 seed) {
65   const size_t size = proto.ByteSizeLong();
66   gtl::InlinedVector<char, kInlinedBufferSize> serialized(size);
67   SerializeToBufferDeterministic(proto, serialized.data(), size);
68   return Hash64(serialized.data(), size, seed);
69 }
70 
DeterministicProtoHash64(const protobuf::MessageLite & proto)71 uint64 DeterministicProtoHash64(const protobuf::MessageLite& proto) {
72   const size_t size = proto.ByteSizeLong();
73   gtl::InlinedVector<char, kInlinedBufferSize> serialized(size);
74   SerializeToBufferDeterministic(proto, serialized.data(), size);
75   return Hash64(serialized.data(), size);
76 }
77 
78 }  // namespace tensorflow
79