Skip to content

Commit 771ba66

Browse files
committed
perf: don't truncate separately over the file channel when writing, just specify the TRUNCATE_EXISTING OpenOption. It is much faster
Signed-off-by: Caleb Hulbert <cmhulbert@gmail.com>
1 parent 15438f2 commit 771ba66

2 files changed

Lines changed: 1 addition & 12 deletions

File tree

src/main/java/org/janelia/saalfeldlab/n5/FsIoPolicy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ static FileChannel openFileChannel(final Path path, final boolean forWriting) th
4545
if (parent != null) {
4646
Files.createDirectories(parent);
4747
}
48-
return FileChannel.open(path, StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
48+
return FileChannel.open(path, StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
4949
} else {
5050
return FileChannel.open(path, StandardOpenOption.READ);
5151
}

src/main/java/org/janelia/saalfeldlab/n5/LockedFileChannel.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,12 @@ public InputStream newInputStream() throws N5Exception.N5IOException {
4545
@Override
4646
public Writer newWriter() throws N5Exception.N5IOException {
4747

48-
truncateChannel();
4948
return Channels.newWriter(channel, StandardCharsets.UTF_8.name());
5049
}
5150

5251
@Override
5352
public OutputStream newOutputStream() throws N5Exception.N5IOException {
5453

55-
truncateChannel();
5654
return Channels.newOutputStream(channel);
5755
}
5856

@@ -62,15 +60,6 @@ public FileChannel getFileChannel() {
6260
return channel;
6361
}
6462

65-
private void truncateChannel() throws N5Exception.N5IOException {
66-
67-
try {
68-
channel.truncate(0);
69-
} catch (final IOException e) {
70-
throw new N5Exception.N5IOException("Failed to truncate channel", e);
71-
}
72-
}
73-
7463
@Override
7564
public void close() throws IOException {
7665

0 commit comments

Comments
 (0)