关于JSON的大文本处理
JSON在处理大文本数据的时候存在严重的效率问题,反序列化的时候CPU占用率甚至可以达到80%,
找到个处理办法是在客户端就拆成小片段,到服务器再组装起来,如下:
然后在服务器端组装起来:
找到个处理办法是在客户端就拆成小片段,到服务器再组装起来,如下:
1//大文本优化
2 var originContent = article.Content;
3 var contentLen = originContent.length;
4 if(contentLen > 1000){
5 article.Content = ''; //clear it!!!
6 var len = Math.ceil(contentLen / 1000);
7 var splitContent = [];
8 for(var i=0;i < len; i++){
9 var start = i*1000;
10 var end = start + 1000;
11 if(end > contentLen-1) end = contentLen -1;
12 splitContent.push(originContent.substring(start,end));
13 }
14 article.ContentSegments = splitContent;
15 }
2 var originContent = article.Content;
3 var contentLen = originContent.length;
4 if(contentLen > 1000){
5 article.Content = ''; //clear it!!!
6 var len = Math.ceil(contentLen / 1000);
7 var splitContent = [];
8 for(var i=0;i < len; i++){
9 var start = i*1000;
10 var end = start + 1000;
11 if(end > contentLen-1) end = contentLen -1;
12 splitContent.push(originContent.substring(start,end));
13 }
14 article.ContentSegments = splitContent;
15 }
然后在服务器端组装起来:
1if(article.Content.Length == 0)
2 {
3 if(article.ContentSegments != null && article.ContentSegments.Count > 0)
4 {
5 StringBuilder buf = new StringBuilder();
6 foreach (string s in article.ContentSegments)
7 {
8 buf.Append(s);
9 }
10 article.Content = buf.ToString();
11 }
12 }
2 {
3 if(article.ContentSegments != null && article.ContentSegments.Count > 0)
4 {
5 StringBuilder buf = new StringBuilder();
6 foreach (string s in article.ContentSegments)
7 {
8 buf.Append(s);
9 }
10 article.Content = buf.ToString();
11 }
12 }