Skip to content

Commit

Permalink
Merge pull request #81 from slandelle/master
Browse files Browse the repository at this point in the history
Use StringBuilder instead of StringBuffer when no concurrent access
  • Loading branch information
mattbishop committed Sep 30, 2014
2 parents c99a0ac + 5f2cf53 commit b3c3735
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 24 deletions.
2 changes: 1 addition & 1 deletion slf4j-api/src/main/java/org/slf4j/helpers/BasicMarker.java
Expand Up @@ -180,7 +180,7 @@ public String toString() {
}
Iterator<Marker> it = this.iterator();
Marker reference;
StringBuffer sb = new StringBuffer(this.getName());
StringBuilder sb = new StringBuilder(this.getName());
sb.append(' ').append(OPEN);
while (it.hasNext()) {
reference = (Marker) it.next();
Expand Down
Expand Up @@ -109,7 +109,7 @@ public static String getSignature(CtBehavior method)

String methodName = method.getName();

StringBuffer sb = new StringBuffer(methodName + "(\" ");
StringBuilder sb = new StringBuilder(methodName).append("(\" ");
for (int i = 0; i < parameterTypes.length; i++) {
if (i > 0) {
// add a comma and a space between printed values
Expand All @@ -129,16 +129,16 @@ public static String getSignature(CtBehavior method)
try {
sb.append(parameterNameFor(method, locals, i));
} catch (Exception e) {
sb.append("" + (i + 1));
sb.append(i + 1);
}
sb.append("\" + \"=");

if (parameterType.isPrimitive()) {
// let the compiler handle primitive -> string
sb.append("\"+ $" + (i + 1));
sb.append("\"+ $").append(i + 1);
} else {
String s = "org.slf4j.instrumentation.ToStringHelper.render";
sb.append("\"+ " + s + "($" + (i + 1) + ")");
sb.append("\"+ ").append(s).append("($").append(i + 1).append(')');
}
}
sb.append("+\")");
Expand Down
Expand Up @@ -32,19 +32,19 @@ public class ToStringHelper {
/**
* Prefix to use at the start of the representation. Always used.
*/
private static final String ARRAY_PREFIX = "[";
private static final char ARRAY_PREFIX = '[';

/**
* Suffix to use at the end of the representation. Always used.
*/
private static final String ARRAY_SUFFIX = "]";
private static final char ARRAY_SUFFIX = ']';

/**
* String separating each element when rendering an array. To be compatible
* with lists comma-space is used.
*/

private static final String ELEMENT_SEPARATOR = ", ";
private static final char[] ELEMENT_SEPARATOR = ", ".toCharArray();

/**
* unrenderableClasses is essentially a Set of Class objects which has for
Expand Down Expand Up @@ -106,9 +106,9 @@ public static String render(Object o) {
* @param objectClass
* @return
*/
private static StringBuffer renderArray(Object o, Class<?> objectClass) {
private static StringBuilder renderArray(Object o, Class<?> objectClass) {
Class<?> componentType = objectClass.getComponentType();
StringBuffer sb = new StringBuffer(ARRAY_PREFIX);
StringBuilder sb = new StringBuilder(ARRAY_PREFIX);

if (componentType.isPrimitive() == false) {
Object[] oa = (Object[]) o;
Expand Down
4 changes: 2 additions & 2 deletions slf4j-ext/src/main/java/org/slf4j/profiler/Profiler.java
Expand Up @@ -234,7 +234,7 @@ public StopWatch getCopyOfGlobalStopWatch() {

private String buildProfilerString(DurationUnit du, String firstPrefix,
String label, String indentation) {
StringBuffer buf = new StringBuffer();
StringBuilder buf = new StringBuilder();

buf.append(firstPrefix);
buf.append(" Profiler [");
Expand All @@ -259,7 +259,7 @@ private String buildProfilerString(DurationUnit du, String firstPrefix,
return buf.toString();
}

private static void buildStopWatchString(StringBuffer buf, DurationUnit du,
private static void buildStopWatchString(StringBuilder buf, DurationUnit du,
String prefix, String indentation, StopWatch sw) {

buf.append(indentation);
Expand Down
50 changes: 49 additions & 1 deletion slf4j-ext/src/main/java/org/slf4j/profiler/SpacePadder.java
Expand Up @@ -32,6 +32,7 @@ public class SpacePadder {
" ", // 16 spaces
" " }; // 32 spaces

@Deprecated
final static public void leftPad(StringBuffer buf, String s, int desiredLength) {
int actualLen = 0;
if (s != null) {
Expand All @@ -45,6 +46,20 @@ final static public void leftPad(StringBuffer buf, String s, int desiredLength)
}
}

final static public void leftPad(StringBuilder buf, String s, int desiredLength) {
int actualLen = 0;
if (s != null) {
actualLen = s.length();
}
if (actualLen < desiredLength) {
spacePad(buf, desiredLength - actualLen);
}
if (s != null) {
buf.append(s);
}
}

@Deprecated
final static public void rightPad(StringBuffer buf, String s, int desiredLength) {
int actualLen = 0;
if (s != null) {
Expand All @@ -57,13 +72,27 @@ final static public void rightPad(StringBuffer buf, String s, int desiredLength)
spacePad(buf, desiredLength - actualLen);
}
}


final static public void rightPad(StringBuilder buf, String s, int desiredLength) {
int actualLen = 0;
if (s != null) {
actualLen = s.length();
}
if (s != null) {
buf.append(s);
}
if (actualLen < desiredLength) {
spacePad(buf, desiredLength - actualLen);
}
}

/**
* Fast space padding method.
*
* @param sbuf the buffer to pad
* @param length the target size of the buffer after padding
*/
@Deprecated
final static public void spacePad(StringBuffer sbuf, int length) {
while (length >= 32) {
sbuf.append(SPACES[5]);
Expand All @@ -76,4 +105,23 @@ final static public void spacePad(StringBuffer sbuf, int length) {
}
}
}

/**
* Fast space padding method.
*
* @param sbuf the buffer to pad
* @param length the target size of the buffer after padding
*/
final static public void spacePad(StringBuilder sbuf, int length) {
while (length >= 32) {
sbuf.append(SPACES[5]);
length -= 32;
}

for (int i = 4; i >= 0; i--) {
if ((length & (1 << i)) != 0) {
sbuf.append(SPACES[i]);
}
}
}
}
2 changes: 1 addition & 1 deletion slf4j-ext/src/main/java/org/slf4j/profiler/StopWatch.java
Expand Up @@ -75,7 +75,7 @@ public StopWatch stop(long stopTime) {

@Override
public String toString() {
StringBuffer buf = new StringBuffer();
StringBuilder buf = new StringBuilder();
buf.append("StopWatch [");
buf.append(name);
buf.append("] ");
Expand Down
6 changes: 3 additions & 3 deletions slf4j-ext/src/main/java/org/slf4j/profiler/Util.java
Expand Up @@ -68,13 +68,13 @@ static public double convertToSeconds(long nanos) {
return ((double) nanos / NANOS_IN_ONE_SECOND);
}

static String durationInDurationUnitsAsStr(StringBuffer buf, StopWatch sw) {
static String durationInDurationUnitsAsStr(StringBuilder buf, StopWatch sw) {
DurationUnit du = selectDurationUnitForDisplay(sw);
return durationInDurationUnitsAsStr(sw.elapsedTime(), du);
}

static String durationInDurationUnitsAsStr(long nanos, DurationUnit durationUnit) {
StringBuffer buf = new StringBuffer();
StringBuilder buf = new StringBuilder();
switch (durationUnit) {
case NANOSECOND:
buf.append(nanos);
Expand All @@ -95,7 +95,7 @@ static String durationInDurationUnitsAsStr(long nanos, DurationUnit durationUnit
return buf.toString();
}

static void appendDurationUnitAsStr(StringBuffer buf, DurationUnit durationUnit) {
static void appendDurationUnitAsStr(StringBuilder buf, DurationUnit durationUnit) {
switch (durationUnit) {
case NANOSECOND:
buf.append("nanoseconds.");
Expand Down
Expand Up @@ -48,7 +48,7 @@ public String abbreviate(String filename) {
// we cant't process this string
return filename;
}
StringBuffer buf = new StringBuffer(desiredLength);
StringBuilder buf = new StringBuilder(desiredLength);
buf.append(filename.substring(0, firstIndex + 1));
buf.append(FILLER);
int nextIndex = computeNextIndex(filename, firstIndex);
Expand Down
Expand Up @@ -364,7 +364,7 @@ List<String> doSanityAnalysis() {
}

void showDialogBox(List<String> errorList) {
StringBuffer buf = new StringBuffer();
StringBuilder buf = new StringBuilder();
buf.append("<html>");
int i = 1;
for (String msg : errorList) {
Expand Down
Expand Up @@ -73,7 +73,7 @@ public String getReplacement(int groupIndex) {
* @see org.slf4j.converter.ConversionRule#replace(java.util.regex.Matcher)
*/
public String replace(Matcher matcher) {
StringBuffer replacementBuffer = new StringBuffer();
StringBuilder replacementBuffer = new StringBuilder();
String replacementText;

for (int group = 1; group <= matcher.groupCount(); group++) {
Expand Down
Expand Up @@ -36,7 +36,7 @@ public class RandomHelper {
}

private String randomString(int len) {
StringBuffer buf = new StringBuffer();
StringBuilder buf = new StringBuilder();
for (int i = 0; i < len; i++) {
int offset = random.nextInt(26);
char c = (char) ('a' + offset);
Expand All @@ -50,7 +50,7 @@ int nextInt(int n) {
}

String buildRandomFileName(int averageNodeLength, int totalLength) {
StringBuffer buf = new StringBuffer();
StringBuilder buf = new StringBuilder();
int MAX_NODE_LENGTH = averageNodeLength * 2;
while (buf.length() < totalLength) {
int remaining = totalLength - buf.length();
Expand Down
4 changes: 2 additions & 2 deletions slf4j-simple/src/main/java/org/slf4j/impl/SimpleLogger.java
Expand Up @@ -319,7 +319,7 @@ private void log(int level, String message, Throwable t) {
return;
}

StringBuffer buf = new StringBuffer(32);
StringBuilder buf = new StringBuilder(32);

// Append date-time if so configured
if (SHOW_DATE_TIME) {
Expand Down Expand Up @@ -377,7 +377,7 @@ private void log(int level, String message, Throwable t) {

}

void write(StringBuffer buf, Throwable t) {
void write(StringBuilder buf, Throwable t) {
TARGET_STREAM.println(buf.toString());
if (t != null) {
t.printStackTrace(TARGET_STREAM);
Expand Down

0 comments on commit b3c3735

Please sign in to comment.