diff options
author | Alexander Hendrich <hendrich@chromium.org> | 2018-12-04 21:27:01 +0900 |
---|---|---|
committer | Qijiang Fan <fqj@google.com> | 2020-06-05 11:22:51 +0900 |
commit | ad1a98751f8f48915cd19f9a3f5c508c893793cc (patch) | |
tree | 1f990b0c3dfcd00b0a977195f972a0b9e7ef1da7 | |
parent | ad8dfb0a86cd9ea078ad50e4d3a2eb596b041d45 (diff) | |
download | libchrome-ad1a98751f8f48915cd19f9a3f5c508c893793cc.tar.gz |
Reduce schema struct size
This CL reduces the size of structs used to store the policy schemas.
SchemaNode: 131 * 4 byte = 524 byte
PropertiesNodes: 56 * 12 byte = 676 byte
PropertyNodes: 517 * 0 byte = 0 byte
Total: ~1kb
(The previous CLs also reduce the number of SchemaNodes from 180 to
131, 49 * 8 byte = 392 byte)
The changes to PropertyNodes does not actually result in any size
reduction due to struct alignment, but since all indices are now shorts
and the |InternalStorage::Parse()| method also uses pointers to shorts,
it makes sense to update the type there as well.
Bug: none
Change-Id: Ie2a301e3e5006e12330a383fa83319d214e61063
Reviewed-on: https://chromium-review.googlesource.com/c/1343109
Commit-Queue: Alexander Hendrich <hendrich@chromium.org>
Reviewed-by: Lutz Justen <ljusten@chromium.org>
Cr-Commit-Position: refs/heads/master@{#613520}
CrOS-Libchrome-Original-Commit: e8db73c06d07dc60e7bcfd710c319eb44ec9d969
-rw-r--r-- | components/policy/core/common/schema.cc | 20 | ||||
-rw-r--r-- | components/policy/core/common/schema_internal.h | 16 |
2 files changed, 21 insertions, 15 deletions
diff --git a/components/policy/core/common/schema.cc b/components/policy/core/common/schema.cc index c15e9b5514..2cf6930d17 100644 --- a/components/policy/core/common/schema.cc +++ b/components/policy/core/common/schema.cc @@ -39,12 +39,12 @@ namespace { struct ReferencesAndIDs { // Maps schema "id" attributes to the corresponding SchemaNode index. - std::map<std::string, int> id_map; + std::map<std::string, short> id_map; // List of pairs of references to be assigned later. The string is the "id" // whose corresponding index should be stored in the pointer, once all the IDs // are available. - std::vector<std::pair<std::string, int*>> reference_list; + std::vector<std::pair<std::string, short*>> reference_list; }; // Sizes for the storage arrays. These are calculated in advance so that the @@ -78,7 +78,7 @@ constexpr char kSensitiveValueMask[] = "********"; // An invalid index, indicating that a node is not present; similar to a NULL // pointer. -const int kInvalid = -1; +const short kInvalid = -1; // Maps a schema key to the corresponding base::Value::Type struct SchemaKeyToValueType { @@ -576,7 +576,7 @@ class Schema::InternalStorage // If |schema| is invalid then |error| gets the error reason and false is // returned. Otherwise returns true. bool Parse(const base::DictionaryValue& schema, - int* index, + short* index, ReferencesAndIDs* references_and_ids, std::string* error); @@ -681,7 +681,7 @@ Schema::InternalStorage::ParseSchema(const base::DictionaryValue& schema, storage->int_enums_.reserve(sizes.int_enums); storage->string_enums_.reserve(sizes.string_enums); - int root_index = kInvalid; + short root_index = kInvalid; ReferencesAndIDs references_and_ids; if (!storage->Parse(schema, &root_index, &references_and_ids, error)) return nullptr; @@ -822,7 +822,7 @@ void Schema::InternalStorage::DetermineStorageSizes( } bool Schema::InternalStorage::Parse(const base::DictionaryValue& schema, - int* index, + short* index, ReferencesAndIDs* references_and_ids, std::string* error) { std::string ref_string; @@ -848,7 +848,13 @@ bool Schema::InternalStorage::Parse(const base::DictionaryValue& schema, return false; } - *index = static_cast<int>(schema_nodes_.size()); + if (schema_nodes_.size() > std::numeric_limits<short>::max()) { + *error = "Can't have more than " + + std::to_string(std::numeric_limits<short>::max()) + + " schema nodes."; + return false; + } + *index = static_cast<short>(schema_nodes_.size()); schema_nodes_.push_back(SchemaNode()); SchemaNode* schema_node = &schema_nodes_.back(); schema_node->type = type; diff --git a/components/policy/core/common/schema_internal.h b/components/policy/core/common/schema_internal.h index fe2937cca8..7213cf0c62 100644 --- a/components/policy/core/common/schema_internal.h +++ b/components/policy/core/common/schema_internal.h @@ -34,7 +34,7 @@ struct POLICY_EXPORT SchemaNode { // RestrictionNode describing the restriction on the value. // // Otherwise extra is -1 and is invalid. - int extra; + short extra; // True if this value is sensitive and should be masked before displaying it // to the user. @@ -51,14 +51,14 @@ struct POLICY_EXPORT PropertyNode { // An offset into SchemaData::schema_nodes that indexes the SchemaNode // describing the structure of this key. - int schema; + short schema; }; // Represents the list of keys of a map policy. struct POLICY_EXPORT PropertiesNode { // An offset into SchemaData::property_nodes that indexes the PropertyNode // describing the first known property of this map policy. - int begin; + short begin; // An offset into SchemaData::property_nodes that indexes the PropertyNode // right beyond the last known property of this map policy. @@ -68,18 +68,18 @@ struct POLICY_EXPORT PropertiesNode { // // Note that the range [begin, end) is sorted by PropertyNode::key, so that // properties can be looked up by binary searching in the range. - int end; + short end; // An offset into SchemaData::property_nodes that indexes the PropertyNode // right beyond the last known pattern property. // // [end, pattern_end) is the range that covers all pattern properties // defined. It's not required to be sorted. - int pattern_end; + short pattern_end; // An offset into SchemaData::required_properties that indexes the first // required property of this map policy. - int required_begin; + short required_begin; // An offset into SchemaData::required_properties that indexes the property // right beyond the last required property. @@ -88,14 +88,14 @@ struct POLICY_EXPORT PropertiesNode { // PropertiesNode corresponds to does not have any required properties. // // Note that the range [required_begin, required_end) is not sorted. - int required_end; + short required_end; // If this map policy supports keys with any value (besides the well-known // values described in the range [begin, end)) then |additional| is an offset // into SchemaData::schema_nodes that indexes the SchemaNode describing the // structure of the values for those keys. Otherwise |additional| is -1 and // is invalid. - int additional; + short additional; }; // Represents the restriction on Type::INTEGER or Type::STRING instance of |