Skip to content

Commit

Permalink
⚡ Avoid String.trim() in Minify().minify() to optimize memory usage
Browse files Browse the repository at this point in the history
  • Loading branch information
ujibang committed Sep 13, 2021
1 parent 41bc1f4 commit 7e07395
Showing 1 changed file with 23 additions and 22 deletions.
45 changes: 23 additions & 22 deletions commons/src/main/java/org/restheart/utils/Minify.java
Expand Up @@ -100,19 +100,16 @@ public Minify() {
* string
*/
public String minify(String json) {
InputStream ins = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8));
ByteArrayOutputStream bout = new ByteArrayOutputStream();
var ins = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8));
var bout = new ByteArrayOutputStream();

try {
minify(ins, bout);
} catch (IOException
| UnterminatedCommentException
| UnterminatedRegExpLiteralException
| UnterminatedStringLiteralException e) {
} catch (Exception e) {
return null;
}

return bout.toString().trim();
return bout.toString();
}

/**
Expand All @@ -134,15 +131,14 @@ public String minify(String json) {
* @throws UnterminatedCommentException
* @throws UnterminatedStringLiteralException
*/
public void minify(InputStream in, OutputStream out) throws IOException, UnterminatedRegExpLiteralException,
UnterminatedCommentException,
UnterminatedStringLiteralException {
public void minify(InputStream in, OutputStream out) throws IOException, UnterminatedRegExpLiteralException, UnterminatedCommentException, UnterminatedStringLiteralException {

// Initialize
this.in = new PushbackInputStream(in);
this.out = out;
this.line = 0;
this.column = 0;
var onFirstChar = true;
currChar = '\n';
action(Action.DELETE_NEXT);

Expand All @@ -165,16 +161,27 @@ public void minify(InputStream in, OutputStream out) throws IOException, Untermi
case '(':
case '+':
case '-':
action(Action.OUTPUT_CURR);
if (!onFirstChar) {
action(Action.OUTPUT_CURR);
} else {
action(Action.DELETE_CURR);
onFirstChar = false;
}
break;
case ' ':
action(Action.DELETE_NEXT);
break;
default:
if (isAlphanum(nextChar)) {
action(Action.OUTPUT_CURR);
if (!onFirstChar) {
action(Action.OUTPUT_CURR);
} else {
action(Action.DELETE_CURR);
onFirstChar = false;
}
} else {
action(Action.DELETE_CURR);
onFirstChar = false;
}
}
break;
Expand Down Expand Up @@ -236,12 +243,9 @@ public void minify(InputStream in, OutputStream out) throws IOException, Untermi
* @throws UnterminatedCommentException
* @throws UnterminatedStringLiteralException
*/
private void action(Action action) throws IOException, UnterminatedRegExpLiteralException, UnterminatedCommentException,
UnterminatedStringLiteralException {

private void action(Action action) throws IOException, UnterminatedRegExpLiteralException, UnterminatedCommentException, UnterminatedStringLiteralException {
// Process action
switch (action) {

case OUTPUT_CURR:
out.write(currChar);

Expand All @@ -256,8 +260,7 @@ private void action(Action action) throws IOException, UnterminatedRegExpLiteral
break;
}
if (currChar <= '\n') {
throw new UnterminatedStringLiteralException(line,
column);
throw new UnterminatedStringLiteralException(line, column);
}
if (currChar == '\\') {
out.write(currChar);
Expand All @@ -268,8 +271,7 @@ private void action(Action action) throws IOException, UnterminatedRegExpLiteral

case DELETE_NEXT:
nextChar = next();
if (nextChar == '/'
&& (currChar == '(' || currChar == ',' || currChar == '=' || currChar == ':')) {
if (nextChar == '/' && (currChar == '(' || currChar == ',' || currChar == '=' || currChar == ':')) {
out.write(currChar);
out.write(nextChar);
for (;;) {
Expand All @@ -280,8 +282,7 @@ private void action(Action action) throws IOException, UnterminatedRegExpLiteral
out.write(currChar);
currChar = get();
} else if (currChar <= '\n') {
throw new UnterminatedRegExpLiteralException(line,
column);
throw new UnterminatedRegExpLiteralException(line, column);
}
out.write(currChar);
}
Expand Down

0 comments on commit 7e07395

Please sign in to comment.