bearophile wrote:
Don:
However, the other case which is interesting is when BigInt is replaced
with FixedInt!n (maybe someone can come up with a better name for this?)
-- an integer with a length of a fixed number of ints.
Unlike BigInt, this has basically the same semantics as built-in integer
types. In fact, FixedInt!1 == int, FixedInt!2 == long, FixedInt!4 == cent.
This is possibly even more relevant for Rational. I haven't thought much
about the implications though.
BTW, does BigInt over-allocate initially to allow certain operations
(like +=) to be done in-place more frequently?
No, it doesn't. As long as it uses copy-on-write, there's no benefit to
doing so.
Reference counting would clearly be superior for FixedInt, but I'm not
at all sure that it would be a win for BigInt. But of course FixedInt
wouldn't need to over-allocate.
Are FixedInt fully allocated on the stack?
Note that they don't currently exist (though I heard someone is working
on it) but the implementation I would envisage is:
If size <= long.sizeof, alias to built-in type.
If size < threshold (maybe 2*cent.sizeof), allocate on the stack.
Otherwise, implement as a pointer to reference counted heap memory.