My application, overlay, constructs and destroys millions of cpp_rational variables. As overlay executes, its memory continually grows. My variables don't have that many digits, but each time that they are constructed or destroyed, they are slightly wider or narrower.
W/o studying the cpp_rational source, I surmise that it uses a heap that gets badly fragmented. Apart from the space, each single allocation is probably taking ever more time. That's annoying because overlay otherwise would take linear time.
What should I do? Are there easy ways to control this problem? Are other rational implementations better? Boost gives me a choice of several. Are there better implementations outside boost?
Good question, cpp_rational is just a pair of cpp_int's, and cpp_int's
either:
* Allocate no memory if the digit count is small enough then no memory
allocation is performed. Exactly where the cutoff occurs depends on the
platform, but should be at least 1024 bits. You can also control this
cutoff via the template parameters.
* Otherwise allocation use std::allocator, you can obviously provide
your own allocator if you think you can do better ;-)
So if memory fragmentation is the issue, then it sounds like you need to
tweak the template parameters to cpp_int and then compose your own
rational type from that:
typedef cpp_int_backend