Files
build/tools/aconfig/templates/FeatureFlagsImpl.java.template
Ted Bauer afe55106e5 Cache Java codegen'd flags in static member variables.
By caching flag values directly in member variables instead of caching
a HashMap and accessing that, flag reads avoid `hashCode()`, map
lookup, and Boolean.parse runtime costs. Flag reads are turning out
to have performance problems in hot paths, so this should help to
alleviate that.

Bug: 309625014
Test: m
Change-Id: I923bf6af2ae3fcbbf2fee7126b492a47cd6049ad
2023-11-14 12:02:01 -05:00

77 lines
2.4 KiB
Plaintext

package {package_name};
// TODO(b/303773055): Remove the annotation after access issue is resolved.
import android.compat.annotation.UnsupportedAppUsage;
{{ if not is_test_mode }}
{{ if is_read_write- }}
import android.provider.DeviceConfig;
import android.provider.DeviceConfig.Properties;
{{ endif }}
/** @hide */
public final class FeatureFlagsImpl implements FeatureFlags \{
{{- if is_read_write }}
{{- for namespace in namespace_set }}
private static boolean {namespace}_is_cached = false;
{{- endfor- }}
{{ for flag in flag_elements }}
{{- if flag.is_read_write }}
private static boolean {flag.method_name} = {flag.default_value};
{{- endif- }}
{{ endfor }}
{{ for namespace in namespace_set }}
private void load_overrides_{namespace}() \{
try \{
Properties properties = DeviceConfig.getProperties("{namespace}");
{{- for flag in flag_elements }}
{{- if flag.is_read_write }}
{flag.method_name} =
properties.getBoolean("{flag.device_config_flag}", {flag.default_value});
{{- endif- }}
{{ endfor }}
} catch (NullPointerException e) \{
throw new RuntimeException(
"Cannot read value from namespace {namespace} "
+ "from DeviceConfig. It could be that the code using flag "
+ "executed before SettingsProvider initialization. Please use "
+ "fixed read-only flag by adding is_fixed_read_only: true in "
+ "flag declaration.",
e
);
}
{namespace}_is_cached = true;
}
{{ endfor- }}
{{ endif- }}
{{ for flag in flag_elements }}
@Override
@UnsupportedAppUsage
public boolean {flag.method_name}() \{
{{ -if flag.is_read_write }}
if (!{flag.device_config_namespace}_is_cached) \{
load_overrides_{flag.device_config_namespace}();
}
return {flag.method_name};
{{ else }}
return {flag.default_value};
{{ endif- }}
}
{{ endfor }}
}
{{ else }}
{#- Generate only stub if in test mode #}
/** @hide */
public final class FeatureFlagsImpl implements FeatureFlags \{
{{ for flag in flag_elements }}
@Override
@UnsupportedAppUsage
public boolean {flag.method_name}() \{
throw new UnsupportedOperationException(
"Method is not implemented.");
}
{{ endfor- }}
}
{{ endif }}