// Protocol Buffers - Google's data interchange format // Copyright 2008 Google Inc. All rights reserved. // https://developers.google.com/protocol-buffers/ // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: // // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above // copyright notice, this list of conditions and the following disclaimer // in the documentation and/or other materials provided with the // distribution. // * Neither the name of Google Inc. nor the names of its // contributors may be used to endorse or promote products derived from // this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include <google/protobuf/compiler/cpp/padding_optimizer.h> #include <google/protobuf/compiler/cpp/helpers.h> namespace google { namespace protobuf { namespace compiler { namespace cpp { namespace { // FieldGroup is just a helper for PaddingOptimizer below. It holds a vector of // fields that are grouped together because they have compatible alignment, and // a preferred location in the final field ordering. class FieldGroup { … }; } // namespace // Reorder 'fields' so that if the fields are output into a c++ class in the new // order, fields of similar family (see below) are together and within each // family, alignment padding is minimized. // // We try to do this while keeping each field as close as possible to its field // number order so that we don't reduce cache locality much for function that // access each field in order. Originally, OptimizePadding used declaration // order for its decisions, but generated code minus the serializer/parsers uses // the output of OptimizePadding as well (stored in // MessageGenerator::optimized_order_). Since the serializers use field number // order, we use that as a tie-breaker. // // We classify each field into a particular "family" of fields, that we perform // the same operation on in our generated functions. // // REPEATED is placed first, as the C++ compiler automatically initializes // these fields in layout order. // // STRING is grouped next, as our Clear/SharedCtor/SharedDtor walks it and // calls ArenaStringPtr::Destroy on each. // // LAZY_MESSAGE is grouped next, as it interferes with the ability to memset // non-repeated fields otherwise. // // MESSAGE is grouped next, as our Clear/SharedDtor code walks it and calls // delete on each. We initialize these fields with a NULL pointer (see // MessageFieldGenerator::GenerateConstructorCode), which allows them to be // memset. // // ZERO_INITIALIZABLE is memset in Clear/SharedCtor // // OTHER these fields are initialized one-by-one. void PaddingOptimizer::OptimizeLayout( std::vector<const FieldDescriptor*>* fields, const Options& options, MessageSCCAnalyzer* scc_analyzer) { … } } // namespace cpp } // namespace compiler } // namespace protobuf } // namespace google