perf: 用类型布局缓存 + 指针偏移寻址重写热路径
将实验目录 experiment/fastx 的实现合入主实现,替换原来逐次反射的做法。 实现方式: - 类型布局缓存: 首次遇到某类型时把字段偏移量构建成描述符存入全局缓存, 字段查找从按名线性比较变成 O(1) map 查表 (rfx_typedesc.go) - 指针偏移寻址: 取字段时用 基址+偏移量 直接算地址, 不再构造中间 reflect.Value (unsafeptr.go, 全部 unsafe 代码集中在这一个文件) - 零分配路径解析: 路径按需切片遍历, 不再为每次 Get 分配临时切片 (path.go) - 标量直读: String/Int/Bool/Float64 在类型匹配时直接按机器类型读内存, 绕开 interface 装箱和 cast 转换 语义复杂、调用频次低的操作(复合类型赋值、Append、Delete、容器转换)仍走原 reflect 实现, 保留为冷路径 refx (rfx_reflect.go), 避免重写引入行为偏差。 新增包级泛型函数 Get[T](r, path...), 按路径直接取出目标类型, 零内存分配。 语义与 r.Get(path...).Xxx() 严格等价, 不改动任何现有接口。 性能对比(同进程同数据, -benchmem -count=6 中位数): - Get 嵌套 + String 150.6ns/7allocs -> 50.7ns/2allocs 2.97x - Get 4 层深路径 234.1ns/10allocs -> 68.9ns/2allocs 3.40x - Set 单层 65.8ns/2allocs -> 18.1ns/0allocs 3.64x - Set 嵌套 122.1ns/3allocs -> 35.5ns/0allocs 3.44x - Exists 127.4ns/5allocs -> 37.2ns/1alloc 3.42x - 访问器 String 15.0ns/1alloc -> 1.3ns/0allocs 11.8x - Get[string] 泛型直取 -> 29.9ns/0allocs 5.04x 两处不快: map 键访问 1.25x(map 无稳定布局, 仍走 reflect); New 构造 0.67x(多一次描述符缓存查找, 一次性成本)。 内存安全: 未导出字段在读和写两处显式拦截 —— reflect.NewAt 构造的 Value 不带只读标记, 语言层面的导出规则保护在 unsafe 路径上失效, 必须自己拦。 不变式与评审要点见 unsafeptr.go 顶部注释。 测试: 新增 703 个用例全部通过, 含 go test -race。 其中接口契约逐方法对拍(含 panic 错误信息逐字比对)、标量转换全矩阵对拍、 []any 内嵌 R、循环引用等边角场景, 均以保留下来的 refx 作为参照实现做差分验证。 合并过程中据此发现并修复 11 处行为偏差。
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
package reflux
|
||||
|
||||
import "strings"
|
||||
|
||||
// pathIter 在不分配内存的前提下遍历路径片段。
|
||||
//
|
||||
// reflux 现有的 expandPath 会对每个片段做 strings.Split 并 append 到一个新切片,
|
||||
// Get("Address","City") 这样一次调用就要分配 4 次(2 次 Split 的结果切片 +
|
||||
// append 扩容 2 次)。pathIter 只做字符串切片(sub-slice 不分配)。
|
||||
type pathIter struct {
|
||||
parts []string
|
||||
// one 用于"路径只有一个字符串"的场景(Set/Delete 的 key 参数)。
|
||||
// 走这条分支可以避免临时构造 []string{key} 带来的堆分配。
|
||||
one string
|
||||
isOne bool
|
||||
i int // 当前处理到第几个片段
|
||||
off int // 在当前片段内部的字节偏移
|
||||
n int // 已经产出的段数
|
||||
limit int // 最多产出多少段, -1 表示不限制
|
||||
}
|
||||
|
||||
func newPathIter(parts []string) pathIter {
|
||||
return pathIter{parts: parts, limit: -1}
|
||||
}
|
||||
|
||||
func (it *pathIter) numParts() int {
|
||||
if it.isOne {
|
||||
return 1
|
||||
}
|
||||
return len(it.parts)
|
||||
}
|
||||
|
||||
func (it *pathIter) partAt(i int) string {
|
||||
if it.isOne {
|
||||
return it.one
|
||||
}
|
||||
return it.parts[i]
|
||||
}
|
||||
|
||||
// next 返回下一个非空路径段。空段(连续点号、前后缀点号)会被跳过,
|
||||
// 与 reflux expandPath 忽略空字符串的行为一致。
|
||||
func (it *pathIter) next() (string, bool) {
|
||||
if it.limit >= 0 && it.n >= it.limit {
|
||||
return "", false
|
||||
}
|
||||
for it.i < it.numParts() {
|
||||
s := it.partAt(it.i)
|
||||
if it.off >= len(s) {
|
||||
it.i++
|
||||
it.off = 0
|
||||
continue
|
||||
}
|
||||
rest := s[it.off:]
|
||||
if j := strings.IndexByte(rest, '.'); j >= 0 {
|
||||
it.off += j + 1
|
||||
if j == 0 {
|
||||
continue // 空段, 跳过
|
||||
}
|
||||
it.n++
|
||||
return rest[:j], true
|
||||
}
|
||||
it.i++
|
||||
it.off = 0
|
||||
it.n++
|
||||
return rest, true
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
func parseIndex(s string) (int, bool) {
|
||||
if len(s) == 0 || len(s) > 18 {
|
||||
return 0, false
|
||||
}
|
||||
neg := false
|
||||
i := 0
|
||||
if s[0] == '-' {
|
||||
neg = true
|
||||
i = 1
|
||||
if len(s) == 1 {
|
||||
return 0, false
|
||||
}
|
||||
}
|
||||
n := 0
|
||||
for ; i < len(s); i++ {
|
||||
c := s[i]
|
||||
if c < '0' || c > '9' {
|
||||
return 0, false
|
||||
}
|
||||
n = n*10 + int(c-'0')
|
||||
}
|
||||
if neg {
|
||||
n = -n
|
||||
}
|
||||
return n, true
|
||||
}
|
||||
|
||||
// splitLastStr 是 splitLast 的单字符串版本, 避免为了调用 splitLast 而临时
|
||||
// 构造一个 []string(那会带来一次堆分配 —— Set 的热路径上不可接受)。
|
||||
func splitLastStr(key string) (parent pathIter, last string, ok bool) {
|
||||
n := 0
|
||||
i := 0
|
||||
for i < len(key) {
|
||||
j := i
|
||||
for j < len(key) && key[j] != '.' {
|
||||
j++
|
||||
}
|
||||
if j > i {
|
||||
last = key[i:j]
|
||||
n++
|
||||
}
|
||||
i = j + 1
|
||||
}
|
||||
if n == 0 {
|
||||
return pathIter{}, "", false
|
||||
}
|
||||
return pathIter{one: key, isOne: true, limit: n - 1}, last, true
|
||||
}
|
||||
Reference in New Issue
Block a user