@@ -1031,10 +1031,14 @@ public JSONArray onArrayParse(JSONObject request, String parentPath, String name
10311031 int index = isSubquery || name == null ? -1 : name .lastIndexOf ("[]" );
10321032 String childPath = index <= 0 ? null : Pair .parseEntry (name .substring (0 , index ), true ).getKey (); // Table-key1-key2...
10331033
1034+ String arrTableKey = null ;
10341035 //判断第一个key,即Table是否存在,如果存在就提取
10351036 String [] childKeys = StringUtil .split (childPath , "-" , false );
10361037 if (childKeys == null || childKeys .length <= 0 || request .containsKey (childKeys [0 ]) == false ) {
10371038 childKeys = null ;
1039+ }
1040+ else if (childKeys .length == 1 && JSONRequest .isTableKey (childKeys [0 ])) { // 可能无需提取,直接返回 rawList 即可
1041+ arrTableKey = childKeys [0 ];
10381042 }
10391043
10401044
@@ -1045,25 +1049,46 @@ public JSONArray onArrayParse(JSONObject request, String parentPath, String name
10451049 .setCount (size )
10461050 .setPage (page )
10471051 .setQuery (query2 )
1052+ .setTable (arrTableKey )
10481053 .setJoinList (onJoinParse (join , request ));
10491054
10501055 JSONObject parent ;
10511056
1052- long startTime = System .currentTimeMillis ();
1057+ boolean isExtract = true ;
1058+
10531059 //生成size个
10541060 for (int i = 0 ; i < (isSubquery ? 1 : size ); i ++) {
10551061 parent = onObjectParse (request , isSubquery ? parentPath : path , isSubquery ? name : "" + i , config .setType (SQLConfig .TYPE_ITEM ).setPosition (i ), isSubquery );
10561062 if (parent == null || parent .isEmpty ()) {
10571063 break ;
10581064 }
10591065
1066+ long startTime = System .currentTimeMillis ();
1067+
1068+ /* 这里优化了 Table[]: { Table:{} } 这种情况下的性能
1069+ * 如果把 List<JSONObject> 改成 JSONArray 来减少以下 addAll 一次复制,则会导致 AbstractSQLExecutor 等其它很多地方 get 要改为 getJSONObject,
1070+ * 修改类型会导致不兼容旧版依赖 ORM 的项目,而且整体上性能只有特殊情况下性能提升,其它非特殊情况下因为多出很多 instanceof JSONObject 的判断而降低了性能。
1071+ */
1072+ JSONObject fo = i != 0 || arrTableKey == null ? null : parent .getJSONObject (arrTableKey );
1073+ @ SuppressWarnings ("unchecked" )
1074+ List <JSONObject > list = fo == null ? null : (List <JSONObject >) fo .remove (SQLExecutor .KEY_RAW_LIST );
1075+
1076+ if (list != null && list .isEmpty () == false ) {
1077+ isExtract = false ;
1078+
1079+ list .set (0 , fo ); // 不知道为啥第 0 项也加了 @RAW@LIST
1080+ response .addAll (list ); // List<JSONObject> cannot match List<Object> response = new JSONArray(list);
1081+
1082+ long endTime = System .currentTimeMillis (); // 0ms
1083+ Log .d (TAG , "\n onArrayParse <<<<<<<<<<<<<<<<<<<<<<<<<<<<\n for (int i = 0; i < (isSubquery ? 1 : size); i++) "
1084+ + " startTime = " + startTime + "; endTime = " + endTime + "; duration = " + (endTime - startTime ) + "\n >>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n " );
1085+ break ;
1086+ }
1087+
10601088 //key[]:{Table:{}}中key equals Table时 提取Table
10611089 response .add (getValue (parent , childKeys )); //null有意义
10621090 }
10631091
1064- long endTime = System .currentTimeMillis ();
1065- Log .e (TAG , "onArrayParse for for (int i = 0; i < (isSubquery ? 1 : size); i++) startTime = " + startTime + "; endTime = " + endTime + "; duration = " + (endTime - startTime ));
1066-
10671092 //Table>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
10681093
10691094
@@ -1082,12 +1107,20 @@ public JSONArray onArrayParse(JSONObject request, String parentPath, String name
10821107 }
10831108 }
10841109 */
1085- Object fo = childKeys == null || response .isEmpty () ? null : response .get (0 );
1086- if (fo instanceof Boolean || fo instanceof Number || fo instanceof String ) { //[{}] 和 [[]] 都没意义
1087- putQueryResult (path , response );
1110+ if (isExtract ) {
1111+ long startTime = System .currentTimeMillis ();
1112+
1113+ Object fo = childKeys == null || response .isEmpty () ? null : response .get (0 );
1114+ if (fo instanceof Boolean || fo instanceof Number || fo instanceof String ) { //[{}] 和 [[]] 都没意义
1115+ putQueryResult (path , response );
1116+ }
1117+
1118+ long endTime = System .currentTimeMillis ();
1119+ Log .d (TAG , "\n onArrayParse <<<<<<<<<<<<<<<<<<<<<<<<<<<<\n isExtract >> putQueryResult "
1120+ + " startTime = " + startTime + "; endTime = " + endTime + "; duration = " + (endTime - startTime ) + "\n >>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n " );
10881121 }
1089- } finally {
10901122
1123+ } finally {
10911124 //后面还可能用到,要还原
10921125 request .put (JSONRequest .KEY_QUERY , query );
10931126 request .put (JSONRequest .KEY_COUNT , count );
0 commit comments