more accurate work with IOUtil.allocReadWriteBuffer:

use IOUtil.write/readUTF that have thread local buffer upon softreference:
- to avoid extra allocations (1G of garbage produced for our codebase indexing)
- possible several threads accessing same buffer problem
This commit is contained in:
Maxim.Mossienko
2014-04-29 18:36:55 +02:00
parent bb2af75b64
commit b02b4c1912
10 changed files with 20 additions and 42 deletions
@@ -64,14 +64,12 @@ public class VirtualFileSetState {
private static class VirtualFileWithDependenciesExternalizer implements DataExternalizer<VirtualFileSetState> {
private byte[] myBuffer = IOUtil.allocReadWriteUTFBuffer();
@Override
public void save(@NotNull DataOutput out, VirtualFileSetState value) throws IOException {
final Map<String, Long> dependencies = value.myTimestamps;
out.writeInt(dependencies.size());
for (Map.Entry<String, Long> entry : dependencies.entrySet()) {
IOUtil.writeUTFFast(myBuffer, out, entry.getKey());
IOUtil.writeUTF(out, entry.getKey());
out.writeLong(entry.getValue());
}
}
@@ -81,7 +79,7 @@ public class VirtualFileSetState {
final VirtualFileSetState state = new VirtualFileSetState();
int size = in.readInt();
while (size-- > 0) {
final String url = IOUtil.readUTFFast(myBuffer, in);
final String url = IOUtil.readUTF(in);
final long timestamp = in.readLong();
state.myTimestamps.put(url, timestamp);
}
@@ -60,15 +60,13 @@ public class VirtualFileWithDependenciesState {
private static class VirtualFileWithDependenciesExternalizer implements DataExternalizer<VirtualFileWithDependenciesState> {
private byte[] myBuffer = IOUtil.allocReadWriteUTFBuffer();
@Override
public void save(@NotNull DataOutput out, VirtualFileWithDependenciesState value) throws IOException {
out.writeLong(value.mySourceTimestamp);
final Map<String, Long> dependencies = value.myDependencies;
out.writeInt(dependencies.size());
for (Map.Entry<String, Long> entry : dependencies.entrySet()) {
IOUtil.writeUTFFast(myBuffer, out, entry.getKey());
IOUtil.writeUTF(out, entry.getKey());
out.writeLong(entry.getValue());
}
}
@@ -78,7 +76,7 @@ public class VirtualFileWithDependenciesState {
final VirtualFileWithDependenciesState state = new VirtualFileWithDependenciesState(in.readLong());
int size = in.readInt();
while (size-- > 0) {
final String url = IOUtil.readUTFFast(myBuffer, in);
final String url = IOUtil.readUTF(in);
final long timestamp = in.readLong();
state.myDependencies.put(url, timestamp);
}
@@ -29,13 +29,11 @@ import java.io.IOException;
* @author nik
*/
public class ArtifactPackagingItemExternalizer implements DataExternalizer<ArtifactPackagingItemOutputState> {
private byte[] myBuffer = IOUtil.allocReadWriteUTFBuffer();
@Override
public void save(@NotNull DataOutput out, ArtifactPackagingItemOutputState value) throws IOException {
out.writeInt(value.myDestinations.size());
for (Pair<String, Long> pair : value.myDestinations) {
IOUtil.writeUTFFast(myBuffer, out, pair.getFirst());
IOUtil.writeUTF(out, pair.getFirst());
out.writeLong(pair.getSecond());
}
}
@@ -45,7 +43,7 @@ public class ArtifactPackagingItemExternalizer implements DataExternalizer<Artif
int size = in.readInt();
SmartList<Pair<String, Long>> destinations = new SmartList<Pair<String, Long>>();
while (size-- > 0) {
String path = IOUtil.readUTFFast(myBuffer, in);
String path = IOUtil.readUTF(in);
long outputTimestamp = in.readLong();
destinations.add(Pair.create(path, outputTimestamp));
}
@@ -30,22 +30,16 @@ import java.util.Collection;
* Date: 29.01.11
*/
public class RW {
private static final byte[] ourStringBuffer = IOUtil.allocReadWriteUTFBuffer();
private RW() {
}
protected static String readUTF(DataInput in) throws IOException {
synchronized (ourStringBuffer) {
return IOUtil.readUTFFast(ourStringBuffer, in);
}
return IOUtil.readUTF(in);
}
protected static void writeUTF(DataOutput out, String value) throws IOException {
synchronized (ourStringBuffer) {
IOUtil.writeUTFFast(ourStringBuffer, out, value);
}
IOUtil.writeUTF(out, value);
}
public interface Savable {
@@ -83,12 +83,10 @@ public class ArtifactOutputToSourceMapping extends AbstractStateStorage<String,
}
private static class SourcePathListExternalizer implements DataExternalizer<List<SourcePathAndRootIndex>> {
private final byte[] myBuffer = IOUtil.allocReadWriteUTFBuffer();
@Override
public void save(@NotNull DataOutput out, List<SourcePathAndRootIndex> value) throws IOException {
for (SourcePathAndRootIndex pair : value) {
IOUtil.writeUTFFast(myBuffer, out, pair.myPath);
IOUtil.writeUTF(out, pair.myPath);
out.writeInt(pair.getRootIndex());
}
}
@@ -98,7 +96,7 @@ public class ArtifactOutputToSourceMapping extends AbstractStateStorage<String,
List<SourcePathAndRootIndex> result = new SmartList<SourcePathAndRootIndex>();
final DataInputStream stream = (DataInputStream)in;
while (stream.available() > 0) {
final String path = IOUtil.readUTFFast(myBuffer, stream);
final String path = IOUtil.readUTF(stream);
final int index = stream.readInt();
result.add(new SourcePathAndRootIndex(path, index));
}
@@ -30,14 +30,12 @@ import java.io.IOException;
* Date: 9/10/12
*/
public final class FileKeyDescriptor implements KeyDescriptor<File> {
private final byte[] buffer = IOUtil.allocReadWriteUTFBuffer();
public void save(@NotNull DataOutput out, File value) throws IOException {
IOUtil.writeUTFFast(buffer, out, value.getPath());
IOUtil.writeUTF(out, value.getPath());
}
public File read(@NotNull DataInput in) throws IOException {
return new File(IOUtil.readUTFFast(buffer, in));
return new File(IOUtil.readUTF(in));
}
public int getHashCode(File value) {
@@ -82,10 +82,9 @@ public class OneToManyPathsMapping extends AbstractStateStorage<String, Collecti
}
private static class PathCollectionExternalizer implements DataExternalizer<Collection<String>> {
private final byte[] myBuffer = IOUtil.allocReadWriteUTFBuffer();
public void save(@NotNull DataOutput out, Collection<String> value) throws IOException {
for (String str : value) {
IOUtil.writeUTFFast(myBuffer, out, str);
IOUtil.writeUTF(out, str);
}
}
@@ -93,7 +92,7 @@ public class OneToManyPathsMapping extends AbstractStateStorage<String, Collecti
final Set<String> result = new THashSet<String>(FileUtil.PATH_HASHING_STRATEGY);
final DataInputStream stream = (DataInputStream)in;
while (stream.available() > 0) {
final String str = IOUtil.readUTFFast(myBuffer, stream);
final String str = IOUtil.readUTF(stream);
result.add(str);
}
return result;
@@ -234,18 +234,17 @@ public class JarHandler extends JarHandlerBase {
try {
info = new PersistentHashMap<String, CacheLibraryInfo>(
file, new EnumeratorStringDescriptor(), new DataExternalizer<CacheLibraryInfo>() {
private final byte[] myBuffer = IOUtil.allocReadWriteUTFBuffer();
@Override
public void save(@NotNull DataOutput out, CacheLibraryInfo value) throws IOException {
IOUtil.writeUTFFast(myBuffer, out, value.mySnapshotPath);
IOUtil.writeUTF(out, value.mySnapshotPath);
out.writeLong(value.myModificationTime);
out.writeLong(value.myFileLength);
}
@Override
public CacheLibraryInfo read(@NotNull DataInput in) throws IOException {
return new CacheLibraryInfo(IOUtil.readUTFFast(myBuffer, in), in.readLong(), in.readLong());
return new CacheLibraryInfo(IOUtil.readUTF(in), in.readLong(), in.readLong());
}
}
);
@@ -38,13 +38,11 @@ public class EnumeratorStringDescriptor implements KeyDescriptor<String> {
@Override
public void save(@NotNull final DataOutput storage, @NotNull final String value) throws IOException {
final byte[] buffer = IOUtil.allocReadWriteUTFBuffer();
IOUtil.writeUTFFast(buffer, storage, value);
IOUtil.writeUTF(storage, value);
}
@Override
public String read(@NotNull final DataInput storage) throws IOException {
final byte[] buffer = IOUtil.allocReadWriteUTFBuffer();
return IOUtil.readUTFFast(buffer, storage);
return IOUtil.readUTF(storage);
}
}
@@ -154,20 +154,18 @@ public class XmlPropertiesIndex extends FileBasedIndexExtension<XmlPropertiesInd
return builder;
}
private final byte[] buffer = IOUtil.allocReadWriteUTFBuffer();
@Override
public void save(@NotNull DataOutput out, Key value) throws IOException {
out.writeBoolean(value.isMarker);
if (value.key != null) {
IOUtil.writeUTFFast(buffer, out, value.key);
IOUtil.writeUTF(out, value.key);
}
}
@Override
public Key read(@NotNull DataInput in) throws IOException {
boolean isMarker = in.readBoolean();
return isMarker ? MARKER_KEY : new Key(IOUtil.readUTFFast(buffer, in));
return isMarker ? MARKER_KEY : new Key(IOUtil.readUTF(in));
}
@Override