message_layout.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (c) 2009-2021, Google LLC
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of Google LLC nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
  20. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef UPBC_MESSAGE_LAYOUT_H
  28. #define UPBC_MESSAGE_LAYOUT_H
  29. #include "absl/base/macros.h"
  30. #include "absl/container/flat_hash_map.h"
  31. #include "google/protobuf/descriptor.h"
  32. namespace upbc {
  33. class MessageLayout {
  34. public:
  35. struct Size {
  36. void Add(const Size& other) {
  37. size32 += other.size32;
  38. size64 += other.size64;
  39. }
  40. void MaxFrom(const Size& other) {
  41. size32 = std::max(size32, other.size32);
  42. size64 = std::max(size64, other.size64);
  43. }
  44. void AlignUp(const Size& align) {
  45. size32 = Align(size32, align.size32);
  46. size64 = Align(size64, align.size64);
  47. }
  48. int64_t size32;
  49. int64_t size64;
  50. };
  51. struct SizeAndAlign {
  52. Size size;
  53. Size align;
  54. void MaxFrom(const SizeAndAlign& other) {
  55. size.MaxFrom(other.size);
  56. align.MaxFrom(other.align);
  57. }
  58. };
  59. MessageLayout(const google::protobuf::Descriptor* descriptor) {
  60. ComputeLayout(descriptor);
  61. }
  62. Size GetFieldOffset(const google::protobuf::FieldDescriptor* field) const {
  63. return GetMapValue(field_offsets_, field);
  64. }
  65. Size GetOneofCaseOffset(
  66. const google::protobuf::OneofDescriptor* oneof) const {
  67. return GetMapValue(oneof_case_offsets_, oneof);
  68. }
  69. int GetHasbitIndex(const google::protobuf::FieldDescriptor* field) const {
  70. return GetMapValue(hasbit_indexes_, field);
  71. }
  72. Size message_size() const { return size_; }
  73. int hasbit_count() const { return hasbit_count_; }
  74. int hasbit_bytes() const { return hasbit_bytes_; }
  75. // Required fields always have the lowest hasbits.
  76. int required_count() const { return required_count_; }
  77. static bool HasHasbit(const google::protobuf::FieldDescriptor* field);
  78. static SizeAndAlign SizeOfUnwrapped(
  79. const google::protobuf::FieldDescriptor* field);
  80. private:
  81. void ComputeLayout(const google::protobuf::Descriptor* descriptor);
  82. void PlaceNonOneofFields(const google::protobuf::Descriptor* descriptor);
  83. void PlaceOneofFields(const google::protobuf::Descriptor* descriptor);
  84. Size Place(SizeAndAlign size_and_align);
  85. template <class K, class V>
  86. static V GetMapValue(const absl::flat_hash_map<K, V>& map, K key) {
  87. auto iter = map.find(key);
  88. if (iter == map.end()) {
  89. fprintf(stderr, "No value for field.\n");
  90. abort();
  91. }
  92. return iter->second;
  93. }
  94. static bool IsPowerOfTwo(size_t val) { return (val & (val - 1)) == 0; }
  95. static size_t Align(size_t val, size_t align) {
  96. ABSL_ASSERT(IsPowerOfTwo(align));
  97. return (val + align - 1) & ~(align - 1);
  98. }
  99. static SizeAndAlign SizeOf(const google::protobuf::FieldDescriptor* field);
  100. static int64_t FieldLayoutRank(
  101. const google::protobuf::FieldDescriptor* field);
  102. absl::flat_hash_map<const google::protobuf::FieldDescriptor*, Size>
  103. field_offsets_;
  104. absl::flat_hash_map<const google::protobuf::FieldDescriptor*, int>
  105. hasbit_indexes_;
  106. absl::flat_hash_map<const google::protobuf::OneofDescriptor*, Size>
  107. oneof_case_offsets_;
  108. Size maxalign_;
  109. Size size_;
  110. int hasbit_count_;
  111. int hasbit_bytes_;
  112. int required_count_;
  113. };
  114. // Returns fields in order of "hotness", eg. how frequently they appear in
  115. // serialized payloads. Ideally this will use a profile. When we don't have
  116. // that, we assume that fields with smaller numbers are used more frequently.
  117. inline std::vector<const google::protobuf::FieldDescriptor*> FieldHotnessOrder(
  118. const google::protobuf::Descriptor* message) {
  119. std::vector<const google::protobuf::FieldDescriptor*> fields;
  120. for (int i = 0; i < message->field_count(); i++) {
  121. fields.push_back(message->field(i));
  122. }
  123. std::sort(fields.begin(), fields.end(),
  124. [](const google::protobuf::FieldDescriptor* a,
  125. const google::protobuf::FieldDescriptor* b) {
  126. return std::make_pair(!a->is_required(), a->number()) <
  127. std::make_pair(!b->is_required(), b->number());
  128. });
  129. return fields;
  130. }
  131. } // namespace upbc
  132. #endif // UPBC_MESSAGE_LAYOUT_H