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

Optimization for xlua_hotfix_flags #1069

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

zhenlinyang
Copy link
Contributor

@zhenlinyang zhenlinyang commented Jun 13, 2023

背景:开发人员难以保证只对主线程执行的代码进行插桩,也难以保证在 xlua_set_hotfix_flag 的过程中时其他线程不执行到被插桩的代码。考虑到运行效率,加锁是不现实的。
做法:由于 xlua_set_hotfix_flag 的过程可以保证在单线程进行,在 xlua_set_hotfix_flag 时,保证其他线程可以安全执行 xlua_get_hotfix_flag 等方法是一种折中处理方式。
(1) xlua_hotfix_flags 和 DelegateBridge.DelegateBridgeList 的元素修改不是原子操作,需要避免xlua_get_hotfix_flag 返回 true 时 DelegateBridge.DelegateBridgeList[n] 取到NullReference。
(2) realloc(xlua_hotfix_flags, (idx + 1) * sizeof(int)) 执行后,原 xlua_hotfix_flags 内存空间已被释放,新申请的内存空间在之后才重新赋值给 xlua_hotfix_flags。此过程不是原子操作,在此过程中如果执行 xlua_get_hotfix_flag 会出现SIGSEGV。
(3) xlua_hotfix_flags 从 int 标记改为 bool 标记,节省内存使用。

背景:开发人员难以保证只对主线程执行的代码进行插桩,也难以保证在 xlua_set_hotfix_flag 的过程中时其他线程不执行到被插桩的代码。考虑到运行效率,加锁是不现实的。
做法:由于 xlua_set_hotfix_flag 的过程可以保证在单线程进行,在 xlua_set_hotfix_flag 时,保证其他线程可以安全执行 xlua_get_hotfix_flag 等方法是一种折中处理方式。
(1) xlua_hotfix_flags 和 DelegateBridge.DelegateBridgeList 的元素修改不是原子操作,需要避免xlua_get_hotfix_flag 返回 true 时 DelegateBridge.DelegateBridgeList[n] 取到NullReference。
(2) realloc(xlua_hotfix_flags, (idx + 1) * sizeof(int)) 执行后,原 xlua_hotfix_flags 内存空间已被释放,新申请的内存空间在之后才重新赋值给 xlua_hotfix_flags。此过程不是原子操作,在此过程中如果执行 xlua_get_hotfix_flag 会出现SIGSEGV。
(3) xlua_hotfix_flags 从 int 标记改为 bool 标记,节省内存使用。
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

Successfully merging this pull request may close these issues.

None yet

1 participant