Skip to content

Commit

Permalink
enhance memory usage using buffer, or avoiding alloc
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud-Nauwynck authored and hboutemy committed Jun 28, 2019
1 parent 2f866c4 commit adb9e24
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion jansi/src/main/java/org/fusesource/jansi/Ansi.java
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ public Ansi reset() {
private final ArrayList<Integer> attributeOptions = new ArrayList<Integer>(5);

public Ansi() {
this(new StringBuilder());
this(new StringBuilder(80));
}

public Ansi(Ansi parent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public synchronized void write(int data) throws IOException { // expected diff w
buffer[pos++] = (byte) data;
if (!('0' <= data && data <= '9')) {
String strValue = new String(buffer, startOfValue, (pos - 1) - startOfValue, Charset.defaultCharset());
Integer value = new Integer(strValue);
Integer value = Integer.valueOf(strValue);
options.add(value);
if (data == ';') {
state = LOOKING_FOR_NEXT_ARG;
Expand Down Expand Up @@ -171,7 +171,7 @@ public synchronized void write(int data) throws IOException { // expected diff w
buffer[pos++] = (byte) data;
if (';' == data) {
String strValue = new String(buffer, startOfValue, (pos - 1) - startOfValue, Charset.defaultCharset());
Integer value = new Integer(strValue);
Integer value = Integer.valueOf(strValue);
options.add(value);
startOfValue = pos;
state = LOOKING_FOR_OSC_PARAM;
Expand Down
4 changes: 2 additions & 2 deletions jansi/src/main/java/org/fusesource/jansi/AnsiPrintStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ protected synchronized boolean filter(int data) { // expected diff with AnsiOutp
buffer[pos++] = (byte) data;
if (!('0' <= data && data <= '9')) {
String strValue = new String(buffer, startOfValue, (pos - 1) - startOfValue, Charset.defaultCharset());
Integer value = new Integer(strValue);
Integer value = Integer.valueOf(strValue);
options.add(value);
if (data == ';') {
state = LOOKING_FOR_NEXT_ARG;
Expand Down Expand Up @@ -163,7 +163,7 @@ protected synchronized boolean filter(int data) { // expected diff with AnsiOutp
buffer[pos++] = (byte) data;
if (';' == data) {
String strValue = new String(buffer, startOfValue, (pos - 1) - startOfValue, Charset.defaultCharset());
Integer value = new Integer(strValue);
Integer value = Integer.valueOf(strValue);
options.add(value);
startOfValue = pos;
state = LOOKING_FOR_OSC_PARAM;
Expand Down
2 changes: 1 addition & 1 deletion jansi/src/main/java/org/fusesource/jansi/AnsiRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class AnsiRenderer {

public static String render(final String input) throws IllegalArgumentException {
try {
return render(input, new StringBuilder()).toString();
return render(input, new StringBuilder(input.length())).toString();
} catch (IOException e) {
// Cannot happen because StringBuilder does not throw IOException
throw new IllegalArgumentException(e);
Expand Down
2 changes: 1 addition & 1 deletion jansi/src/main/java/org/fusesource/jansi/AnsiString.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public AnsiString(final CharSequence str) {
private CharSequence chew(final CharSequence str) {
assert str != null;

ByteArrayOutputStream buff = new ByteArrayOutputStream();
ByteArrayOutputStream buff = new ByteArrayOutputStream(str.length());
AnsiOutputStream out = new AnsiOutputStream(buff);

try {
Expand Down
14 changes: 12 additions & 2 deletions jansi/src/main/java/org/fusesource/jansi/FilterPrintStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@
public class FilterPrintStream extends PrintStream
{
private static final String NEWLINE = System.getProperty("line.separator");

private static final int TMP_STR_TO_CHAR_BUFFER_LENGTH = 400;

protected final PrintStream ps;

private final char[] strToCharBuffer = new char[TMP_STR_TO_CHAR_BUFFER_LENGTH];

public FilterPrintStream(PrintStream ps)
{
super( new OutputStream() {
Expand Down Expand Up @@ -103,7 +108,7 @@ private void write(char buf[]) {
}

private void write(String s) {
char[] buf = new char[s.length()];
char[] buf = (s.length() < strToCharBuffer.length)? strToCharBuffer : new char[s.length()];
s.getChars(0, s.length(), buf, 0);
write(buf);
}
Expand All @@ -121,7 +126,12 @@ public void print(boolean b) {

@Override
public void print(char c) {
write(String.valueOf(c));
// optim for: write(String.valueOf(c));
if (filter(c))
{
ps.print(c);
}

}

@Override
Expand Down

0 comments on commit adb9e24

Please sign in to comment.