Skip to content

Commit

Permalink
Introduce small writing buffer to improve performance
Browse files Browse the repository at this point in the history
Reduce the amount of writing operations called on the given writer. This
improves writing performance even when writing to a StringWriter. As an
effect, it also speeds up toString().

The default size of 128 chars turned out to be a good compromise. It
hardly affects the performance for smaller documents, but sigificantly
speeds up writing bigger ones.
  • Loading branch information
ralfstx committed Jan 18, 2015
1 parent ae41ab0 commit 1c73a6c
Show file tree
Hide file tree
Showing 3 changed files with 225 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,9 @@ public boolean asBoolean() {
* if an I/O error occurs in the writer
*/
public void writeTo( Writer writer ) throws IOException {
write( new JsonWriter( writer ) );
WritingBuffer buffer = new WritingBuffer( writer, 128 );
write( new JsonWriter( buffer ) );
buffer.flush();
}

/**
Expand All @@ -414,15 +416,14 @@ public void writeTo( Writer writer ) throws IOException {
*/
@Override
public String toString() {
StringWriter stringWriter = new StringWriter();
JsonWriter jsonWriter = new JsonWriter( stringWriter );
StringWriter writer = new StringWriter();
try {
write( jsonWriter );
writeTo( writer );
} catch( IOException exception ) {
// StringWriter does not throw IOExceptions
throw new RuntimeException( exception );
}
return stringWriter.toString();
return writer.toString();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*******************************************************************************
* Copyright (c) 2015 EclipseSource.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package com.eclipsesource.json;

import java.io.IOException;
import java.io.Writer;


/**
* A lightweight writing buffer to reduce the amount of write operations to be performed on the
* underlying writer. This implementation is not thread-safe. It deliberately deviates from the
* contract of Writer. In particular, it does not flush or close the wrapped writer nor does it
* ensure that the wrapped writer is open.
*/
class WritingBuffer extends Writer {

private final Writer writer;
private final char[] buffer;
private int fill = 0;

WritingBuffer( Writer writer ) {
this( writer, 16 );
}

WritingBuffer( Writer writer, int bufferSize ) {
this.writer = writer;
buffer = new char[bufferSize];
}

@Override
public void write( int c ) throws IOException {
if( fill > buffer.length - 1 ) {
flush();
}
buffer[fill++] = (char)c;
}

@Override
public void write( char[] cbuf, int off, int len ) throws IOException {
if( fill > buffer.length - len ) {
flush();
if( len > buffer.length ) {
writer.write( cbuf, off, len );
return;
}
}
System.arraycopy( cbuf, off, buffer, fill, len );
fill += len;
}

@Override
public void write( String str, int off, int len ) throws IOException {
if( fill > buffer.length - len ) {
flush();
if( len > buffer.length ) {
writer.write( str, off, len );
return;
}
}
str.getChars( off, off + len, buffer, fill );
fill += len;
}

/**
* Flushes the internal buffer but does not flush the wrapped writer.
*/
@Override
public void flush() throws IOException {
writer.write( buffer, 0, fill );
fill = 0;
}

/**
* Does not close or flush the wrapped writer.
*/
@Override
public void close() throws IOException {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package com.eclipsesource.json;

import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Arrays;

import org.junit.Before;
import org.junit.Test;


public class WritingBuffer_Test {

private static final int BUFFER_SIZE = 16;
private StringWriter wrapped;
private WritingBuffer writer;

@Before
public void setUp() {
wrapped = new StringWriter();
writer = new WritingBuffer( wrapped, BUFFER_SIZE );
}

@Test
public void testWriteChar() throws IOException {
writer.write( 'x' );
writer.flush();

assertEquals( "x", wrapped.toString() );
}

@Test
public void testWriteChar_exceeding() throws IOException {
writer.write( createString( BUFFER_SIZE ) );
writer.write( 'x' );
writer.flush();

assertEquals( createString( BUFFER_SIZE + 1 ), wrapped.toString() );
}

@Test
public void testWriteCharArray() throws IOException {
writer.write( "foobar".toCharArray(), 1, 3 );
writer.flush();

assertEquals( "oob", wrapped.toString() );
}

@Test
public void testWriteCharArray_fit() throws IOException {
writer.write( createString( BUFFER_SIZE - 3 ) );
writer.write( "foobar".toCharArray(), 1, 3 );
writer.flush();

assertEquals( createString( BUFFER_SIZE - 3 ) + "oob", wrapped.toString() );
}

@Test
public void testWriteCharArray_exceeding() throws IOException {
writer.write( createString( BUFFER_SIZE - 2 ) );
writer.write( "foobar".toCharArray(), 1, 3 );
writer.flush();

assertEquals( createString( BUFFER_SIZE - 2 ) + "oob", wrapped.toString() );
}

@Test
public void testWriteCharArray_exceedingBuffer() throws IOException {
writer.write( createChars( BUFFER_SIZE + 1 ) );
writer.flush();

assertEquals( createString( BUFFER_SIZE + 1 ), wrapped.toString() );
}

@Test
public void testWriteString() throws IOException {
writer.write( "foobar", 1, 3 );
writer.flush();

assertEquals( "oob", wrapped.toString() );
}

@Test
public void testWriteString_fit() throws IOException {
writer.write( createString( BUFFER_SIZE - 3 ) );
writer.write( "foobar", 1, 3 );
writer.flush();

assertEquals( createString( BUFFER_SIZE - 3 ) + "oob", wrapped.toString() );
}

@Test
public void testWriteString_exceeding() throws IOException {
writer.write( createString( BUFFER_SIZE - 2 ) );
writer.write( "foobar", 1, 3 );
writer.flush();

assertEquals( createString( BUFFER_SIZE - 2 ) + "oob", wrapped.toString() );
}

@Test
public void testWriteString_exceedingBuffer() throws IOException {
writer.write( createString( BUFFER_SIZE + 1 ) );
writer.flush();

assertEquals( createString( BUFFER_SIZE + 1 ), wrapped.toString() );
}

private static String createString( int length ) {
return new String( createChars( length ) );
}

private static char[] createChars( int length ) {
char[] array = new char[length];
Arrays.fill( array, 'x' );
return array;
}

}

0 comments on commit 1c73a6c

Please sign in to comment.