-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce small writing buffer to improve performance
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
Showing
3 changed files
with
225 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
com.eclipsesource.json/src/main/java/com/eclipsesource/json/WritingBuffer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} | ||
|
||
} |
120 changes: 120 additions & 0 deletions
120
com.eclipsesource.json/src/test/java/com/eclipsesource/json/WritingBuffer_Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |