-
Notifications
You must be signed in to change notification settings - Fork 444
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
借助新的并行计算工具构造了一个Trie结构,在保持APK Parser性能基本不变的情况下,将 自动适配表达式的性能提升了10倍。原先一个表达式求值需要耗费将近1秒,现在只需20ms不到。 旧的Benchmark记录 BM: Parse DexClasses takes 758 ms. BM: Search android.support.v4.app takes 747 ms. BM: Search com.tencent.mm.ui.contact takes 1239 ms. BM: Search com.tencent.mm.plugin.sns.ui takes 1393 ms. BM: Search com.tencent.mm takes 1635 ms. BM: Search com.tencent.mm.ui.conversation takes 761 ms. BM: Search com.tencent.mm.sdk.platformtools takes 860 ms. BM: Search com.tencent.mm.sdk.platformtools takes 669 ms. BM: Search com.tencent.mm.booter.notification takes 561 ms. BM: Search com.tencent.mm.storage takes 871 ms. BM: Search com.tencent.mm.booter.notification.queue takes 913 ms. BM: Search com.tencent.mm.ui takes 518 ms. BM: Search com.tencent.mm.modelsfs takes 626 ms. BM: Search com.tencent.mm.ui.chatting takes 470 ms. BM: Search over classes takes 4414 ms. 数据结构升级后的Benchmark记录 BM: Parse DexClasses takes 779 ms. BM: Search android.support.v4.app takes 42 ms. BM: Search com.tencent.mm takes 167 ms. BM: Search com.tencent.mm.sdk.platformtools takes 5 ms. BM: Search com.tencent.mm.booter.notification takes 1 ms. BM: Search com.tencent.mm.booter.notification.queue takes 0 ms. BM: Search com.tencent.mm.modelsfs takes 0 ms. BM: Search com.tencent.mm.plugin.sns.ui takes 66 ms. BM: Search com.tencent.mm.storage takes 1 ms. BM: Search com.tencent.mm.ui takes 2 ms. BM: Search com.tencent.mm.ui.chatting takes 24 ms. BM: Search com.tencent.mm.ui.contact takes 19 ms. BM: Search com.tencent.mm.ui.conversation takes 14 ms. BM: Search over classes takes 558 ms.
- Loading branch information
Em3rs0n
authored and
Em3rs0n
committed
Nov 24, 2018
1 parent
7fcb0d7
commit 388e25f
Showing
5 changed files
with
105 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/main/kotlin/com/gh0u1l5/wechatmagician/spellbook/parser/ClassTrie.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.gh0u1l5.wechatmagician.spellbook.parser | ||
|
||
import com.gh0u1l5.wechatmagician.spellbook.util.ParallelUtil.parallelForEach | ||
import java.util.concurrent.ConcurrentHashMap | ||
|
||
class ClassTrie { | ||
companion object { | ||
private fun convertJVMTypeToClassName(type: String) = | ||
type.substring(1, type.length - 1).replace('/', '.') | ||
} | ||
|
||
private val head: TrieNode = TrieNode() | ||
|
||
operator fun plusAssign(type: String) { | ||
head.add(convertJVMTypeToClassName(type)) | ||
} | ||
|
||
operator fun plusAssign(types: Array<String>) { | ||
types.asList().parallelForEach { this += it } | ||
} | ||
|
||
fun search(packageName: String, depth: Int): List<String> { | ||
return head.search(packageName, depth) | ||
} | ||
|
||
private class TrieNode { | ||
val classes: MutableList<String> = ArrayList(50) | ||
|
||
val children: MutableMap<String, TrieNode> = ConcurrentHashMap() | ||
|
||
fun add(className: String) { | ||
add(className, 0) | ||
} | ||
|
||
private fun add(className: String, pos: Int) { | ||
val delimiterAt = className.indexOf('.', pos) | ||
if (delimiterAt == -1) { | ||
synchronized(this) { | ||
classes.add(className) | ||
} | ||
return | ||
} | ||
val pkg = className.substring(pos, delimiterAt) | ||
if (pkg !in children) { | ||
children[pkg] = TrieNode() | ||
} | ||
children[pkg]!!.add(className, delimiterAt + 1) | ||
} | ||
|
||
fun get(depth: Int = 0): List<String> { | ||
if (depth == 0) { | ||
return classes | ||
} | ||
return children.flatMap { it.value.get(depth - 1) } | ||
} | ||
|
||
fun search(packageName: String, depth: Int): List<String> { | ||
return search(packageName, depth, 0) | ||
} | ||
|
||
private fun search(packageName: String, depth: Int, pos: Int): List<String> { | ||
val delimiterAt = packageName.indexOf('.', pos) | ||
if (delimiterAt == -1) { | ||
val pkg = packageName.substring(pos) | ||
return children[pkg]?.get(depth) ?: emptyList() | ||
} | ||
val pkg = packageName.substring(pos, delimiterAt) | ||
return children[pkg]?.search(packageName, depth, delimiterAt + 1) ?: emptyList() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
388e25f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
大佬回来了牛皮 ,我可以帮忙翻译的~