Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions src/main/java/graphql/language/AstPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -575,28 +575,28 @@ String wrap(String start, String maybeString, String end) {
}
return "";
}
return start + maybeString + (!isEmpty(end) ? end : "");
return new StringBuilder().append(start).append(maybeString).append(!isEmpty(end) ? end : "").toString();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This actually makes a difference in the benchmarks - not a lot but some

Copy link
Contributor

@jord1e jord1e Feb 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may no longer be true when compiling with JDK 9, just throwing it in
https://openjdk.java.net/jeps/280

I do not know how long you want to stay on JDK 8 though, for now it is an improvement

}

private <T extends Node> String block(List<T> nodes) {
if (isEmpty(nodes)) {
return "{}";
}
if (compactMode) {
return "{"
+ join(nodes, " ")
+ "}";
return new StringBuilder().append("{").append(join(nodes, " ")).append("}").toString();
}
return indent("{\n"
+ join(nodes, "\n"))
return indent(new StringBuilder().append("{\n").append(join(nodes, "\n")))
+ "\n}";
}

private String indent(String maybeString) {
if (isEmpty(maybeString)) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it was never empty - ever

return "";
private StringBuilder indent(StringBuilder maybeString) {
for (int i = 0; i < maybeString.length(); i++) {
char c = maybeString.charAt(i);
if (c == '\n') {
maybeString.replace(i,i+1,"\n ");
i+=3;
}
}
maybeString = maybeString.replaceAll("\\n", "\n ");
return maybeString;
}

Expand All @@ -605,13 +605,14 @@ String wrap(String start, Node maybeNode, String end) {
if (maybeNode == null) {
return "";
}
return start + node(maybeNode) + (isEmpty(end) ? "" : end);
return new StringBuilder().append(start).append(node(maybeNode)).append(isEmpty(end) ? "" : end).toString();
}

/**
* This will pretty print the AST node in graphql language format
*
* @param node the AST node to print
*
* @return the printed node in graphql language format
*/
public static String printAst(Node node) {
Expand All @@ -637,6 +638,7 @@ public static void printAst(Writer writer, Node node) {
* and comments stripped out of the text.
*
* @param node the AST node to print
*
* @return the printed node in a compact graphql language format
*/
public static String printAstCompact(Node node) {
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/benchmark/AstPrinterBenchmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,22 @@ public void benchMarkAstPrinterAvgTime(Blackhole blackhole) {
public static void printAst(Blackhole blackhole) {
blackhole.consume(AstPrinter.printAst(document));
}

@Benchmark
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.SECONDS)
public void benchMarkAstPrinterCompactThroughput(Blackhole blackhole) {
printAstCompact(blackhole);
}

@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public void benchMarkAstPrinterCompactAvgTime(Blackhole blackhole) {
printAstCompact(blackhole);
}

public static void printAstCompact(Blackhole blackhole) {
blackhole.consume(AstPrinter.printAstCompact(document));
}
}