aconfig: Cache flag values for c/c++ codegen
Bug: b/307336730 Test: atest aconfig.test Change-Id: Id604cf154d09a48f657277af6d799f0e17bc4e93
This commit is contained in:
@@ -31,9 +31,10 @@ pub fn generate_cpp_code<'a, I>(
|
|||||||
where
|
where
|
||||||
I: Iterator<Item = &'a ProtoParsedFlag>,
|
I: Iterator<Item = &'a ProtoParsedFlag>,
|
||||||
{
|
{
|
||||||
|
let mut readwrite_count = 0;
|
||||||
let class_elements: Vec<ClassElement> =
|
let class_elements: Vec<ClassElement> =
|
||||||
parsed_flags_iter.map(|pf| create_class_element(package, pf)).collect();
|
parsed_flags_iter.map(|pf| create_class_element(package, pf, &mut readwrite_count)).collect();
|
||||||
let readwrite = class_elements.iter().any(|item| item.readwrite);
|
let readwrite = readwrite_count > 0;
|
||||||
let has_fixed_read_only = class_elements.iter().any(|item| item.is_fixed_read_only);
|
let has_fixed_read_only = class_elements.iter().any(|item| item.is_fixed_read_only);
|
||||||
let header = package.replace('.', "_");
|
let header = package.replace('.', "_");
|
||||||
let package_macro = header.to_uppercase();
|
let package_macro = header.to_uppercase();
|
||||||
@@ -46,6 +47,7 @@ where
|
|||||||
package,
|
package,
|
||||||
has_fixed_read_only,
|
has_fixed_read_only,
|
||||||
readwrite,
|
readwrite,
|
||||||
|
readwrite_count,
|
||||||
for_test: codegen_mode == CodegenMode::Test,
|
for_test: codegen_mode == CodegenMode::Test,
|
||||||
class_elements,
|
class_elements,
|
||||||
};
|
};
|
||||||
@@ -88,12 +90,14 @@ pub struct Context<'a> {
|
|||||||
pub package: &'a str,
|
pub package: &'a str,
|
||||||
pub has_fixed_read_only: bool,
|
pub has_fixed_read_only: bool,
|
||||||
pub readwrite: bool,
|
pub readwrite: bool,
|
||||||
|
pub readwrite_count: i32,
|
||||||
pub for_test: bool,
|
pub for_test: bool,
|
||||||
pub class_elements: Vec<ClassElement>,
|
pub class_elements: Vec<ClassElement>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
pub struct ClassElement {
|
pub struct ClassElement {
|
||||||
|
pub readwrite_idx: i32,
|
||||||
pub readwrite: bool,
|
pub readwrite: bool,
|
||||||
pub is_fixed_read_only: bool,
|
pub is_fixed_read_only: bool,
|
||||||
pub default_value: String,
|
pub default_value: String,
|
||||||
@@ -103,8 +107,13 @@ pub struct ClassElement {
|
|||||||
pub device_config_flag: String,
|
pub device_config_flag: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_class_element(package: &str, pf: &ProtoParsedFlag) -> ClassElement {
|
fn create_class_element(package: &str, pf: &ProtoParsedFlag, rw_count: &mut i32) -> ClassElement {
|
||||||
ClassElement {
|
ClassElement {
|
||||||
|
readwrite_idx: if pf.permission() == ProtoFlagPermission::READ_WRITE {
|
||||||
|
let index = *rw_count; *rw_count += 1; index
|
||||||
|
} else {
|
||||||
|
-1
|
||||||
|
},
|
||||||
readwrite: pf.permission() == ProtoFlagPermission::READ_WRITE,
|
readwrite: pf.permission() == ProtoFlagPermission::READ_WRITE,
|
||||||
is_fixed_read_only: pf.is_fixed_read_only(),
|
is_fixed_read_only: pf.is_fixed_read_only(),
|
||||||
default_value: if pf.state() == ProtoFlagState::ENABLED {
|
default_value: if pf.state() == ProtoFlagState::ENABLED {
|
||||||
@@ -139,8 +148,12 @@ mod tests {
|
|||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
namespace com::android::aconfig::test {
|
namespace com::android::aconfig::test {
|
||||||
|
|
||||||
|
extern std::vector<int8_t> cache_;
|
||||||
|
|
||||||
class flag_provider_interface {
|
class flag_provider_interface {
|
||||||
public:
|
public:
|
||||||
virtual ~flag_provider_interface() = default;
|
virtual ~flag_provider_interface() = default;
|
||||||
@@ -330,11 +343,14 @@ namespace com::android::aconfig::test {
|
|||||||
}
|
}
|
||||||
|
|
||||||
virtual bool disabled_rw() override {
|
virtual bool disabled_rw() override {
|
||||||
return server_configurable_flags::GetServerConfigurableFlag(
|
if (cache_[0] == -1) {
|
||||||
|
cache_[0] = server_configurable_flags::GetServerConfigurableFlag(
|
||||||
"aconfig_flags.aconfig_test",
|
"aconfig_flags.aconfig_test",
|
||||||
"com.android.aconfig.test.disabled_rw",
|
"com.android.aconfig.test.disabled_rw",
|
||||||
"false") == "true";
|
"false") == "true";
|
||||||
}
|
}
|
||||||
|
return cache_[0];
|
||||||
|
}
|
||||||
|
|
||||||
virtual bool enabled_fixed_ro() override {
|
virtual bool enabled_fixed_ro() override {
|
||||||
return COM_ANDROID_ACONFIG_TEST_ENABLED_FIXED_RO;
|
return COM_ANDROID_ACONFIG_TEST_ENABLED_FIXED_RO;
|
||||||
@@ -345,14 +361,19 @@ namespace com::android::aconfig::test {
|
|||||||
}
|
}
|
||||||
|
|
||||||
virtual bool enabled_rw() override {
|
virtual bool enabled_rw() override {
|
||||||
return server_configurable_flags::GetServerConfigurableFlag(
|
if (cache_[1] == -1) {
|
||||||
|
cache_[1] = server_configurable_flags::GetServerConfigurableFlag(
|
||||||
"aconfig_flags.aconfig_test",
|
"aconfig_flags.aconfig_test",
|
||||||
"com.android.aconfig.test.enabled_rw",
|
"com.android.aconfig.test.enabled_rw",
|
||||||
"true") == "true";
|
"true") == "true";
|
||||||
}
|
}
|
||||||
|
return cache_[1];
|
||||||
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
std::vector<int8_t> cache_ = std::vector<int8_t>(2, -1);
|
||||||
|
|
||||||
std::unique_ptr<flag_provider_interface> provider_ =
|
std::unique_ptr<flag_provider_interface> provider_ =
|
||||||
std::make_unique<flag_provider>();
|
std::make_unique<flag_provider>();
|
||||||
}
|
}
|
||||||
|
@@ -18,10 +18,16 @@
|
|||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
{{ if not for_test- }}
|
||||||
|
#include <vector>
|
||||||
|
{{ -endif }}
|
||||||
|
|
||||||
namespace {cpp_namespace} \{
|
namespace {cpp_namespace} \{
|
||||||
|
|
||||||
|
{{ if not for_test- }}
|
||||||
|
extern std::vector<int8_t> cache_;
|
||||||
|
{{ -endif }}
|
||||||
|
|
||||||
class flag_provider_interface \{
|
class flag_provider_interface \{
|
||||||
public:
|
public:
|
||||||
virtual ~flag_provider_interface() = default;
|
virtual ~flag_provider_interface() = default;
|
||||||
|
@@ -53,10 +53,13 @@ namespace {cpp_namespace} \{
|
|||||||
{{ for item in class_elements}}
|
{{ for item in class_elements}}
|
||||||
virtual bool {item.flag_name}() override \{
|
virtual bool {item.flag_name}() override \{
|
||||||
{{ if item.readwrite- }}
|
{{ if item.readwrite- }}
|
||||||
return server_configurable_flags::GetServerConfigurableFlag(
|
if (cache_[{item.readwrite_idx}] == -1) \{
|
||||||
|
cache_[{item.readwrite_idx}] = server_configurable_flags::GetServerConfigurableFlag(
|
||||||
"aconfig_flags.{item.device_config_namespace}",
|
"aconfig_flags.{item.device_config_namespace}",
|
||||||
"{item.device_config_flag}",
|
"{item.device_config_flag}",
|
||||||
"{item.default_value}") == "true";
|
"{item.default_value}") == "true";
|
||||||
|
}
|
||||||
|
return cache_[{item.readwrite_idx}];
|
||||||
{{ -else- }}
|
{{ -else- }}
|
||||||
{{ if item.is_fixed_read_only }}
|
{{ if item.is_fixed_read_only }}
|
||||||
return {package_macro}_{item.flag_macro};
|
return {package_macro}_{item.flag_macro};
|
||||||
@@ -68,13 +71,14 @@ namespace {cpp_namespace} \{
|
|||||||
{{ endfor }}
|
{{ endfor }}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
std::vector<int8_t> cache_ = std::vector<int8_t>({readwrite_count}, -1);
|
||||||
|
|
||||||
{{ -endif }}
|
{{ -endif }}
|
||||||
|
|
||||||
|
|
||||||
std::unique_ptr<flag_provider_interface> provider_ =
|
std::unique_ptr<flag_provider_interface> provider_ =
|
||||||
std::make_unique<flag_provider>();
|
std::make_unique<flag_provider>();
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user