Optimization for xlua_hotfix_flags #1069
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
背景:开发人员难以保证只对主线程执行的代码进行插桩,也难以保证在 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 标记,节省内存使用。