Boost logo

Boost :

Subject: Re: [boost] [xint] Boost.XInt formal review (concrete complaint)
From: Jeffrey Lee Hellrung, Jr. (jhellrung_at_[hidden])
Date: 2011-03-07 01:28:46


On 3/6/2011 9:07 PM, Chad Nelson wrote:
> On Sun, 6 Mar 2011 17:43:15 -0800
> "Simonson, Lucanus J"<lucanus.j.simonson_at_[hidden]> wrote:
[...]
>> This whole argument misses the point of optimizing code that uses a
>> bigint class. You want to reuse the memory allocated in a bigint
>> variable over and over instead of reallocating every time.
>
> That requires writing at least a minimal memory manager. As I
> understand it, the managers that come with compiler libraries are
> optimized for that sort of thing these days, since C++ is geared toward
> so many small heap-allocated objects. I'd rather let the compiler's
> library deal with it than write more non-core code for something that
> the built-in code will almost certainly be able to do better most of the
> time.

I don't think that's what Luke's talking about. He's saying, if x has
some value, say [x0, x1, x2], and you assign some expression to it, you
want the result of that expression to directly replace the digits [x0,
x1, x2], rather than allocating a new block, compute the result in this
new block, and swap this block in place of the block holding [x0, x1, x2].

>> Whether you make one unneeded allocation or two is a less important
>> consideration than whether you make one or zero since 2/1 is a lot
>> smaller than 1/0. If a temporary is generated by the user's code they
>> have already pessimized their code. I am sorry, but you cannot avoid
>> allocation with a return by value semantic, only expression templates
>> allows you to reuse the storage on the left hand side of the
>> assignment operator. [...]
>
> I'm not familiar with a use of expression templates that would allow
> that. Can you point me to something explaining it?

Expression templates would allow you to tell the compiler, when it sees

x = a + b,

to generate code that replaces x's digits with the sum of a's and b's
digits, one at a time. Under ideal circumstances, if x already has a
large enough block, no new memory needs to be allocated. With no
expression templates, with or without COW or move emulation, a temporary
result holding the sum of a and b would be created, necessitating an
allocation, which would then (at best) be moved into x.

google "c++ expression templates" and/or look at Boost.Proto and
libraries that use Boost.Proto.

- Jeff


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk