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

tutorial02_answer中为什么指针暂存有着更好的性能。 #78

Open
shuaitq opened this issue Dec 1, 2016 · 2 comments
Open

Comments

@shuaitq
Copy link

shuaitq commented Dec 1, 2016

原文

首先,如同 lept_parse_whitespace(),我们使用一个指针 p 来表示当前的解析字符位置。这样做有两个好处,一是代码更简单,二是在某些编译器下性能更好(因为不能确定 c 会否被改变,从而每次更改 c->json 都要做一次间接访问)。如果校验成功,才把 p 赋值至 c->json。

疑问

为什么使用p指针暂存有利于编译器优化,c->json的间接访问是什么意思?

@feiwofeifeixiaowo
Copy link

个人理解:使用 c->json 形式访问比 p 访问 多了一次解引用过程。

@zyc737347123
Copy link

个人理解:用p还有一个好处,如果解析出错,c->json不用回退。因为编译器不能确定c会不会变,每次改变c->json , 要对c寻址并改变对应地址中的值。而p是局部变量,有些编译器可能直接用寄存器存放p的值,所以对应的少了一条st命令

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants