Skip to content

Avoid copying bytes from immutable byte arrays on reads #809

@artemananiev

Description

@artemananiev

Check the following comment in RandomAccessSequenceAdapter.java:

        // Always create a copy because Bytes.getBytes() returns a view
        // and the RandomAccessSequenceAdapter.readBytes(int) method is unaware of
        // the underlying buffer ownership, but users of Bytes assume it's immutable.
        // We could make this conditional on `delegate instanceof Bytes` if we have to.
        bytes = bytes.replicate();

This is protection from the caller to hold a reference on the input and modify the data later, which would change the parsed Bytes objects. As the comment suggests, this protection can be skipped, if the input is immutable, for example, when delegate is an instance of Bytes. This would avoid allocating some bytes, which can be very noticeable in heavy reading / parsing apps.

Metadata

Metadata

Labels

PerformanceIssues related to performance concerns.

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions