缓冲区增长策略 [英] Buffer growth strategy

查看:212
本文介绍了缓冲区增长策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我indended积累随机串片,然后获取结果的通用增长的缓冲。 <一href=\"http://github.com/agladysh/luabins/blob/060439a91f8c0da9a968484e1cab4ae7983cf238/src/savebuffer.c\">$c$c处理该缓冲区是写在纯C

I have a generic growing buffer indended to accumulate "random" string pieces and then fetch the result. Code to handle that buffer is written in plain C.

伪code API:

void write(buffer_t * buf, const unsigned char * bytes, size_t len);/* appends */
const unsigned char * buffer(buffer_t * buf);/* returns accumulated data */

我在思考发展战略,我应该挑选该缓冲区。

I'm thinking about the growth strategy I should pick for that buffer.

我不知道如果我的用户都会preFER内存或速度 - 或者这将是用户的数据的性质

I do not know if my users would prefer memory or speed — or what would be the nature of user's data.

我见过两个策略在野外:由固定大小的增量增长缓冲区(这就是我目前实现的),或者成倍增长的数据。 (也有分配的内存所需的确切数额的策略 - 但这不是我的情况很有意思)

I've seen two strategies in the wild: grow buffer by fixed size increments (that is what I've currently implemented) or grow data exponentially. (There is also a strategy to allocate the exact amount of memory needed — but this is not that interesting in my case.)

也许我应该让用户选择的策略。但是,这将使code稍微复杂...

Perhaps I should let user to pick the strategy... But that would make code a bit more complex...

曾几何时,香草萨特写道(参考安德鲁·柯尼希),最好的策略是,很可能,指数增长系数为1.5(搜索增长战略)。这仍然是最好的选择?

Once upon a time, Herb Sutter wrote (referencing Andrew Koenig) that the best strategy is, probably, exponential growth with factor 1.5 (search for "Growth Strategy"). Is this still the best choice?

有何建议?什么是你的经验说?

Any advice? What does your experience say?

推荐答案

除非你有一个很好的理由不这样做,指数增长可能是最好的选择。使用1.5的指数是不是真的神奇,而事实上,这不是什么安德鲁·柯尼希原来说。他原来说是该生长因子应小于(1 + SQRT(5))/ 2(〜1.6)。

Unless you have a good reason to do otherwise, exponential growth is probably the best choice. Using 1.5 for the exponent isn't really magical, and in fact that's not what Andrew Koenig originally said. What he originally said was that the growth factor should be less than (1+sqrt(5))/2 (~1.6).

<击>皮特·贝克尔说,当他在的Dinkumware P.J. Plauger,Dinkumware公司的老板说,他们做了一些测试,发现1.5行之有效。当您分配的内存块,分配器通常会分配一个块,这比你的要求给它的空间一点点簿记信息至少稍大。我的猜测(虽然未经证实的任何测试)是减少的因素有点让真正的块大小仍然适合的范围内。

Pete Becker says when he was at Dinkumware P.J. Plauger, owner of Dinkumware, says they did some testing and found that 1.5 worked well. When you allocate a block of memory, the allocator will usually allocate a block that's at least slightly larger than you requested to give it room for a little book-keeping information. My guess (though unconfirmed by any testing) is that reducing the factor a little lets the real block size still fit within the limit.

参考:
我相信安德鲁原来在杂志上发表了这个(在杂志面向对象编程的,IIRC)并没有发表在几年了,所以得到了重新打印可能会是相当困难的。

References: I believe Andrew originally published this in a magazine (the Journal of Object Oriented Programming, IIRC) which hasn't been published in years now, so getting a re-print would probably be quite difficult.

安德鲁·柯尼希的新闻组帖子和的PJ Plauger的新闻组帖子

这篇关于缓冲区增长策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆