Skip to content

Avoid extra byte copy inside FileSystemImpl.readFileInternal#5730

Merged
tsegismont merged 2 commits into
eclipse-vertx:masterfrom
tsegismont:issue/5665
Jun 19, 2026
Merged

Avoid extra byte copy inside FileSystemImpl.readFileInternal#5730
tsegismont merged 2 commits into
eclipse-vertx:masterfrom
tsegismont:issue/5665

Conversation

@tsegismont

@tsegismont tsegismont commented Sep 23, 2025

Copy link
Copy Markdown
Member

See #5665

The improvement consists in creating the buffer upfront and using Netty API to read the file channel.

@tsegismont tsegismont added this to the 5.1.0 milestone Sep 23, 2025
@tsegismont
tsegismont requested a review from vietj September 23, 2025 16:51
@tsegismont

Copy link
Copy Markdown
Member Author

@franz1981 please take a look

@tsegismont

Copy link
Copy Markdown
Member Author

@vietj PTAL

I added a test with JMH. Here are the results on my machine.

Latency

File size fileChannel (ns/op) readAllBytes (ns/op) Speedup
128 B 1,384 +/- 25 1,554 +/- 23 ~11%
4 KB 1,887 +/- 20 2,495 +/- 25 ~24%
64 KB 10,911 +/- 136 18,657 +/- 455 ~42%
1 MB 220,739 +/- 8,878 420,946 +/- 38,985 ~48%

The advantage grows with file size. At 1 MB, fileChannel is nearly 2x faster.

Allocations per operation

File size fileChannel (B/op) readAllBytes (B/op) Saved
128 B 784 837 ~6%
4 KB 4,752 8,776 ~46%
64 KB 66,192 131,656 ~50%
1 MB 1,049,238 2,097,728 ~50%

From 4 KB up, readAllBytes allocates almost exactly double: the intermediate byte[] plus the ByteBuf copy. fileChannel eliminates the byte[] entirely.

At 128 B the gap is smaller, probably because of the fixed overhead (FileChannel/Path objects, ByteBuf header) dominates over the data copy.

@tsegismont tsegismont modified the milestones: 5.1.3, 5.2.0 Jun 19, 2026
Closes eclipse-vertx#5677

The improvement consists in creating the buffer upfront and using Netty API to read the file channel.

Signed-off-by: Thomas Segismont <tsegismont@gmail.com>
On my machine, the advantage grows with file size. At 1MB, using the Netty FileChannel method is nearly 2x faster and allocates half as much as the Files.readAllBytes approach.

Signed-off-by: Thomas Segismont <tsegismont@gmail.com>
@tsegismont
tsegismont merged commit 0325932 into eclipse-vertx:master Jun 19, 2026
8 checks passed
@tsegismont
tsegismont deleted the issue/5665 branch June 19, 2026 13:17
tsegismont added a commit that referenced this pull request Jun 19, 2026
…6213)

* Avoid extra byte copy inside FileSystemImpl.readFileInternal

Closes #5677

The improvement consists in creating the buffer upfront and using Netty API to read the file channel.



* Added ReadFileBenchmark

On my machine, the advantage grows with file size. At 1MB, using the Netty FileChannel method is nearly 2x faster and allocates half as much as the Files.readAllBytes approach.



---------

Signed-off-by: Thomas Segismont <tsegismont@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants