Skip to content

Commit 3c172b6

Browse files
committed
默认启用mirror池,500w次的简单json字符串解码耗时,从66秒降到26秒,对得起观众了吧
1 parent 35ed83e commit 3c172b6

File tree

4 files changed

+66
-34
lines changed

4 files changed

+66
-34
lines changed

performance/org/nutz/json/JsonFaster.java

+35-14
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,45 @@ public class JsonFaster {
1010

1111
@Test
1212
public void json() {
13-
nutzJson(10000);
14-
fastJson(10000);
13+
String jsonStr = "{name:'wendal'}";
14+
nutzJson(100000, jsonStr);
15+
fastJson(100000, jsonStr);
16+
System.gc();
17+
18+
int t = 5000*10000;
1519

1620
Stopwatch sw = Stopwatch.begin();
17-
nutzJson(50*10000);
21+
nutzJson(t, jsonStr);
1822
sw.stop();
19-
System.out.println("Nutz-Json 50w次耗时: " + sw.getDuration());
23+
System.out.println("Nutz-Json "+t+"次耗时: " + sw.getDuration());
24+
System.gc();
2025

21-
sw.start();
22-
fastJson(50*10000);
23-
System.out.println("Fast-Json 50w次耗时: " + sw.getDuration());
26+
sw = Stopwatch.begin();
27+
//fastJson(t, jsonStr);
28+
sw.stop();
29+
System.out.println("Fast-Json "+t+"次耗时: " + sw.getDuration());
30+
System.gc();
2431

2532
//-------------------------------------------------------------------
26-
sw.start();
27-
nutzJson(50*10000);
33+
sw = Stopwatch.begin();
34+
nutzJson(t, jsonStr);
2835
sw.stop();
29-
System.out.println("Nutz-Json 50w次耗时: " + sw.getDuration());
36+
System.out.println("Nutz-Json "+t+"次耗时: " + sw.getDuration());
37+
System.gc();
3038

31-
sw.start();
32-
fastJson(50*10000);
39+
sw = Stopwatch.begin();
40+
//fastJson(t, jsonStr);
3341
sw.stop();
34-
System.out.println("Fast-Json 50w次耗时: " + sw.getDuration());
42+
System.out.println("Fast-Json "+t+"次耗时: " + sw.getDuration());
43+
System.gc();
3544
}
3645

3746
public void nutzJson(int time) {
3847
JsonObject obj = new JsonObject();
3948
obj.setName("wendal");
4049
for (int i = 0; i < time; i++) {
4150
String jsonStr = Json.toJson(obj);
42-
obj = Json.fromJson(JsonObject.class, jsonStr);
51+
Json.fromJson(jsonStr);
4352
}
4453
}
4554

@@ -52,6 +61,18 @@ public void fastJson(int time) {
5261
}
5362
}
5463

64+
public void nutzJson(int time, String jsonStr) {
65+
for (int i = 0; i < time; i++) {
66+
Json.fromJson(JsonObject.class, jsonStr);
67+
}
68+
}
69+
70+
public void fastJson(int time, String jsonStr) {
71+
for (int i = 0; i < time; i++) {
72+
JSON.parseObject(jsonStr, JsonObject.class);
73+
}
74+
}
75+
5576
public static void main(String[] args) throws Throwable {
5677
Thread.sleep(60*1000);
5778
new JsonFaster().json();

src/org/nutz/conf/NutConf.java

+1
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,5 @@ public static void clear() {
134134
}
135135

136136
public static boolean USE_FASTCLASS = !Lang.isAndroid;
137+
public static boolean USE_MIRROR_CACHE = true;
137138
}

src/org/nutz/lang/Mirror.java

+24-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import org.nutz.castor.Castors;
2424
import org.nutz.castor.FailToCastObjectException;
25+
import org.nutz.conf.NutConf;
2526
import org.nutz.lang.born.BornContext;
2627
import org.nutz.lang.born.Borning;
2728
import org.nutz.lang.born.BorningException;
@@ -43,6 +44,11 @@
4344
* @param <T>
4445
*/
4546
public class Mirror<T> {
47+
48+
@SuppressWarnings("rawtypes")
49+
static Map<Type, Mirror> mirrorCache = new HashMap<Type, Mirror>();
50+
51+
protected BornContext<T> emtryArgsBornContext;
4652

4753
private static class DefaultTypeExtractor implements TypeExtractor {
4854

@@ -156,9 +162,16 @@ public static <T> Mirror<T> me(Type type) {
156162
if (null == type) {
157163
return null;
158164
}
159-
Mirror<T> mir = (Mirror<T>) Mirror.me(Lang.getTypeClass(type));
160-
mir.type = type;
161-
return mir;
165+
if (NutConf.USE_MIRROR_CACHE) {
166+
Mirror<T> mir = mirrorCache.get(type);
167+
if (mir == null) {
168+
mir = (Mirror<T>) Mirror.me(Lang.getTypeClass(type));
169+
mir.type = type;
170+
mirrorCache.put(type, mir);
171+
}
172+
return mir;
173+
}
174+
return (Mirror<T>) Mirror.me(Lang.getTypeClass(type));
162175
}
163176

164177
private Class<T> klass;
@@ -983,7 +996,14 @@ public Borning<T> getBorningByArgTypes(Class<?>... argTypes) throws BorningExcep
983996
* @return 新对象
984997
*/
985998
public T born(Object... args) {
986-
BornContext<T> bc = Borns.eval(klass, args);
999+
BornContext<T> bc;
1000+
if (NutConf.USE_MIRROR_CACHE && args.length == 0) {
1001+
if (emtryArgsBornContext == null)
1002+
emtryArgsBornContext = Borns.eval(klass, args);
1003+
bc = emtryArgsBornContext;
1004+
}
1005+
else
1006+
bc = Borns.eval(klass, args);
9871007
if (null == bc)
9881008
throw new BorningException(klass, args);
9891009

src/org/nutz/mapl/impl/convert/ObjConvertImpl.java

+6-16
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.LinkedHashMap;
99
import java.util.List;
1010
import java.util.Map;
11+
import java.util.Map.Entry;
1112
import java.util.Set;
1213
import java.util.Stack;
1314

@@ -192,35 +193,24 @@ private Object injectObj(Object model, Mirror<?> mirror) {
192193
Map<String, ?> map = (Map<String, ?>) model;
193194

194195
JsonEntity jen = Json.getEntity(mirror);
195-
for (String key : map.keySet()) {
196+
for (Entry<String, ?> en : map.entrySet()) {
197+
Object val = en.getValue();
198+
if (val == null)
199+
continue;
200+
String key = en.getKey();
196201
JsonEntityField jef = jen.getField(key);
197202
if (jef == null) {
198203
continue;
199204
}
200-
201-
Object val = map.get(jef.getName());
202-
if (val == null) {
203-
continue;
204-
}
205-
206205
if (isLeaf(val)) {
207206
if (val instanceof El) {
208207
val = ((El) val).eval(context);
209208
}
210-
// zzh@2012-09-14: 暂时去掉 createBy 吧
211-
// jef.setValue(obj, Castors.me().castTo(jef.createValue(obj,
212-
// val, null), Lang.getTypeClass(jef.getGenericType())));
213-
// jef.setValue(obj, jef.createValue(obj, val, null));
214209
jef.setValue(obj, Mapl.maplistToObj(val, jef.getGenericType()));
215210
continue;
216211
} else {
217212
path.push(key);
218-
// jef.setValue(obj, Mapl.maplistToObj(val,
219-
// me.getGenericsType(0)));
220213
jef.setValue(obj, Mapl.maplistToObj(val, jef.getGenericType()));
221-
// zzh@2012-09-14: 暂时去掉 createBy 吧
222-
// jef.setValue(obj, jef.createValue(obj, val,
223-
// me.getGenericsType(0)));
224214
}
225215
}
226216
return obj;

0 commit comments

Comments
 (0)