Skip to content
New issue

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接口优化 #49

Closed
luyu6056 opened this issue Dec 30, 2019 · 4 comments
Closed

React接口优化 #49

luyu6056 opened this issue Dec 30, 2019 · 4 comments
Assignees
Labels
enhancement New feature or request proposal Proposal for this repo
Milestone

Comments

@luyu6056
Copy link

luyu6056 commented Dec 30, 2019

我注意到,在调用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数据
image

通过此样修改之后,可以取消所有关于 c.action = Skip 的判断与代码,Plan B可以收缩减少gnet暴露的接口,增加代码性能与可读性,减少React的代码量。

@luyu6056 luyu6056 added enhancement New feature or request proposal Proposal for this repo labels Dec 30, 2019
@iHuangYaoshi
Copy link

Cyclomatic complexity of the plan b code maybe high, if in if is not good code smell

@panjf2000
Copy link
Owner

panjf2000 commented Dec 30, 2019

@KaiwenWang
抱歉,没太懂你的意思?能仔细说下你的想法吗?

@iHuangYaoshi
Copy link

搜一下代码圈复杂度的概念。层层嵌套,如for if if 可以认为是bad smell 并且是 潜在bug的根源

@panjf2000
Copy link
Owner

我知道,我还以为你是对具体的逻辑有什么担忧,关于代码的圈复杂度的确需要考虑,不过还是要具体场景分析,如果只是完全套用公式的话也可能让逻辑变得太死板,我会好好考虑下的,谢谢提醒。

@panjf2000 panjf2000 added this to the v1 milestone Mar 7, 2020
0-haha pushed a commit to 0-haha/gnet that referenced this issue Jan 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request proposal Proposal for this repo
Projects
None yet
Development

No branches or pull requests

3 participants