Skip to content

Commit a03f28d

Browse files
committed
使用String.concat而非StringBuilder,实测更快。
1 parent 93b26d4 commit a03f28d

File tree

8 files changed

+73
-72
lines changed

8 files changed

+73
-72
lines changed

src/main/generator/mybatis/CustomXmlFormatter.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,9 @@ private static class CustomXmlGenerator {
4040
}
4141

4242
private static String generaterContent(Document document) {
43-
StringBuilder sb = new StringBuilder();
44-
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
45-
sb.append(lineSeparator);
46-
sb.append(
47-
"<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\">");
48-
sb.append(lineSeparator);
49-
sb.append(getFormattedContent(document.getRootElement(), 0));
50-
return sb.toString();
43+
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>".concat(lineSeparator).concat(
44+
"<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\">")
45+
.concat(lineSeparator).concat(getFormattedContent(document.getRootElement(), 0));
5146
}
5247

5348
private static String getFormattedContent(XmlElement element, int indentLevel) {

src/main/java/frodez/config/aop/request/checker/impl/KeyGenerator.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@
1111
public class KeyGenerator {
1212

1313
public static String servletKey(String sault, HttpServletRequest request) {
14-
StringBuilder builder = new StringBuilder(sault).append(DefStr.SEPERATOR).append(request.getRequestURI());
14+
String key = sault.concat(DefStr.SEPERATOR).concat(request.getRequestURI());
1515
if (URLMatcher.needVerify(request.getRequestURI())) {
1616
// 非登录接口使用token判断,同一token不能重复请求
1717
String fullToken = TokenManager.getFullToken(request);
1818
if (fullToken == null) {
19-
return builder.toString();
19+
return key;
2020
} else {
21-
return builder.append(DefStr.SEPERATOR).append(fullToken).toString();
21+
return key.concat(DefStr.SEPERATOR).concat(fullToken);
2222
}
2323
} else {
2424
// 登录接口使用IP判断,同一IP不能重复请求
25-
return builder.append(DefStr.SEPERATOR).append(ServletUtil.getAddr(request)).toString();
25+
return key.concat(DefStr.SEPERATOR).concat(ServletUtil.getAddr(request));
2626
}
2727
}
2828

src/main/java/frodez/config/mvc/JacksonConfig.java

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/main/java/frodez/config/mvc/JsonHttpMessageConverer.java

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import org.springframework.http.converter.json.MappingJacksonInputMessage;
3636
import org.springframework.http.converter.json.MappingJacksonValue;
3737
import org.springframework.lang.Nullable;
38-
import org.springframework.util.Assert;
3938
import org.springframework.util.TypeUtils;
4039

4140
public class JsonHttpMessageConverer extends AbstractGenericHttpMessageConverter<Object> {
@@ -66,7 +65,6 @@ public JsonHttpMessageConverer(ObjectMapper objectMapper) {
6665
DefaultPrettyPrinter prettyPrinter = new DefaultPrettyPrinter();
6766
prettyPrinter.indentObjectsWith(new DefaultIndenter(" ", "\ndata:"));
6867
this.ssePrettyPrinter = prettyPrinter;
69-
setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_JSON, new MediaType("application", "*+json")));
7068
}
7169

7270
public JsonHttpMessageConverer(ObjectMapper objectMapper, MediaType supportedMediaType) {
@@ -94,29 +92,6 @@ private JavaType getJavaType(Type type, @Nullable Class<?> contextClass) {
9492
.constructType(GenericTypeResolver.resolveType(type, contextClass)));
9593
}
9694

97-
/**
98-
* Set the {@code ObjectMapper} for this view. If not set, a default {@link ObjectMapper#ObjectMapper()
99-
* ObjectMapper} is used.
100-
* <p>
101-
* Setting a custom-configured {@code ObjectMapper} is one way to take further control of the JSON serialization
102-
* process. For example, an extended {@link com.fasterxml.jackson.databind.ser.SerializerFactory} can be configured
103-
* that provides custom serializers for specific types. The other option for refining the serialization process is
104-
* to use Jackson's provided annotations on the types to be serialized, in which case a custom-configured
105-
* ObjectMapper is unnecessary.
106-
*/
107-
public void setObjectMapper(ObjectMapper objectMapper) {
108-
Assert.notNull(objectMapper, "ObjectMapper must not be null");
109-
this.objectMapper = objectMapper;
110-
configurePrettyPrint();
111-
}
112-
113-
/**
114-
* Return the underlying {@code ObjectMapper} for this view.
115-
*/
116-
public ObjectMapper getObjectMapper() {
117-
return this.objectMapper;
118-
}
119-
12095
/**
12196
* Whether to use the {@link DefaultPrettyPrinter} when writing JSON. This is a shortcut for setting up an
12297
* {@code ObjectMapper} as follows:

src/main/java/frodez/config/mvc/WebMvcConfiguration.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import frodez.util.json.JSONUtil;
44
import java.util.List;
55
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.http.MediaType;
67
import org.springframework.http.converter.HttpMessageConverter;
78
import org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter;
89
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@@ -18,7 +19,8 @@ public void configureMessageConverters(List<HttpMessageConverter<?>> converters)
1819
break;
1920
}
2021
}
21-
converters.add(0, new JsonHttpMessageConverer(JSONUtil.mapper()));
22+
converters.add(0, new JsonHttpMessageConverer(JSONUtil.mapper(), MediaType.APPLICATION_JSON, new MediaType(
23+
"application", "*+json")));
2224
}
2325

2426
}

src/main/java/frodez/util/common/RegexUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ public static boolean match(String regex, String input, int flag) {
3030
if (flag == 0) {
3131
return CACHE.computeIfAbsent(regex, i -> Pattern.compile(i)).matcher(input).matches();
3232
}
33-
return CACHE.computeIfAbsent(new StringBuilder(regex).append(DefStr.SEPERATOR).append(flag).toString(),
34-
i -> Pattern.compile(i)).matcher(input).matches();
33+
return CACHE.computeIfAbsent(regex.concat(DefStr.SEPERATOR).concat(Integer.toString(flag)), i -> Pattern
34+
.compile(i)).matcher(input).matches();
3535
}
3636

3737
/**

src/main/java/frodez/util/reflect/BeanUtil.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ public class BeanUtil {
3838
private static final int GETTER_SETTER_MODIFIER = Modifier.PUBLIC;
3939

4040
private static BeanCopier getCopier(Object source, Object target) {
41-
return COPIER_CACHE.computeIfAbsent(new StringBuilder(source.getClass().getName()).append(target.getClass()
42-
.getName()).toString(), i -> BeanCopier.create(source.getClass(), target.getClass(), false));
41+
return COPIER_CACHE.computeIfAbsent(source.getClass().getName().concat(target.getClass().getName()).toString(),
42+
i -> BeanCopier.create(source.getClass(), target.getClass(), false));
4343
}
4444

4545
/**
@@ -202,8 +202,8 @@ public Map<String, Pair<FastMethod, FastMethod>> getProperties(Class<?> klass) {
202202
Pair<FastMethod, FastMethod> pair = new Pair<>();
203203
pair.setKey(getter);
204204
pair.setValue(method);
205-
properties.put(new StringBuilder().append(Character.toLowerCase(propertyName.charAt(0))).append(
206-
propertyName.substring(1)).toString(), pair);
205+
properties.put(String.valueOf(Character.toLowerCase(propertyName.charAt(0))).concat(propertyName
206+
.substring(1)), pair);
207207
}
208208
}
209209
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package frodez;
2+
3+
import java.util.UUID;
4+
5+
public class StringTest {
6+
7+
public static void main(String[] args) {
8+
int testLength = 100000000;
9+
int arrayLength = 10;
10+
String[] arr = new String[arrayLength];
11+
12+
long start = System.currentTimeMillis();
13+
String testStr = UUID.randomUUID().toString();
14+
System.out.println("首次生成randomUUID耗时:" + (System.currentTimeMillis() - start));
15+
16+
// start = new Date();
17+
// for (int i = 0; i < testLength; i++) {
18+
// testStr = UUID.randomUUID().toString();
19+
// }
20+
// System.out.println("非首次生成randomUUID " + testLength + "次耗时:" + (new Date().getTime() - start.getTime()));
21+
22+
@SuppressWarnings("unused")
23+
String str = "";
24+
start = System.currentTimeMillis();
25+
for (int i = 0; i < testLength; i++) {
26+
str = "";
27+
for (int j = 0; j < arrayLength; j++) {
28+
str = testStr + testStr;
29+
}
30+
}
31+
System.out.println("String 拼接测试,测试长度" + testLength + ",测试字符串数组长度" + arr.length + ",完成时间" + (System
32+
.currentTimeMillis() - start));
33+
34+
start = System.currentTimeMillis();
35+
for (int i = 0; i < testLength; i++) {
36+
str = "";
37+
for (int j = 0; j < arrayLength; j++) {
38+
str = testStr.concat(testStr);
39+
}
40+
}
41+
System.out.println("String.concat 拼接测试,测试长度" + testLength + ",测试字符串数组长度" + arr.length + ",完成时间" + (System
42+
.currentTimeMillis() - start));
43+
44+
start = System.currentTimeMillis();
45+
for (int i = 0; i < testLength; i++) {
46+
str = "";
47+
StringBuilder sb = new StringBuilder();
48+
for (int j = 0; j < arr.length; j++) {
49+
sb.append(testStr);
50+
}
51+
str = sb.toString();
52+
}
53+
System.out.println("StringBuilder 拼接测试,测试长度" + testLength + ",测试字符串数组长度" + arr.length + ",完成时间" + (System
54+
.currentTimeMillis() - start));
55+
}
56+
57+
}

0 commit comments

Comments
 (0)