将实验目录 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 处行为偏差。
295 lines
6.5 KiB
Go
295 lines
6.5 KiB
Go
package reflux
|
|
|
|
// 新实现(rfx: 类型布局缓存 + unsafe 指针运算)与老实现(refx: 逐次 reflect)的对比。
|
|
//
|
|
// 两者在同一个包、同一次进程里跑, 用完全相同的数据结构和路径, 结果可直接比较。
|
|
// newLegacy 构造的就是合并前的实现, 见 rfx_contract_test.go。
|
|
|
|
import "testing"
|
|
|
|
type benchAddress struct {
|
|
City string
|
|
Street string
|
|
ZipCode int
|
|
}
|
|
|
|
type benchPerson struct {
|
|
Name string
|
|
Age int
|
|
Email string
|
|
Address benchAddress
|
|
Tags []string
|
|
Meta map[string]string
|
|
}
|
|
|
|
func newBenchPerson() *benchPerson {
|
|
return &benchPerson{
|
|
Name: "Benchmark",
|
|
Age: 30,
|
|
Address: benchAddress{City: "TestCity", ZipCode: 100000},
|
|
Tags: []string{"a", "b", "c"},
|
|
Meta: map[string]string{"k": "v"},
|
|
}
|
|
}
|
|
|
|
type benchDeep4 struct{ B benchDeep3 }
|
|
type benchDeep3 struct{ C benchDeep2 }
|
|
type benchDeep2 struct{ D benchDeep1 }
|
|
type benchDeep1 struct{ Leaf string }
|
|
|
|
var (
|
|
benchStr string
|
|
benchInt int
|
|
benchR R
|
|
benchBool bool
|
|
)
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// 参照基准线: 纯 Go 字段访问
|
|
// ---------------------------------------------------------------------------
|
|
|
|
func BenchmarkPlainGoGet(b *testing.B) {
|
|
p := newBenchPerson()
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
benchStr = p.Address.City
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Get: 嵌套字段
|
|
// ---------------------------------------------------------------------------
|
|
|
|
func BenchmarkGetLegacy(b *testing.B) {
|
|
r := newLegacy(newBenchPerson())
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
benchStr = r.Get("Address", "City").String()
|
|
}
|
|
}
|
|
|
|
func BenchmarkGetNew(b *testing.B) {
|
|
r := New(newBenchPerson())
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
benchStr = r.Get("Address", "City").String()
|
|
}
|
|
}
|
|
|
|
func BenchmarkGetGeneric(b *testing.B) {
|
|
r := New(newBenchPerson())
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
benchStr = Get[string](r, "Address", "City")
|
|
}
|
|
}
|
|
|
|
// 点号路径
|
|
func BenchmarkGetDottedLegacy(b *testing.B) {
|
|
r := newLegacy(newBenchPerson())
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
benchStr = r.Get("Address.City").String()
|
|
}
|
|
}
|
|
|
|
func BenchmarkGetDottedNew(b *testing.B) {
|
|
r := New(newBenchPerson())
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
benchStr = r.Get("Address.City").String()
|
|
}
|
|
}
|
|
|
|
// 4 层深路径
|
|
func BenchmarkGetDeepLegacy(b *testing.B) {
|
|
r := newLegacy(&benchDeep4{B: benchDeep3{C: benchDeep2{D: benchDeep1{Leaf: "deep"}}}})
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
benchStr = r.Get("B", "C", "D", "Leaf").String()
|
|
}
|
|
}
|
|
|
|
func BenchmarkGetDeepNew(b *testing.B) {
|
|
r := New(&benchDeep4{B: benchDeep3{C: benchDeep2{D: benchDeep1{Leaf: "deep"}}}})
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
benchStr = r.Get("B", "C", "D", "Leaf").String()
|
|
}
|
|
}
|
|
|
|
func BenchmarkGetDeepGeneric(b *testing.B) {
|
|
r := New(&benchDeep4{B: benchDeep3{C: benchDeep2{D: benchDeep1{Leaf: "deep"}}}})
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
benchStr = Get[string](r, "B", "C", "D", "Leaf")
|
|
}
|
|
}
|
|
|
|
// slice 下标
|
|
func BenchmarkGetSliceIndexLegacy(b *testing.B) {
|
|
r := newLegacy(newBenchPerson())
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
benchStr = r.Get("Tags", "1").String()
|
|
}
|
|
}
|
|
|
|
func BenchmarkGetSliceIndexNew(b *testing.B) {
|
|
r := New(newBenchPerson())
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
benchStr = r.Get("Tags", "1").String()
|
|
}
|
|
}
|
|
|
|
// map 键
|
|
func BenchmarkGetMapKeyLegacy(b *testing.B) {
|
|
r := newLegacy(newBenchPerson())
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
benchStr = r.Get("Meta", "k").String()
|
|
}
|
|
}
|
|
|
|
func BenchmarkGetMapKeyNew(b *testing.B) {
|
|
r := New(newBenchPerson())
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
benchStr = r.Get("Meta", "k").String()
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Set
|
|
// ---------------------------------------------------------------------------
|
|
|
|
func BenchmarkSetLegacy(b *testing.B) {
|
|
r := newLegacy(&benchPerson{})
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
r.Set("Name", "TestName")
|
|
}
|
|
}
|
|
|
|
func BenchmarkSetNew(b *testing.B) {
|
|
r := New(&benchPerson{})
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
r.Set("Name", "TestName")
|
|
}
|
|
}
|
|
|
|
func BenchmarkSetNestedLegacy(b *testing.B) {
|
|
r := newLegacy(&benchPerson{})
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
r.Set("Address.City", "TestCity")
|
|
}
|
|
}
|
|
|
|
func BenchmarkSetNestedNew(b *testing.B) {
|
|
r := New(&benchPerson{})
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
r.Set("Address.City", "TestCity")
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// 访问器: 纯类型转换开销(循环外先取到目标值)
|
|
// ---------------------------------------------------------------------------
|
|
|
|
func BenchmarkAccessorStringLegacy(b *testing.B) {
|
|
v := newLegacy(newBenchPerson()).Get("Address.City")
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
benchStr = v.String()
|
|
}
|
|
}
|
|
|
|
func BenchmarkAccessorStringNew(b *testing.B) {
|
|
v := New(newBenchPerson()).Get("Address.City")
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
benchStr = v.String()
|
|
}
|
|
}
|
|
|
|
func BenchmarkAccessorIntLegacy(b *testing.B) {
|
|
v := newLegacy(newBenchPerson()).Get("Age")
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
benchInt = v.Int()
|
|
}
|
|
}
|
|
|
|
func BenchmarkAccessorIntNew(b *testing.B) {
|
|
v := New(newBenchPerson()).Get("Age")
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
benchInt = v.Int()
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// 其它 API
|
|
// ---------------------------------------------------------------------------
|
|
|
|
func BenchmarkExistsLegacy(b *testing.B) {
|
|
r := newLegacy(newBenchPerson())
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
benchBool = r.Exists("Address", "City")
|
|
}
|
|
}
|
|
|
|
func BenchmarkExistsNew(b *testing.B) {
|
|
r := New(newBenchPerson())
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
benchBool = r.Exists("Address", "City")
|
|
}
|
|
}
|
|
|
|
func BenchmarkNewPtrLegacy(b *testing.B) {
|
|
p := newBenchPerson()
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
benchR = newLegacy(p)
|
|
}
|
|
}
|
|
|
|
func BenchmarkNewPtrNew(b *testing.B) {
|
|
p := newBenchPerson()
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
benchR = New(p)
|
|
}
|
|
}
|