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

fix binary search bug. #108

Closed
wants to merge 2 commits into from
Closed

fix binary search bug. #108

wants to merge 2 commits into from

Conversation

leeonix
Copy link
Contributor

@leeonix leeonix commented May 13, 2014

二分查找经典的bug,使用(begin+end)/2会有整数溢出的问题。换减法就没问题了。

@cloudwu
Copy link
Owner

cloudwu commented May 13, 2014

这个暂时不改, 因为不支持这么多的名字(所以不会溢出). 下个版本会改过来.

btw, 用减法只是把搜索数量的上限从 30bit 增加到 31bit . 超过 31bit 依旧有 int32 不能完整表示下标的问题.

@cloudwu cloudwu closed this May 13, 2014
@leeonix
Copy link
Contributor Author

leeonix commented May 13, 2014

减法的换算:
(begin + end) / 2 = begin / 2 + end / 2 = begin - begin / 2 + end / 2 = begin + (end - begin) / 2
由公式得出,减法只是一个加法的公式的换算,实际意义和加法是没区别的。
不管是从概率统计还是实际出现的几率都小,但隐患毕竟是隐患。

@cloudwu
Copy link
Owner

cloudwu commented May 13, 2014

我在 dev 分支上加上了限制, 不准超过 0x40000000 个 name handle .
如果 name handle 有如此之多, 设计或实现一定出了严重的问题 (内存也不可能够支持这么多 service).

(begin + end)/2 毕竟实现简单, 而且数学意义明确. (不值得一提的是:性能也略高.)

@leeonix
Copy link
Contributor Author

leeonix commented May 13, 2014

噢,你在生成上加限制,这样确实避免了。不过我现在在64位服务器上测试。也是限制这么多?不过也确实。0x40000000就已经超级多了。

@leeonix
Copy link
Contributor Author

leeonix commented May 13, 2014

不过话说你也真够顽固的,宁愿改上限也不改算法那。在现在的指令流水线方式下,你说得性能提高微乎其微……

@cloudwu
Copy link
Owner

cloudwu commented May 13, 2014

这个限制不仅不少,反而多了. 其实加不加都一样.
因为单个节点的 service 总数受 id 限制, 最多同时只能有 0x1000000 个,远小于 0x40000000。这和是不是 64bit 系统无关。

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.

2 participants