Skip to content

Commit 69bfa72

Browse files
fmeumcopybara-github
authored andcommitted
Avoid double encoding in PersistentStringIndexer
Strings using Bazel's internal encoding are already raw UTF-8 byte arrays. Closes bazelbuild#27590. PiperOrigin-RevId: 829517859 Change-Id: I558419f0a74c640c5d82e8b58c29943af226c7b3
1 parent 496e566 commit 69bfa72

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/main/java/com/google/devtools/build/lib/actions/cache/PersistentStringIndexer.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
package com.google.devtools.build.lib.actions.cache;
1515

1616
import static java.lang.Math.max;
17-
import static java.nio.charset.StandardCharsets.UTF_8;
1817

1918
import com.google.devtools.build.lib.clock.Clock;
2019
import com.google.devtools.build.lib.concurrent.ThreadSafety.ConditionallyThreadSafe;
20+
import com.google.devtools.build.lib.unsafe.StringUnsafe;
2121
import com.google.devtools.build.lib.util.MapCodec;
2222
import com.google.devtools.build.lib.util.PersistentMap;
2323
import com.google.devtools.build.lib.util.StringIndexer;
@@ -183,7 +183,7 @@ public void dump(PrintStream out) {
183183
}
184184

185185
private static final MapCodec<String, Integer> CODEC =
186-
new MapCodec<String, Integer>() {
186+
new MapCodec<>() {
187187
@Override
188188
protected String readKey(DataInput in) throws IOException {
189189
int length = in.readInt();
@@ -192,7 +192,7 @@ protected String readKey(DataInput in) throws IOException {
192192
}
193193
byte[] content = new byte[length];
194194
in.readFully(content);
195-
return new String(content, UTF_8);
195+
return StringUnsafe.newInstance(content, StringUnsafe.LATIN1);
196196
}
197197

198198
@Override
@@ -202,7 +202,7 @@ protected Integer readValue(DataInput in) throws IOException {
202202

203203
@Override
204204
protected void writeKey(String key, DataOutput out) throws IOException {
205-
byte[] content = key.getBytes(UTF_8);
205+
byte[] content = StringUnsafe.getInternalStringBytes(key);
206206
out.writeInt(content.length);
207207
out.write(content);
208208
}
@@ -218,7 +218,7 @@ protected void writeValue(Integer value, DataOutput out) throws IOException {
218218
* metadata cache.
219219
*/
220220
private static final class PersistentIndexMap extends PersistentMap<String, Integer> {
221-
private static final int VERSION = 0x01;
221+
private static final int VERSION = 0x02;
222222
private static final long SAVE_INTERVAL_NS = 3L * 1000 * 1000 * 1000;
223223

224224
private final Clock clock;

0 commit comments

Comments
 (0)