blob: 1967d2a4317e955c3a1a0a511223767628660aef [file]
/*
* Copyright (C) 2026 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Benchmarks DominatorTree construction for a translated string: one resource
// entry holding a default value plus one value per locale.
//
// The locales below are from core/res/res.
#include <array>
#include <iterator>
#include <memory>
#include <string>
#include <vector>
#include "DominatorTree.h"
#include "ResourceTable.h"
#include "android-base/logging.h"
#include "androidfw/ConfigDescription.h"
#include "benchmark/benchmark.h"
#include "util/Util.h"
using ::android::ConfigDescription;
namespace aapt {
namespace {
// Every locale qualifier under frameworks/base/core/res/res, in directory
// order. A translated string has at most one value per locale.
constexpr std::array kLocales = {
"af", "af-rZA", "am", "am-rET", "ar", "ar-rEG", "ar-rIL", "as", "az",
"be", "bg", "bg-rBG", "bn", "bs", "b+sr+Latn", "ca", "ca-rES", "cs",
"cs-rCZ", "da", "da-rDK", "de", "de-rAT", "de-rCH", "de-rDE", "de-rLI", "el",
"el-rGR", "en-rAU", "en-rCA", "en-rGB", "en-rIE", "en-rIN", "en-rNZ", "en-rSG", "en-rUS",
"en-rXC", "en-rZA", "es", "es-rCO", "es-rCR", "es-rEC", "es-rES", "es-rGT", "es-rHN",
"es-rMX", "es-rNI", "es-rPA", "es-rPE", "es-rSV", "es-rUS", "et", "et-rEE", "eu",
"fa", "fa-rIR", "fi", "fi-rFI", "fr", "fr-rBE", "fr-rCA", "fr-rCH", "fr-rFR",
"gl", "gu", "hi", "hi-rIN", "hr", "hr-rHR", "hu", "hu-rHU", "hy",
"in", "in-rID", "is", "it", "it-rCH", "it-rIT", "iw", "iw-rIL", "ja",
"ja-rJP", "ka", "kk", "km", "kn", "ko", "ko-rKR", "ky", "lo",
"lt", "lt-rLT", "lv", "lv-rLV", "mk", "ml", "mn", "mr", "ms",
"ms-rMY", "my", "nb", "nb-rNO", "ne", "nl", "nl-rBE", "nl-rNL", "or",
"pa", "pl", "pl-rPL", "pt", "pt-rBR", "pt-rPT", "rm-rCH", "ro", "ro-rRO",
"ru", "ru-rRU", "si", "sk", "sk-rSK", "sl", "sl-rSI", "sq", "sr",
"sr-rRS", "sv", "sv-rSE", "sw", "sw-rTZ", "ta", "te", "th", "th-rTH",
"tl", "tl-rPH", "tr", "tr-rTR", "uk", "uk-rUA", "ur", "uz", "vi",
"vi-rVN", "zh-rCN", "zh-rHK", "zh-rTW", "zu", "zu-rZA",
};
ConfigDescription ParseConfig(const std::string& qualifiers) {
ConfigDescription config;
CHECK(ConfigDescription::Parse(qualifiers, &config)) << "invalid config: '" << qualifiers << "'";
return config;
}
// Builds one resource entry for a string translated into |num_locales| locales:
// the default (untranslated) value, then one value per locale.
std::vector<std::unique_ptr<ResourceConfigValue>> MakeTranslatedString(int num_locales) {
CHECK(num_locales <= kLocales.size()) << "only " << kLocales.size() << " locales available";
std::vector<std::unique_ptr<ResourceConfigValue>> values;
values.reserve(num_locales + 1);
values.push_back(util::make_unique<ResourceConfigValue>(ConfigDescription::DefaultConfig(), ""));
for (int i = 0; i < num_locales; i++) {
values.push_back(util::make_unique<ResourceConfigValue>(ParseConfig(kLocales[i]), ""));
}
return values;
}
// Builds the dominator tree for a translated string. state.range(0) is the
// number of locales the string is translated into.
void BM_TranslatedString_BuildTree(benchmark::State& state) {
std::vector<std::unique_ptr<ResourceConfigValue>> values =
MakeTranslatedString(static_cast<int>(state.range(0)));
for (auto _ : state) {
DominatorTree tree(values);
benchmark::DoNotOptimize(tree);
}
}
BENCHMARK(BM_TranslatedString_BuildTree)
->Arg(8)
->Arg(40)
->Arg(80)
->Arg(kLocales.size())
->Unit(benchmark::kNanosecond);
// Times a.Dominates(b) for a pair of config descriptions.
void RunDominates(benchmark::State& state, const char* a_str, const char* b_str) {
ConfigDescription a, b;
CHECK(ConfigDescription::Parse(a_str, &a)) << "invalid config: '" << a_str << "'";
CHECK(ConfigDescription::Parse(b_str, &b)) << "invalid config: '" << b_str << "'";
for (auto _ : state) {
benchmark::DoNotOptimize(a);
benchmark::DoNotOptimize(b);
bool dominates = a.Dominates(b);
benchmark::DoNotOptimize(dominates);
}
}
// Benchmark config descriptions with different regions.
void BM_Dominates_DifferentLocale(benchmark::State& state) {
RunDominates(state, "en-rGB", "en-rUS");
}
BENCHMARK(BM_Dominates_DifferentLocale)->Unit(benchmark::kNanosecond);
// Benchmark same locale, but domination occurs.
void BM_Dominates_SameLocaleDominates(benchmark::State& state) {
RunDominates(state, "en", "en-v21");
}
BENCHMARK(BM_Dominates_SameLocaleDominates)->Unit(benchmark::kNanosecond);
// Benchmark same locale, but no domination occurs.
void BM_Dominates_SameLocaleNoDomination(benchmark::State& state) {
RunDominates(state, "en-land", "en-port");
}
BENCHMARK(BM_Dominates_SameLocaleNoDomination)->Unit(benchmark::kNanosecond);
} // namespace
} // namespace aapt