Skip to content

Commit 9f2cd68

Browse files
cushongoogle-java-format Team
authored andcommitted
Make google-java-format friendlier to TSAN
This code does deliberate racy initialization of some memoized values, and there is a static final instance of the `Space` subclass that ends up being shared across multiple threads. Tested: sponge/1777b644-2dd8-420b-ad06-b4f17c893d8f PiperOrigin-RevId: 563181626
1 parent 28b199c commit 9f2cd68

File tree

1 file changed

+8
-21
lines changed
  • core/src/main/java/com/google/googlejavaformat

1 file changed

+8
-21
lines changed

core/src/main/java/com/google/googlejavaformat/Doc.java

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@
1919
import static java.lang.Math.max;
2020

2121
import com.google.common.base.MoreObjects;
22+
import com.google.common.base.Suppliers;
2223
import com.google.common.collect.DiscreteDomain;
2324
import com.google.common.collect.Iterators;
2425
import com.google.common.collect.Range;
2526
import com.google.googlejavaformat.Output.BreakTag;
2627
import java.util.ArrayList;
2728
import java.util.List;
2829
import java.util.Optional;
30+
import java.util.function.Supplier;
2931

3032
/**
3133
* {@link com.google.googlejavaformat.java.JavaInputAstVisitor JavaInputAstVisitor} outputs a
@@ -102,28 +104,21 @@ public String toString() {
102104
private static final DiscreteDomain<Integer> INTEGERS = DiscreteDomain.integers();
103105

104106
// Memoized width; Float.POSITIVE_INFINITY if contains forced breaks.
105-
private boolean widthComputed = false;
106-
private float width = 0.0F;
107+
private final Supplier<Float> width = Suppliers.memoize(this::computeWidth);
107108

108109
// Memoized flat; not defined (and never computed) if contains forced breaks.
109-
private boolean flatComputed = false;
110-
private String flat = "";
110+
private final Supplier<String> flat = Suppliers.memoize(this::computeFlat);
111111

112112
// Memoized Range.
113-
private boolean rangeComputed = false;
114-
private Range<Integer> range = EMPTY_RANGE;
113+
private final Supplier<Range<Integer>> range = Suppliers.memoize(this::computeRange);
115114

116115
/**
117116
* Return the width of a {@code Doc}, or {@code Float.POSITIVE_INFINITY} if it must be broken.
118117
*
119118
* @return the width
120119
*/
121120
final float getWidth() {
122-
if (!widthComputed) {
123-
width = computeWidth();
124-
widthComputed = true;
125-
}
126-
return width;
121+
return width.get();
127122
}
128123

129124
/**
@@ -133,11 +128,7 @@ final float getWidth() {
133128
* @return the flat-string value
134129
*/
135130
final String getFlat() {
136-
if (!flatComputed) {
137-
flat = computeFlat();
138-
flatComputed = true;
139-
}
140-
return flat;
131+
return flat.get();
141132
}
142133

143134
/**
@@ -146,11 +137,7 @@ final String getFlat() {
146137
* @return the {@code Doc}'s {@link Range}
147138
*/
148139
final Range<Integer> range() {
149-
if (!rangeComputed) {
150-
range = computeRange();
151-
rangeComputed = true;
152-
}
153-
return range;
140+
return range.get();
154141
}
155142

156143
/**

0 commit comments

Comments
 (0)