Compare commits

...

2 Commits

Author SHA1 Message Date
xinian
4ea9864833 perf: 使用数组代替map优化元素计算性能
All checks were successful
ci/woodpecker/push/my-first-workflow Pipeline was successful
2026-04-06 07:16:57 +08:00
xinian
77057e01b6 refactor: 优化命令注册和请求处理逻辑 2026-04-06 07:07:15 +08:00
4 changed files with 196 additions and 158 deletions

View File

@@ -22,8 +22,14 @@ var ctx = context.TODO()
type Cmd struct {
Func reflect.Value //方法函数
Req reflect.Type //请求体
// HeaderFieldIndex 是请求结构体中 TomeeHeader 字段的索引路径。
HeaderFieldIndex []int
// UseConn 标记第二个参数是否为 gnet.Conn。
UseConn bool
// 新增预缓存的req创建函数返回结构体指针
NewReqFunc func() interface{}
// NewReqValue 返回请求结构体指针的 reflect.Value避免重复构造类型信息。
NewReqValue func() reflect.Value
//Res reflect.Value //返回体
}

View File

@@ -42,15 +42,15 @@ const (
maxMatrixSize = 227 // 矩阵维度覆盖最大属性ID 226
)
// 合法单属性ID集合快速校验
var validSingleElementIDs = map[int]bool{
// 合法单属性ID集合按ID直接索引避免运行时 map 查找
var validSingleElementIDs = [maxMatrixSize]bool{
1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true,
11: true, 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true, 20: true,
221: true, 222: true, 223: true, 224: true, 225: true, 226: true,
}
// 元素名称映射(全属性对应,便于日志输出)
var elementNameMap = map[ElementType]string{
// 元素名称映射(按ID直接索引,便于日志输出)
var elementNameMap = [maxMatrixSize]string{
ElementTypeGrass: "GRASS",
ElementTypeWater: "WATER",
ElementTypeFire: "FIRE",
@@ -198,46 +198,55 @@ type ElementCombination struct {
ID int // 组合唯一ID
}
// 全局预加载资源(程序启动时init初始化,运行时直接使用
// 全局预加载资源(程序启动时初始化,运行时只读
var (
// 元素组合池key=组合IDvalue=组合实例(预加载所有合法组合)
elementCombinationPool = make(map[int]*ElementCombination, 150) // 128双+26单=154预分配足够容量
// 单属性克制矩阵预初始化所有特殊克制关系默认1.0
matrix [maxMatrixSize][maxMatrixSize]float64
validCombinationIDs [maxMatrixSize]bool
elementCombinationPool [maxMatrixSize]ElementCombination
dualElementSecondaryPool [maxMatrixSize]ElementType
matrix [maxMatrixSize][maxMatrixSize]float64
Calculator *ElementCalculator
)
// init 预加载所有资源(程序启动时执行一次,无并发问题)
func init() {
// 1. 初始化单属性克制矩阵
initFullTableMatrix()
initElementCombinationPool()
Calculator = NewElementCalculator()
}
// 2. 预加载所有单属性组合
for id := range validSingleElementIDs {
combo := &ElementCombination{
Primary: ElementType(id),
Secondary: nil,
ID: id,
func initElementCombinationPool() {
for id, valid := range validSingleElementIDs {
if !valid {
continue
}
validCombinationIDs[id] = true
elementCombinationPool[id] = ElementCombination{
Primary: ElementType(id),
ID: id,
}
elementCombinationPool[id] = combo
}
// 3. 预加载所有双属性组合
for dualID, atts := range dualElementMap {
primaryID, secondaryID := atts[0], atts[1]
// 按ID升序排序保证组合一致性
primary, secondary := ElementType(primaryID), ElementType(secondaryID)
if primary > secondary {
primary, secondary = secondary, primary
}
combo := &ElementCombination{
dualElementSecondaryPool[dualID] = secondary
validCombinationIDs[dualID] = true
elementCombinationPool[dualID] = ElementCombination{
Primary: primary,
Secondary: &secondary,
Secondary: &dualElementSecondaryPool[dualID],
ID: dualID,
}
elementCombinationPool[dualID] = combo
}
}
func isValidCombinationID(id int) bool {
return id > 0 && id < maxMatrixSize && validCombinationIDs[id]
}
// IsDual 判断是否为双属性
func (ec *ElementCombination) IsDual() bool {
return ec.Secondary != nil
@@ -245,84 +254,82 @@ func (ec *ElementCombination) IsDual() bool {
// Elements 获取所有属性列表
func (ec *ElementCombination) Elements() []ElementType {
if ec.IsDual() {
return []ElementType{ec.Primary, *ec.Secondary}
if secondary := ec.Secondary; secondary != nil {
return []ElementType{ec.Primary, *secondary}
}
return []ElementType{ec.Primary}
}
// String 友好格式化输出
func (ec *ElementCombination) String() string {
primaryName := elementNameMap[ec.Primary]
if !ec.IsDual() {
return fmt.Sprintf("(%s)", primaryName)
if secondary := ec.Secondary; secondary != nil {
return fmt.Sprintf("(%s, %s)", elementNameMap[ec.Primary], elementNameMap[*secondary])
}
return fmt.Sprintf("(%s, %s)", primaryName, elementNameMap[*ec.Secondary])
return fmt.Sprintf("(%s)", elementNameMap[ec.Primary])
}
// ElementCalculator 无锁元素克制计算器(依赖预加载资源
// ElementCalculator 无锁元素克制计算器(所有倍数在初始化阶段预计算
type ElementCalculator struct {
offensiveCache map[string]float64 // 攻击克制缓存(运行时填充,无并发写)
offensiveTable [maxMatrixSize][maxMatrixSize]float64
}
// NewElementCalculator 创建计算器实例(仅初始化缓存)
// NewElementCalculator 创建计算器实例(构建只读查表缓存)
func NewElementCalculator() *ElementCalculator {
return &ElementCalculator{
offensiveCache: make(map[string]float64, 4096), // 预分配大容量缓存
c := &ElementCalculator{}
c.initOffensiveTable()
return c
}
func (c *ElementCalculator) initOffensiveTable() {
for attackerID, valid := range validCombinationIDs {
if !valid {
continue
}
attacker := &elementCombinationPool[attackerID]
for defenderID, valid := range validCombinationIDs {
if !valid {
continue
}
defender := &elementCombinationPool[defenderID]
c.offensiveTable[attackerID][defenderID] = c.calculateMultiplier(attacker, defender)
}
}
}
// getMatrixValue 直接返回矩阵值修复核心问题不再将0转换为1
func (c *ElementCalculator) getMatrixValue(attacker, defender ElementType) float64 {
return matrix[attacker][defender] // 矩阵默认已初始化1.0,特殊值直接返回
return matrix[attacker][defender]
}
// GetCombination 获取元素组合(直接从预加载池读取
// GetCombination 获取元素组合(直接按ID索引
func (c *ElementCalculator) GetCombination(id int) (*ElementCombination, error) {
combo, exists := elementCombinationPool[id]
if !exists {
if !isValidCombinationID(id) {
return nil, fmt.Errorf("invalid element combination ID: %d", id)
}
return combo, nil
return &elementCombinationPool[id], nil
}
// GetOffensiveMultiplier 计算攻击方→防御方的克制倍数(缓存优先
// GetOffensiveMultiplier 计算攻击方→防御方的克制倍数(只读查表
func (c *ElementCalculator) GetOffensiveMultiplier(attackerID, defenderID int) (float64, error) {
// 1. 获取预加载的组合实例
attacker, err := c.GetCombination(attackerID)
if err != nil {
return 0, fmt.Errorf("attacker invalid: %w", err)
if !isValidCombinationID(attackerID) {
return 0, fmt.Errorf("attacker invalid: invalid element combination ID: %d", attackerID)
}
defender, err := c.GetCombination(defenderID)
if err != nil {
return 0, fmt.Errorf("defender invalid: %w", err)
if !isValidCombinationID(defenderID) {
return 0, fmt.Errorf("defender invalid: invalid element combination ID: %d", defenderID)
}
// 2. 缓存键(全局唯一)
cacheKey := fmt.Sprintf("a%d_d%d", attackerID, defenderID)
if val, exists := c.offensiveCache[cacheKey]; exists {
return val, nil
}
// 3. 核心计算+缓存
val := c.calculateMultiplier(attacker, defender)
c.offensiveCache[cacheKey] = val
return val, nil
return c.offensiveTable[attackerID][defenderID], nil
}
// calculateMultiplier 核心克制计算逻辑
func (c *ElementCalculator) calculateMultiplier(attacker, defender *ElementCombination) float64 {
// 场景1单→单
if !attacker.IsDual() && !defender.IsDual() {
return c.getMatrixValue(attacker.Primary, defender.Primary)
}
// 场景2单→双
if !attacker.IsDual() {
y1, y2 := defender.Primary, *defender.Secondary
m1 := c.getMatrixValue(attacker.Primary, y1)
m2 := c.getMatrixValue(attacker.Primary, y2)
switch {
case m1 == 2 && m2 == 2:
return 4.0
@@ -333,12 +340,10 @@ func (c *ElementCalculator) calculateMultiplier(attacker, defender *ElementCombi
}
}
// 场景3双→单
if !defender.IsDual() {
return c.calculateDualToSingle(attacker.Primary, *attacker.Secondary, defender.Primary)
}
// 场景4双→双
x1, x2 := attacker.Primary, *attacker.Secondary
y1, y2 := defender.Primary, *defender.Secondary
coeffY1 := c.calculateDualToSingle(x1, x2, y1)
@@ -350,7 +355,6 @@ func (c *ElementCalculator) calculateMultiplier(attacker, defender *ElementCombi
func (c *ElementCalculator) calculateDualToSingle(attacker1, attacker2, defender ElementType) float64 {
k1 := c.getMatrixValue(attacker1, defender)
k2 := c.getMatrixValue(attacker2, defender)
switch {
case k1 == 2 && k2 == 2:
return 4.0
@@ -361,60 +365,49 @@ func (c *ElementCalculator) calculateDualToSingle(attacker1, attacker2, defender
}
}
var Calculator = NewElementCalculator()
// TestAllScenarios 全场景测试(验证预加载和计算逻辑)
func TestAllScenarios() {
// 测试1单→单草→水
m1, _ := Calculator.GetOffensiveMultiplier(1, 2)
fmt.Println("草→水: %.2f预期2.0", m1)
if math.Abs(m1-2.0) > 0.001 {
fmt.Println("测试1失败实际%.2f", m1)
}
// 测试2特殊单→单混沌→虚空
m2, _ := Calculator.GetOffensiveMultiplier(222, 226)
fmt.Println("混沌→虚空: %.2f预期0.0", m2)
if math.Abs(m2-0.0) > 0.001 {
fmt.Println("测试2失败实际%.2f", m2)
}
// 测试3单→双火→冰龙43
m3, _ := Calculator.GetOffensiveMultiplier(3, 43)
fmt.Println("火→冰龙: %.2f预期1.5", m3)
if math.Abs(m3-1.5) > 0.001 {
fmt.Println("测试3失败实际%.2f", m3)
}
// 测试4双→特殊单混沌暗影92→神灵223
m4, _ := Calculator.GetOffensiveMultiplier(92, 223)
fmt.Println("混沌暗影→神灵: %.2f预期1.25", m4)
if math.Abs(m4-1.25) > 0.001 {
fmt.Println("测试4失败实际%.2f", m4)
}
// 测试5双→双虚空邪灵113→混沌远古98
m5, _ := Calculator.GetOffensiveMultiplier(113, 98)
fmt.Println("虚空邪灵→混沌远古: %.2f预期0.875", m5)
if math.Abs(m5-0.875) > 0.001 {
fmt.Println("测试5失败实际%.2f", m5)
}
// 测试6缓存命中
m6, _ := Calculator.GetOffensiveMultiplier(113, 98)
if math.Abs(m6-m5) > 0.001 {
fmt.Println("测试6失败缓存未命中")
}
// 测试7含无效组合电→地面
m7, _ := Calculator.GetOffensiveMultiplier(5, 7)
fmt.Println("电→地面: %.2f预期0.0", m7)
if math.Abs(m7-0.0) > 0.001 {
fmt.Println("测试7失败实际%.2f", m7)
}
// 测试8双属性含无效电战斗→地面
m8, _ := Calculator.GetOffensiveMultiplier(35, 7)
fmt.Println("电战斗→地面: %.2f预期0.25", m8)
if math.Abs(m8-0.25) > 0.001 {

View File

@@ -16,6 +16,7 @@ import (
"github.com/gogf/gf/v2/os/glog"
"github.com/lunixbochs/struc"
"github.com/panjf2000/gnet/v2"
)
// Maincontroller 是控制器层共享变量。
@@ -45,19 +46,14 @@ func ParseCmd[T any](data []byte) T {
// Init 初始化控制器注册所有cmd处理方法
// 参数 isGame: 标识是否为游戏服务器(true)或登录服务器(false)
func Init(isGame bool) {
// 获取控制器实例的反射值
controllerValue := reflect.ValueOf(Maincontroller)
// 获取控制器类型
controllerType := controllerValue.Type()
// 遍历控制器的所有方法
for i := 0; i < controllerType.NumMethod(); i++ {
method := controllerType.Method(i)
methodValue := controllerValue.MethodByName(method.Name)
methodValue := controllerValue.Method(i)
methodType := methodValue.Type()
// 获取方法第一个参数的类型(请求结构体)
if methodType.NumIn() == 0 {
continue
}
@@ -68,43 +64,46 @@ func Init(isGame bool) {
continue
}
reqType := reqArgType.Elem()
binding := getCmdBinding(reqType)
// 解析请求结构体中的cmd标签
for _, cmd := range getCmd(reqType) {
if cmd == 0 { // 说明不是有效的注册方法
for _, cmd := range binding.cmds {
if cmd == 0 {
glog.Warning(context.Background(), "方法参数必须包含CMD参数", method.Name, "跳过注册")
continue
}
// 根据服务器类型过滤cmd
// 登录服务器只处理小于1000的cmd
if methodType.NumIn() != 2 {
glog.Warning(context.Background(), "方法参数数量必须为2", method.Name, "跳过注册")
continue
}
if !isGame && cmd > 1000 {
continue
}
// 游戏服务器只处理大于等于1000的cmd
if isGame && cmd < 1000 {
continue
}
// 注册命令处理函数
if cool.Config.ServerInfo.IsDebug != 0 {
fmt.Println("注册方法", cmd, method.Name)
}
cmdInfo := cool.Cmd{
Func: methodValue,
Req: reqType,
// Res: , // TODO 待实现对不同用户初始化方法以取消全局cmdcache
}
// 预编译创建req实例的函数返回结构体指针
reqTypeForNew := reqType
cmdInfo.NewReqFunc = func() interface{} {
return reflect.New(reqTypeForNew).Interface()
cmdInfo := cool.Cmd{
Func: methodValue,
Req: reqType,
HeaderFieldIndex: append([]int(nil), binding.headerFieldIndex...),
UseConn: methodType.In(1) == connType,
NewReqFunc: func() interface{} {
return reflect.New(reqTypeForNew).Interface()
},
NewReqValue: func() reflect.Value {
return reflect.New(reqTypeForNew)
},
}
if _, exists := cool.CmdCache[cmd]; exists { // 方法已存在
if _, exists := cool.CmdCache[cmd]; exists {
panic(fmt.Sprintf("命令处理方法已存在,跳过注册 %d %s", cmd, method.Name))
}
cool.CmdCache[cmd] = cmdInfo
@@ -112,12 +111,20 @@ func Init(isGame bool) {
}
}
var targetType = reflect.TypeOf(common.TomeeHeader{})
var cmdTypeCache sync.Map
var (
targetType = reflect.TypeOf(common.TomeeHeader{})
connType = reflect.TypeOf((*gnet.Conn)(nil)).Elem()
cmdTypeCache sync.Map
)
// 默认返回值(无匹配字段/解析失败时)
const defaultCmdValue = 0
type cmdBinding struct {
cmds []uint32
headerFieldIndex []int
}
func normalizeStructType(typ reflect.Type) reflect.Type {
for typ.Kind() == reflect.Ptr {
typ = typ.Elem()
@@ -125,92 +132,93 @@ func normalizeStructType(typ reflect.Type) reflect.Type {
return typ
}
// getCmd 从结构体类型中提取绑定的cmd指令(递归查找嵌套结构体,支持值/指针类型的TomeeHeader
// 参数 typ: 待解析的结构体类型(支持多层指针)
// 返回值: 解析到的cmd切片无匹配/解析失败时返回[defaultCmdValue]
func getCmd(typ reflect.Type) []uint32 {
// getCmdBinding 从结构体类型中提取绑定的cmd指令和头字段位置。
func getCmdBinding(typ reflect.Type) cmdBinding {
typ = normalizeStructType(typ)
if cached, ok := cmdTypeCache.Load(typ); ok {
return cached.([]uint32)
return cached.(cmdBinding)
}
// 非结构体类型直接返回默认值
if typ.Kind() != reflect.Struct {
return []uint32{defaultCmdValue}
binding := cmdBinding{cmds: []uint32{defaultCmdValue}}
cmdTypeCache.Store(typ, binding)
return binding
}
if cmd, ok := findCmd(typ, make(map[reflect.Type]struct{})); ok {
cmdTypeCache.Store(typ, cmd)
return cmd
if binding, ok := findCmdBinding(typ, make(map[reflect.Type]struct{})); ok {
cmdTypeCache.Store(typ, binding)
return binding
}
// 未找到目标字段/所有解析失败,返回默认值
defaultCmd := []uint32{defaultCmdValue}
cmdTypeCache.Store(typ, defaultCmd)
return defaultCmd
binding := cmdBinding{cmds: []uint32{defaultCmdValue}}
cmdTypeCache.Store(typ, binding)
return binding
}
func findCmd(typ reflect.Type, visiting map[reflect.Type]struct{}) ([]uint32, bool) {
func findCmdBinding(typ reflect.Type, visiting map[reflect.Type]struct{}) (cmdBinding, bool) {
typ = normalizeStructType(typ)
if typ.Kind() != reflect.Struct {
return nil, false
return cmdBinding{}, false
}
if _, seen := visiting[typ]; seen {
return nil, false
return cmdBinding{}, false
}
visiting[typ] = struct{}{}
defer delete(visiting, typ)
// 遍历结构体字段查找TomeeHeader字段并解析cmd
for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)
// 尝试解析当前字段的cmd标签
cmdSlice, isHeader, err := parseCmdTagWithStructField(field)
if isHeader && err == nil { // 解析成功,直接返回结果
return cmdSlice, true
if isHeader && err == nil {
return cmdBinding{
cmds: cmdSlice,
headerFieldIndex: append([]int(nil), field.Index...),
}, true
}
// 递归处理嵌套结构体(值/指针类型)
nestedTyp := normalizeStructType(field.Type)
if nestedTyp.Kind() == reflect.Struct {
// 递归查找找到有效cmd则立即返回
if nestedCmd, ok := findCmd(nestedTyp, visiting); ok {
return nestedCmd, true
}
if nestedTyp.Kind() != reflect.Struct {
continue
}
nestedBinding, ok := findCmdBinding(nestedTyp, visiting)
if !ok {
continue
}
fieldIndex := make([]int, 0, len(field.Index)+len(nestedBinding.headerFieldIndex))
fieldIndex = append(fieldIndex, field.Index...)
fieldIndex = append(fieldIndex, nestedBinding.headerFieldIndex...)
nestedBinding.headerFieldIndex = fieldIndex
return nestedBinding, true
}
return nil, false
return cmdBinding{}, false
}
// parseCmdTagWithStructField 校验字段是否为TomeeHeader值/指针并解析cmd标签
// 参数 field: 结构体字段元信息
// 返回值: 解析后的cmd切片是否为目标类型解析失败错误
func parseCmdTagWithStructField(field reflect.StructField) ([]uint32, bool, error) {
// 判断字段类型是否为 TomeeHeader 或 *TomeeHeader
if field.Type != targetType && !(field.Type.Kind() == reflect.Ptr && field.Type.Elem() == targetType) {
return nil, false, nil
}
// 提取cmd标签
cmdStr := field.Tag.Get("cmd")
if cmdStr == "" {
return nil, true, fmt.Errorf("field %s cmd tag is empty", field.Name)
}
// 高性能解析标签为uint32切片替代gconv减少第三方依赖且可控
result := make([]uint32, 0, strings.Count(cmdStr, "|")+1)
remain := cmdStr
for idx := 0; ; idx++ {
part, next, found := strings.Cut(remain, "|")
// 去除空白字符(兼容标签中意外的空格)
s := strings.TrimSpace(part)
if s == "" {
return nil, true, fmt.Errorf("field %s cmd tag part %d is empty", field.Name, idx)
}
// 手动解析uint32比gconv更可控避免隐式转换问题
num, err := strconv.ParseUint(s, 10, 32)
if err != nil {
return nil, true, fmt.Errorf("field %s cmd tag part %d parse error: %v (value: %s)",

View File

@@ -53,6 +53,44 @@ func getUnderlyingValue(val reflect.Value) (reflect.Value, error) {
}
}
func setFieldByIndex(root reflect.Value, index []int, value reflect.Value) bool {
current := root
for pos, idx := range index {
if current.Kind() == reflect.Ptr {
if current.IsNil() {
current.Set(reflect.New(current.Type().Elem()))
}
current = current.Elem()
}
if current.Kind() != reflect.Struct || idx < 0 || idx >= current.NumField() {
return false
}
field := current.Field(idx)
if pos == len(index)-1 {
if !field.CanSet() {
return false
}
if value.Type().AssignableTo(field.Type()) {
field.Set(value)
return true
}
if field.Kind() == reflect.Ptr && value.Type().AssignableTo(field.Type().Elem()) {
ptr := reflect.New(field.Type().Elem())
ptr.Elem().Set(value)
field.Set(ptr)
return true
}
return false
}
current = field
}
return false
}
// XORDecryptU 原地执行异或解密,避免额外分配和拷贝。
func XORDecryptU(encryptedData []byte, key uint32) []byte {
if len(encryptedData) == 0 {
@@ -193,45 +231,38 @@ func (h *ClientData) OnEvent(data common.TomeeHeader) {
return //TODO 待实现cmd未注册
}
params := []reflect.Value{}
var ptrValue reflect.Value
if cmdlister.NewReqValue != nil {
ptrValue = cmdlister.NewReqValue()
} else {
ptrValue = reflect.New(cmdlister.Req)
}
//funct := cmdlister.Type().NumIn()
// 如果需要可设置的变量(用于修改值),创建指针并解引用
ptrValue := reflect.New(cmdlister.Req)
// fmt.Println(tt1)
if data.Res != nil {
tt1 := ptrValue.Elem().Addr().Interface()
err := struc.Unpack(bytes.NewBuffer(data.Res), tt1)
err := struc.Unpack(bytes.NewBuffer(data.Res), ptrValue.Interface())
if err != nil {
cool.Logger.Error(context.Background(), data.UserID, data.CMD, "解包失败,", err, hex.EncodeToString(data.Res))
//fmt.Println(data.UserID, data.CMD, "解包失败,", hex.EncodeToString(data.Data))
data.Result = uint32(errorcode.ErrorCodes.ErrSystemProcessingError)
h.SendPack(data.Pack(nil))
return
}
}
ptrValue1 := ptrValue.Elem().Addr()
// 设置 Name 字段
nameField := ptrValue.Elem().Field(0) //首个为header
nameField.Set(reflect.ValueOf(data))
if data.CMD > 1001 { //if cmdlister.Type().In(1) == reflect.TypeOf(&Player{}) {
//t := GetPlayer(c, data.UserID)
// fmt.Println(data.CMD, "接收 变量的地址 ", &t.Info, t.Info.UserID)
params = append(params, ptrValue1, reflect.ValueOf(h.Player))
} else {
params = append(params, ptrValue1, reflect.ValueOf(h.Conn))
if !setFieldByIndex(ptrValue.Elem(), cmdlister.HeaderFieldIndex, reflect.ValueOf(data)) {
cool.Logger.Warning(context.Background(), data.UserID, data.CMD, "设置请求头失败")
return
}
ret := cmdlister.Func.Call(params)
var params [2]reflect.Value
params[0] = ptrValue
if cmdlister.UseConn {
params[1] = reflect.ValueOf(h.Conn)
} else {
params[1] = reflect.ValueOf(h.Player)
}
ret := cmdlister.Func.Call(params[:])
if len(ret) <= 0 { //如果判断没有参数,那就说明这个包没有返回参数
return