Skip to content

#220 do *not* use Arrays.copyOf(), performance seems to be inferior - #224

Merged
cowtowncoder merged 4 commits into
FasterXML:masterfrom
winfriedgerlach:feature/#220-do-not-use-Arrays.copyOf
May 28, 2025
Merged

cowtowncoder merged 4 commits into
FasterXML:masterfrom
winfriedgerlach:feature/#220-do-not-use-Arrays.copyOf

Conversation

@winfriedgerlach

Copy link
Copy Markdown
Contributor

No description provided.

@cowtowncoder

Copy link
Copy Markdown
Member

@winfriedgerlach Ok no real change wrt Arrays.copyOf(), but curious as to if you have something that points to such performance difference? Or did you do micro-benchmark?
(would have like used it mostly for bit more compact code)

@winfriedgerlach

Copy link
Copy Markdown
Contributor Author

@cowtowncoder Very good question that I don't have the perfect answer for.

Very old comments (Joshua Bloch) say .clone() is the fastest way to clone an array. This is already challenged in some of the answers to that question. Most published benchmarks of "medium age" (e.g. some years ago) find that System.arrayCopy() is fastest.

Things seem to have changed quite a lot in the JDK recently (~beginning of 2023), so different Java versions may perform considerably differently:

Other quite new (2024) benchmarks find no significant difference between Arrays.copy() and System.arrayCopy() (note the absence of the JVM version under test...):
https://www.baeldung.com/java-system-arraycopy-arrays-copyof-performance

My own micro (and a little less micro benchmarks) showed System.arrayCopy() ahead most of the times, sometimes on same level as clone()/Arrays.copyOf().

As Jackson currently supports every Java version from Java 8 to 23, I recon it is safer to stick with System.arrayCopy() for now when optimizing for performance, even if there is probably almost no performance difference when using the most current (=2024) Java versions.

I definitely have to agree that the code is nicer with Arrays.copyOf()though, see cfe59fb

@cowtowncoder
cowtowncoder merged commit 7b66a2b into FasterXML:master May 28, 2025
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