[Issue 17746] Improve BigInt memory usage

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Jan 2 17:42:23 UTC 2018


https://issues.dlang.org/show_bug.cgi?id=17746

--- Comment #2 from hsteoh at quickfur.ath.cx ---
If the BigInt is const, then the best we can do is to create a new BigInt to
hold the result of the operation, i.e., what BigInt does now.  For the
optimization to have significant benefits, we pretty much require mutable
BigInt.  So one approach could be that if the BigInt is const/immutable, just
do what we do today (allocate a new BigInt to hold the result), otherwise
perform write-in-place optimization.

So the following approach *might* work:

- When performing an operation, if the operand is const or immutable, revert to
current behaviour (i.e., make a new BigInt to hold the result).

- When assigning to/from a const(BigInt) or immutable(BigInt), always make a
copy of the source. There's pretty much no choice when assigning from a
const(BigInt), because you can't update the refcount through the const
reference. Copying is also required when assigning to const(BigInt) because
otherwise you could assign mutable x to const y, then mutate x through the
mutable reference, which breaks by-value semantics when viewed through y.

- If assigning mutable BigInts to each other, update the reference count and
copy by reference instead.

- When performing an operation on mutable BigInt, if the refcount > 1, revert
to current behaviour (allocate a new BigInt to hold the result). If the
refcount == 1, then mutate in-place where possible.

--


More information about the Digitalmars-d-bugs mailing list