blob: 3aab2e3a0c785a52182fad5119e0588856369222 [file] [log] [blame]
Alexandria Cornwall77788eb2016-09-06 15:16:49 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Adam Lesinskid48944a2017-02-21 14:22:30 -080017#include "optimize/ResourceDeduper.h"
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070018
19#include <algorithm>
20
Adam Lesinskice5e56e2016-10-21 17:56:45 -070021#include "DominatorTree.h"
22#include "ResourceTable.h"
23
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070024namespace aapt {
25
26namespace {
27
28/**
29 * Remove duplicated key-value entries from dominated resources.
30 *
31 * Based on the dominator tree, we can remove a value of an entry if:
32 *
33 * 1. The configuration for the entry's value is dominated by a configuration
34 * with an equivalent entry value.
35 * 2. All compatible configurations for the entry (those not in conflict and
36 * unrelated by domination with the configuration for the entry's value) have
37 * an equivalent entry value.
38 */
39class DominatedKeyValueRemover : public DominatorTree::BottomUpVisitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070040 public:
41 using Node = DominatorTree::Node;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070042
Adam Lesinskicacb28f2016-10-19 12:18:14 -070043 explicit DominatedKeyValueRemover(IAaptContext* context, ResourceEntry* entry)
Adam Lesinskice5e56e2016-10-21 17:56:45 -070044 : context_(context), entry_(entry) {}
Adam Lesinskicacb28f2016-10-19 12:18:14 -070045
Adam Lesinskice5e56e2016-10-21 17:56:45 -070046 void VisitConfig(Node* node) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070047 Node* parent = node->parent();
48 if (!parent) {
49 return;
50 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070051 ResourceConfigValue* node_value = node->value();
52 ResourceConfigValue* parent_value = parent->value();
53 if (!node_value || !parent_value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 return;
55 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070056 if (!node_value->value->Equals(parent_value->value.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070057 return;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070058 }
59
Adam Lesinskicacb28f2016-10-19 12:18:14 -070060 // Compare compatible configs for this entry and ensure the values are
61 // equivalent.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070062 const ConfigDescription& node_configuration = node_value->config;
63 for (const auto& sibling : entry_->values) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070064 if (!sibling->value) {
65 // Sibling was already removed.
66 continue;
67 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070068 if (node_configuration.IsCompatibleWith(sibling->config) &&
69 !node_value->value->Equals(sibling->value.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070 // The configurations are compatible, but the value is
71 // different, so we can't remove this value.
72 return;
73 }
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070074 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070075 if (context_->IsVerbose()) {
76 context_->GetDiagnostics()->Note(
77 DiagMessage(node_value->value->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -070078 << "removing dominated duplicate resource with name \""
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079 << entry_->name << "\"");
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070081 node_value->value = {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082 }
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070083
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070085 DISALLOW_COPY_AND_ASSIGN(DominatedKeyValueRemover);
86
87 IAaptContext* context_;
88 ResourceEntry* entry_;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070089};
90
Adam Lesinskice5e56e2016-10-21 17:56:45 -070091static void DedupeEntry(IAaptContext* context, ResourceEntry* entry) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070092 DominatorTree tree(entry->values);
93 DominatedKeyValueRemover remover(context, entry);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070094 tree.Accept(&remover);
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070095
Adam Lesinskicacb28f2016-10-19 12:18:14 -070096 // Erase the values that were removed.
97 entry->values.erase(
98 std::remove_if(
99 entry->values.begin(), entry->values.end(),
100 [](const std::unique_ptr<ResourceConfigValue>& val) -> bool {
101 return val == nullptr || val->value == nullptr;
102 }),
103 entry->values.end());
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700104}
105
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700106} // namespace
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700107
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700108bool ResourceDeduper::Consume(IAaptContext* context, ResourceTable* table) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700109 for (auto& package : table->packages) {
110 for (auto& type : package->types) {
111 for (auto& entry : type->entries) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700112 DedupeEntry(context, entry.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700113 }
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700114 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700115 }
116 return true;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700117}
118
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700119} // namespace aapt