We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
我注意到,在调用React的前后,有判断action与goto进行loop循环调用,我们大家都知道,尽量避免goto调用,还有这里的action判断逻辑有点混乱不直观,让人难以理解什么时候goto
loopReact: out, action := lp.eventHandler.React(c) if out != nil { if frame, err := lp.codec.Encode(c, out); err == nil { c.write(frame) } } switch c.action == action { case true: goto loopReact case false: c.action = action }
深入阅读代码之后,发现这里的loop是为了循环调用ReadFrame(),来实现每次React只处理一帧有效数据,但是React里面还要二次判断是否为有效帧,实为浪费性能。 因此觉得优化此处代码取消goto,有以下两种方案: Plan A: 在业务层内实现for循环遍历每一帧数据
func (hs *httpServer) React(c gnet.Conn) (out []byte, action gnet.Action) { for _, data := range c.ReadFrame() { ... } }
Plan B: 在React上层,for循环分好data数据
通过此样修改之后,可以取消所有关于 c.action = Skip 的判断与代码,Plan B可以收缩减少gnet暴露的接口,增加代码性能与可读性,减少React的代码量。
The text was updated successfully, but these errors were encountered:
Cyclomatic complexity of the plan b code maybe high, if in if is not good code smell
Sorry, something went wrong.
@KaiwenWang 抱歉,没太懂你的意思?能仔细说下你的想法吗?
搜一下代码圈复杂度的概念。层层嵌套,如for if if 可以认为是bad smell 并且是 潜在bug的根源
我知道,我还以为你是对具体的逻辑有什么担忧,关于代码的圈复杂度的确需要考虑,不过还是要具体场景分析,如果只是完全套用公式的话也可能让逻辑变得太死板,我会好好考虑下的,谢谢提醒。
2d8545d
refactor: optimize gnet React API, fixes panjf2000#49
bf78824
panjf2000
No branches or pull requests
我注意到,在调用React的前后,有判断action与goto进行loop循环调用,我们大家都知道,尽量避免goto调用,还有这里的action判断逻辑有点混乱不直观,让人难以理解什么时候goto
深入阅读代码之后,发现这里的loop是为了循环调用ReadFrame(),来实现每次React只处理一帧有效数据,但是React里面还要二次判断是否为有效帧,实为浪费性能。
因此觉得优化此处代码取消goto,有以下两种方案:
Plan A: 在业务层内实现for循环遍历每一帧数据
Plan B: 在React上层,for循环分好data数据
通过此样修改之后,可以取消所有关于 c.action = Skip 的判断与代码,Plan B可以收缩减少gnet暴露的接口,增加代码性能与可读性,减少React的代码量。
The text was updated successfully, but these errors were encountered: