Home > Erlang探索, 源码分析 > 决定vheap大小的golden ratio算法(1.61803398875)鉴赏

决定vheap大小的golden ratio算法(1.61803398875)鉴赏

October 19th, 2013

原创文章,转载请注明: 转载自系统技术非业余研究

本文链接地址: 决定vheap大小的golden ratio算法(1.61803398875)鉴赏

摘抄自Erlang release note:

The vheap size series will now use the golden ratio instead of doubling and fibonacci sequences.

决定binary heap的大小现在是黄金分割率算法,很有意思,给大家参考下:

/* grow
*
* vheap_sz ======================
*
* vheap 75% + grow
* ———————-
*
* vheap 25 – 75% same
* ———————-
*
* vheap ~ – 25% shrink
*
* ———————-
*/

代码如下:

//erl_gc.c:2155
static Uint64
do_next_vheap_size(Uint64 vheap, Uint64 vheap_sz) {
    if ((Uint64) vheap/3 > (Uint64) (vheap_sz/4)) {
        Uint64 new_vheap_sz = vheap_sz;
        while((Uint64) vheap/3 > (Uint64) (vheap_sz/4)) {
            /* the golden ratio = 1.618 */
            new_vheap_sz = (Uint64) vheap_sz * 1.618;
            if (new_vheap_sz < vheap_sz ) {
                return vheap_sz;
            }
            vheap_sz = new_vheap_sz;
        }

        return vheap_sz;
    }

    if (vheap < (Uint64) (vheap_sz/4)) {
        return (vheap_sz >> 1);
    }

    return vheap_sz;

}

祝大家玩得开心!

Post Footer automatically generated by wp-posturl plugin for wordpress.

  1. erben
    October 21st, 2013 at 08:56 | #1

    1 e e^2 e^3 正好也满足fib序列要求

Comments are closed.