Skip to content

Commit

Permalink
Merge pull request #1 from eyalkoren/stacktrace-remove-more-allocations
Browse files Browse the repository at this point in the history
Reduce stacktrace serialization allocations further
  • Loading branch information
HaloFour authored Apr 26, 2022
2 parents 108e468 + 2469753 commit 921f48b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -279,34 +279,30 @@ private static void formatStackTraceAsArray(StringBuilder builder, CharSequence
continue;
}

// append line
CharSequence match = stackTrace.subSequence(index, matcher.start());
builder.append("\t\"");
JsonUtils.quoteAsString(match, builder);
builder.append("\",");
// append non-last line
appendStackTraceLine(builder, stackTrace, index, start);
builder.append(',');
builder.append(NEW_LINE);
index = end;
} while (matcher.find());

int length = stackTrace.length();
if (index < length) {
// append remaining line
CharSequence remaining = stackTrace.subSequence(index, length);
builder.append("\t\"");
JsonUtils.quoteAsString(remaining, builder);
builder.append("\"");
appendStackTraceLine(builder, stackTrace, index, length);
}

removeIfEndsWith(builder, NEW_LINE);
removeIfEndsWith(builder, ",");
} else {
// no newlines found, add entire stack trace as single element
builder.append("\t\"");
JsonUtils.quoteAsString(stackTrace, builder);
builder.append("\"");
appendStackTraceLine(builder, stackTrace, 0, stackTrace.length());
}
}

private static void appendStackTraceLine(StringBuilder builder, CharSequence stackTrace, int start, int end) {
builder.append("\t\"");
JsonUtils.quoteAsString(stackTrace, start, end, builder);
builder.append("\"");
}

public static void removeIfEndsWith(StringBuilder sb, String ending) {
if (endsWith(sb, ending)) {
sb.setLength(sb.length() - ending.length());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,21 @@ public final class JsonUtils {
}

public static void quoteAsString(CharSequence content, StringBuilder sb) {
if (content == null) {
sb.append("null");
return;
}
quoteAsString(content, 0, content.length(), sb);
}

public static void quoteAsString(CharSequence content, int start, int end, StringBuilder sb) {
if (content == null) {
sb.append("null");
return;
}
final int[] escCodes = sOutputEscapes128;
final int escLen = escCodes.length;
for (int i = 0, len = content.length(); i < len; ++i) {
for (int i = start; i < end; ++i) {
char c = content.charAt(i);
if (c >= escLen || escCodes[c] == 0) {
sb.append(c);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,31 @@ void serializeExceptionWithStackTraceAsArray() throws JsonProcessingException {
jsonBuilder.append('}');

JsonNode jsonNode = objectMapper.readTree(jsonBuilder.toString());
System.out.println(jsonNode.toPrettyString());
assertThat(jsonNode.get(ERROR_TYPE).textValue()).isEqualTo("className");
assertThat(jsonNode.get(ERROR_STACK_TRACE).isArray()).isTrue();
assertThat(jsonNode.get(ERROR_STACK_TRACE).size()).isEqualTo(2);
assertThat(jsonNode.get(ERROR_STACK_TRACE).get(0).textValue()).isEqualTo("stacktrace");
assertThat(jsonNode.get(ERROR_STACK_TRACE).get(1).textValue()).isEqualTo("caused by error");
assertThat(jsonNode.get(ERROR_MESSAGE).textValue()).isEqualTo("message");
}

@Test
void serializeExceptionWithSingleLineStackTraceAsArray() throws JsonProcessingException {
StringBuilder jsonBuilder = new StringBuilder();
jsonBuilder.append('{');
EcsJsonSerializer.serializeException(jsonBuilder, "className", "message", "caused by error", true);
jsonBuilder.append('}');
System.out.println(jsonBuilder);
JsonNode jsonNode = objectMapper.readTree(jsonBuilder.toString());
System.out.println(jsonNode.toPrettyString());
assertThat(jsonNode.get(ERROR_TYPE).textValue()).isEqualTo("className");
assertThat(jsonNode.get(ERROR_STACK_TRACE).isArray()).isTrue();
assertThat(jsonNode.get(ERROR_STACK_TRACE).size()).isEqualTo(1);
assertThat(jsonNode.get(ERROR_STACK_TRACE).get(0).textValue()).isEqualTo("caused by error");
assertThat(jsonNode.get(ERROR_MESSAGE).textValue()).isEqualTo("message");
}

@Test
void serializeExceptionWithNullMessage() throws JsonProcessingException {
StringBuilder jsonBuilder = new StringBuilder();
Expand Down

0 comments on commit 921f48b

Please sign in to comment.