链接: pan.baidu.com/s/1d6YONkCi… 提取码: iamh 作者-\/ 307570512
2.3 方案
进行到这一步,高效的算法已经呼之欲出了。算法如下:
把字符 UTF-8 数据的二进制长度不为 1 的称为特征点。除特征点外,每个字符都是一个字节长度。记下所有特征点的对应关系,对于给定的 bytePos,都可以根据公式计算得到 charPos。
公式为:
charPos = bytePos - preBytePos + preCharPos
举例:
则本实例中有两个特征点 一、个,记作:
bytes[6:] = chars[4:]
bytes[9:] = chars[5:]
如果给定 bytePos 10, 首先找到前一个特征点的对应关系 9(preBytePos) -> 5(preCharPos), 根据公式得出 (10 - 9) + 5 = 6。
2.4 核心代码
该算法还有一个比较关键的问题要解决,即高效的计算一个 char 的字节长度。计算 char 的字节长度的算法参考了
// 计算特征点
private int[][] calcSpecialPos(String str) {
ArrayList<int[]> specialPos = new ArrayList<>()
specialPos.add(new int[] {0, 0});
int lastCharPost = 0;
int lastBytePos = 0;
Charset utf8 = Charset.forName("UTF-8");
CharsetEncoder encoder = utf8.newEncoder();
CharBuffer input = CharBuffer.wrap(str.toCharArray());
ByteBuffer output = ByteBuffer.allocate(10);
int limit = input.limit();
while(input.position() < limit) {
output.clear();
input.mark();
input.limit(Math.min(input.position() + 2, input.capacity()));
if (Character.isHighSurrogate(input.get()) && !Character.isLowSurrogate(input.get())) {
//Malformed surrogate pair
lastCharPost++;
}
input.limit(input.position());
input.reset();
encoder.encode(input, output, false);
int encodedLen = output.position();
lastCharPost++;
lastBytePos += encodedLen;
if (encodedLen != 1) { // 特征点
specialPos.add(new int[]{lastBytePos, lastCharPost});
}
}
return toArray(specialPos);
}
// 根据特征点,计算 bytePos 对应的 charPos
private int calcPos(int[][] specialPos, int bytePos) {
// 如果只有一个元素 {0, 0),说明没有特征值
if (specialPos.length == 1) return bytePos;
int pos = Arrays.binarySearch(specialPos,
new int[] {bytePos, 0},
(int[] a, int[] b) -> Integer.compare(a[0], b[0]));
if (pos >= 0) {
return specialPos[pos][1];
} else {
// if binary search not fonund, will return (-(insertion point) - 1),
// so here -2 is mean -1 to get insertpoint and then -1 to get previous specialPos
int[] preSpecialPos = specialPos[-pos-2];
return bytePos - preSpecialPos[0] + preSpecialPos[1];
}
}
3 优化二
3.1 背景
接下来解决第二个函数 rest。该函数的功能是得到 JsonArray(gson) 的除第一个元素外的所有元素。
由于 rest 是在一个递归函数中被调用且递归栈很深,因此如果 rest 实现的不够高效,其影响会被成倍放大。
3.2 原实现
private JsonArray rest(JsonArray arr) {
JsonArray result = new JsonArray();
if (arr.size() > 1) {
for (int i = 1; i < arr.size(); i++) {
result.add(arr.get(i));
}
}
return result;
}
3.3 剖析
通过调试发现 JsonArray 中存储了相当大的数据,对于频繁调用的场景,每次都对其重新构建明显不是一个明智的选择。
通过查看返回的 JsonArray 使用情况,我们得到了另一条线索:仅仅使用里面的数据,而不涉及修改。
考虑到 JsonArray 被实现成 final,最后方案确定为实现一个针对 rest 这种需求定制的代理类。
3.4 方案 & 代码
代理类 JsonArrayWrapper 分别对 first、rest、foreach 等功能进行了实现。
class JsonArrayWrapper implements Iterable<JsonElement> {
private JsonArray jsonArray;
private int mark;
public JsonArrayWrapper() {
this.jsonArray = new JsonArray();
this.mark = 0;
}
public JsonArrayWrapper(JsonArray jsonArray) {
this.jsonArray= jsonArray;
this.mark = 0;
}
public JsonArrayWrapper(JsonArray jsonArray, int mark) {
this.jsonArray = jsonArray;
this.mark = mark;
}
public JsonObject first() {
return jsonArray.get(mark).getAsJsonObject();
}
public JsonArrayWrapper rest() {
return new JsonArrayWrapper(jsonArray, mark+1);
}
public int size() {
return jsonArray.size() - mark;
}
public JsonElement get(int n) {
return jsonArray.get(mark + n);
}
public void add(JsonElement jsonElement) {
jsonArray.add(jsonElement);
}
public void addAll(JsonArrayWrapper jsonArrayWrapper) {
jsonArrayWrapper.forEach(this.jsonArray::add);
}
@Override
public Iterator<JsonElement> iterator() {
JsonArray jsonarray = new JsonArray();
this.forEach(e -> jsonarray.add(e));
return jsonarray.iterator();
}
@Override
public void forEach(Consumer<? super JsonElement> action) {
for (int i=mark; i<jsonArray.size(); i++) {
action.accept(jsonArray.get(i));
}
}
}
4 成果
经过这两个主要的优化,就解决了代码中的性能问题,成果如下图所示:
\