This is an automated email from the ASF dual-hosted git repository.

papegaaij pushed a commit to branch resource-name-iterator-alloc
in repository https://gitbox.apache.org/repos/asf/wicket.git

commit 584f39392a88c8cb9571d3a3d5b71d330e7ed6a2
Author: Emond Papegaaij <[email protected]>
AuthorDate: Mon Sep 7 05:12:36 2026 +0000

    Serialize pages into a buffer that grows by chaining
    
    JavaSerializer wrote pages into a java.io.ByteArrayOutputStream with no 
initial
    size. That starts at 32 bytes and grows by copying everything written so far
    into a buffer of twice the size, so a page of any substance is copied a 
dozen
    times over on its way out, and every one of those buffers is discarded again
    immediately.
    
    Wicket already has a ByteArrayOutputStream that chains a new buffer instead 
of
    copying, which is what this switches to, starting at a size no page worth
    storing comes in under.
    
    PageSerializationBenchmark, per page:
    
      500 components (37,924 bytes)  256,714 -> 214,763 B/op
       50 components ( 5,173 bytes)   33,592 ->  33,000 B/op
    
    The saving grows with the page, as the copying it removes did: at 500
    components it is larger than the serialized page itself. Time is unchanged.
    
    Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
---
 .../java/org/apache/wicket/serialize/java/JavaSerializer.java | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git 
a/wicket-core/src/main/java/org/apache/wicket/serialize/java/JavaSerializer.java
 
b/wicket-core/src/main/java/org/apache/wicket/serialize/java/JavaSerializer.java
index a91a1c4ef2..02876cb3c8 100644
--- 
a/wicket-core/src/main/java/org/apache/wicket/serialize/java/JavaSerializer.java
+++ 
b/wicket-core/src/main/java/org/apache/wicket/serialize/java/JavaSerializer.java
@@ -17,7 +17,6 @@
 package org.apache.wicket.serialize.java;
 
 import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.NotSerializableException;
@@ -40,6 +39,7 @@ import 
org.apache.wicket.core.util.objects.checker.CheckingObjectOutputStream;
 import org.apache.wicket.core.util.objects.checker.ObjectSerializationChecker;
 import org.apache.wicket.serialize.ISerializer;
 import org.apache.wicket.settings.ApplicationSettings;
+import org.apache.wicket.util.io.ByteArrayOutputStream;
 import org.apache.wicket.util.io.IOUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -70,6 +70,9 @@ public class JavaSerializer implements ISerializer
        /**
         * The key of the application which can be used later to find the 
proper {@link IClassResolver}
         */
+       /** No page worth storing serializes into less than this, so it is 
where the buffer starts. */
+       private static final int INITIAL_BUFFER_SIZE = 4096;
+
        private final String applicationKey;
 
        /**
@@ -88,7 +91,11 @@ public class JavaSerializer implements ISerializer
        {
                try
                {
-                       final ByteArrayOutputStream out = new 
ByteArrayOutputStream();
+                       // Wicket's ByteArrayOutputStream chains a new buffer 
when it runs out of room, where
+                       // java.io's copies everything written so far into a 
buffer of twice the size. A page
+                       // of any substance outgrows the initial buffer several 
times over, and each of those
+                       // copies is thrown away again immediately.
+                       final ByteArrayOutputStream out = new 
ByteArrayOutputStream(INITIAL_BUFFER_SIZE);
                        ObjectOutputStream oos = null;
                        try
                        {

Reply via email to