Skip to content

Commit 57b5a2d

Browse files
committed
perf: 诊断埋点支持运行时关闭 + inflateAsync 复用 mainHandler
- PoolStats/InflateTracker 加 @volatile var enabled,关闭后热路径零额外开销 - FastInflater.setMetricsEnabled() 一键切两个开关 - inflateAsync 复用缓存的 mainHandler,省每次 fallback 的 Handler 分配 - README 新增'关闭埋点'章节
1 parent bbb99f0 commit 57b5a2d

3 files changed

Lines changed: 35 additions & 0 deletions

File tree

‎README.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,21 @@ FastInflater.get().autoTune()
155155
FastInflater.get().setMaxPoolSize(R.layout.item_feed, 8)
156156
```
157157

158+
### 关闭埋点(可选)
159+
160+
诊断埋点(`InflateTracker` + `PoolStats`)默认开启——这是这个库的核心诊断价值。调优完成、上线后如果想彻底消除热路径上的 `nanoTime` / 原子自增 / HashMap 查询开销,可以一键关闭:
161+
162+
```kotlin
163+
// 一键关闭两个埋点
164+
FastInflater.get().setMetricsEnabled(false)
165+
166+
// 或单独控制
167+
InflateTracker.enabled = false
168+
PoolStats.enabled = false
169+
```
170+
171+
关闭后 `inflate` 池命中是纯 poll,miss 是纯 `LayoutInflater.inflate`,没有任何额外计算。
172+
158173
### 主线程依赖的布局
159174

160175
部分 View 不能后台 inflate(ComposeView、WebView、含 LiveData 的自定义 View)。FastInflater 会自动检测并降级到主线程 IdleHandler,也可以预先标记:

‎lib/src/main/java/com/github/donglua/fastinflater/InflateTracker.kt‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ import java.util.concurrent.atomic.AtomicLong
77

88
object InflateTracker {
99

10+
/**
11+
* 全局开关。默认开启——耗时追踪是这个库的核心诊断价值。
12+
* 调优完成后可关闭,彻底消除热路径上的 nanoTime / HashMap / CAS 开销。
13+
*
14+
* 关闭后 [track] 仅调用 block(),不计时;[recordInflate] 直接 return。
15+
*/
16+
@Volatile
17+
var enabled: Boolean = true
18+
1019
private val stats = ConcurrentHashMap<Int, LayoutStat>()
1120
private var reporter: ((Map<Int, LayoutStat>) -> Unit)? = null
1221

@@ -15,6 +24,7 @@ object InflateTracker {
1524
}
1625

1726
fun recordInflate(@LayoutRes layoutId: Int, durationNs: Long) {
27+
if (!enabled) return
1828
val stat = stats.getOrPut(layoutId) { LayoutStat() }
1929
stat.count.incrementAndGet()
2030
stat.totalNs.addAndGet(durationNs)
@@ -43,6 +53,7 @@ object InflateTracker {
4353
}
4454

4555
inline fun <T> track(@LayoutRes layoutId: Int, block: () -> T): T {
56+
if (!enabled) return block()
4657
val start = System.nanoTime()
4758
val result = block()
4859
recordInflate(layoutId, System.nanoTime() - start)

‎lib/src/main/java/com/github/donglua/fastinflater/PoolStats.kt‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,24 @@ import java.util.concurrent.atomic.AtomicLong
2323
*/
2424
object PoolStats {
2525

26+
/**
27+
* 全局开关。默认开启——命中率数据是这个库的核心诊断价值。
28+
* 调优完成、上线后可以关掉以彻底消除热路径上的原子自增和 HashMap 查询。
29+
*/
30+
@Volatile
31+
var enabled: Boolean = true
32+
2633
private val global = Stat()
2734
private val perLayout = ConcurrentHashMap<Int, Stat>()
2835

2936
internal fun recordHit(@LayoutRes layoutId: Int) {
37+
if (!enabled) return
3038
global.hits.incrementAndGet()
3139
perLayout.getOrPut(layoutId) { Stat() }.hits.incrementAndGet()
3240
}
3341

3442
internal fun recordMiss(@LayoutRes layoutId: Int) {
43+
if (!enabled) return
3544
global.misses.incrementAndGet()
3645
perLayout.getOrPut(layoutId) { Stat() }.misses.incrementAndGet()
3746
}

0 commit comments

Comments
 (0)