Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite05db5c

Browse files
Optimize redaction state calculation
Memoizes the state, shifts to using a single `volatile` field, and updates `TextFormat` to look at the state directly.PiperOrigin-RevId: 803053745
1 parentc47f34c commite05db5c

File tree

2 files changed

+56
-38
lines changed

2 files changed

+56
-38
lines changed

‎java/core/src/main/java/com/google/protobuf/Descriptors.java‎

Lines changed: 54 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1753,15 +1753,37 @@ public String toString() {
17531753
privatefinalDescriptorextensionScope;
17541754
privatefinalbooleanisProto3Optional;
17551755

1756-
privateenumSensitivity {
1757-
UNKNOWN,
1758-
SENSITIVE,
1759-
NOT_SENSITIVE
1756+
staticfinalclassRedactionState {
1757+
privatestaticfinalRedactionStateFALSE_FALSE =newRedactionState(false,false);
1758+
privatestaticfinalRedactionStateFALSE_TRUE =newRedactionState(false,true);
1759+
privatestaticfinalRedactionStateTRUE_FALSE =newRedactionState(true,false);
1760+
privatestaticfinalRedactionStateTRUE_TRUE =newRedactionState(true,true);
1761+
1762+
finalbooleanredact;
1763+
finalbooleanreport;
1764+
1765+
privateRedactionState(booleanredact,booleanreport) {
1766+
this.redact =redact;
1767+
this.report =report;
1768+
}
1769+
1770+
privatestaticRedactionStateof(booleanredact) {
1771+
returnof(redact,false);
1772+
}
1773+
1774+
privatestaticRedactionStateof(booleanredact,booleanreport) {
1775+
if (redact) {
1776+
returnreport ?TRUE_TRUE :TRUE_FALSE;
1777+
}
1778+
returnreport ?FALSE_TRUE :FALSE_FALSE;
1779+
}
1780+
1781+
privatestaticRedactionStatecombine(RedactionStatelhs,RedactionStaterhs) {
1782+
returnof(lhs.redact ||rhs.redact,rhs.report);
1783+
}
17601784
}
17611785

1762-
// Caches the result of isSensitive() for performance reasons.
1763-
privatevolatileSensitivitysensitivity =Sensitivity.UNKNOWN;
1764-
privatevolatilebooleanisReportable =false;
1786+
privatevolatileRedactionStateredactionState;
17651787

17661788
// Possibly initialized during cross-linking.
17671789
privateTypetype;
@@ -1937,73 +1959,68 @@ private FieldDescriptor(
19371959
}
19381960

19391961
@SuppressWarnings("unchecked")// List<EnumValueDescriptor> guaranteed by protobuf runtime.
1940-
privateList<Boolean>isOptionSensitive(FieldDescriptorfield,Objectvalue) {
1962+
privatestaticRedactionStateisOptionSensitive(FieldDescriptorfield,Objectvalue) {
19411963
if (field.getType() ==Descriptors.FieldDescriptor.Type.ENUM) {
19421964
if (field.isRepeated()) {
19431965
for (EnumValueDescriptorv : (List<EnumValueDescriptor>)value) {
19441966
if (v.getOptions().getDebugRedact()) {
1945-
returnArrays.asList(true,false);
1967+
returnRedactionState.of(true,false);
19461968
}
19471969
}
19481970
}else {
19491971
if (((EnumValueDescriptor)value).getOptions().getDebugRedact()) {
1950-
returnArrays.asList(true,false);
1972+
returnRedactionState.of(true,false);
19511973
}
19521974
}
19531975
}elseif (field.getJavaType() ==Descriptors.FieldDescriptor.JavaType.MESSAGE) {
19541976
if (field.isRepeated()) {
19551977
for (Messagem : (List<Message>)value) {
19561978
for (Map.Entry<FieldDescriptor,Object>entry :m.getAllFields().entrySet()) {
1957-
List<Boolean>result =isOptionSensitive(entry.getKey(),entry.getValue());
1958-
if (result.get(0)) {
1959-
returnresult;
1979+
RedactionStatestate =isOptionSensitive(entry.getKey(),entry.getValue());
1980+
if (state.redact) {
1981+
returnstate;
19601982
}
19611983
}
19621984
}
19631985
}else {
19641986
for (Map.Entry<FieldDescriptor,Object>entry :
19651987
((Message)value).getAllFields().entrySet()) {
1966-
List<Boolean>result =isOptionSensitive(entry.getKey(),entry.getValue());
1967-
if (result.get(0)) {
1968-
returnresult;
1988+
RedactionStatestate =isOptionSensitive(entry.getKey(),entry.getValue());
1989+
if (state.redact) {
1990+
returnstate;
19691991
}
19701992
}
19711993
}
19721994
}
1973-
returnArrays.asList(false,false);
1995+
returnRedactionState.of(false);
19741996
}
19751997

1976-
// Lazily calculates if the field is marked as sensitive, and caches results.
1977-
privateList<Boolean>calculateSensitivityData() {
1978-
if (sensitivity ==Sensitivity.UNKNOWN) {
1998+
// Lazily calculates the redact state of the field, caching the result.
1999+
RedactionStategetRedactionState() {
2000+
RedactionStatestate =redactionState;
2001+
if (state ==null) {
19792002
// If the field is directly marked with debug_redact=true, then it is sensitive.
19802003
synchronized (this) {
1981-
if (sensitivity ==Sensitivity.UNKNOWN) {
1982-
booleanisSensitive =proto.getOptions().getDebugRedact();
2004+
state =redactionState;
2005+
if (state ==null) {
2006+
FieldOptionsoptions =getOptions();
2007+
state =RedactionState.of(options.getDebugRedact());
19832008
// Check if the FieldOptions contain any enums that are marked as debug_redact=true,
19842009
// either directly or indirectly via a message option.
19852010
for (Map.Entry<Descriptors.FieldDescriptor,Object>entry :
1986-
proto.getOptions().getAllFields().entrySet()) {
1987-
List<Boolean>result =isOptionSensitive(entry.getKey(),entry.getValue());
1988-
isSensitive =isSensitive ||result.get(0);
1989-
isReportable =result.get(1);
1990-
if (isSensitive) {
2011+
options.getAllFields().entrySet()) {
2012+
state =
2013+
RedactionState.combine(
2014+
state,isOptionSensitive(entry.getKey(),entry.getValue()));
2015+
if (state.redact) {
19912016
break;
19922017
}
19932018
}
1994-
sensitivity =isSensitive ?Sensitivity.SENSITIVE :Sensitivity.NOT_SENSITIVE;
2019+
redactionState =state;
19952020
}
19962021
}
19972022
}
1998-
returnArrays.asList(sensitivity ==Sensitivity.SENSITIVE,isReportable);
1999-
}
2000-
2001-
booleanisSensitive() {
2002-
returncalculateSensitivityData().get(0);
2003-
}
2004-
2005-
booleanisReportable() {
2006-
returncalculateSensitivityData().get(1);
2023+
returnstate;
20072024
}
20082025

20092026
/** See {@link FileDescriptor#resolveAllFeatures}. */

‎java/core/src/main/java/com/google/protobuf/TextFormat.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,8 @@ private void printFieldValue(
665665
// field, b) via an enum field marked with debug_redact=true that is within the proto's
666666
// FieldOptions, either directly or indirectly via a message option.
667667
privatebooleanshouldRedact(finalFieldDescriptorfield,TextGeneratorgenerator) {
668-
returnenablingSafeDebugFormat &&field.isSensitive();
668+
FieldDescriptor.RedactionStatestate =field.getRedactionState();
669+
returnenablingSafeDebugFormat &&state.redact;
669670
}
670671

671672
/** Like {@code print()}, but writes directly to a {@code String} and returns it. */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp