C++ Logo

sg20

Advanced search

Re: "Clean" Code, Horrible Performance

From: Victor Eijkhout <eijkhout_at_[hidden]>
Date: Wed, 1 Mar 2023 14:32:35 +0000
(Sorry for unthreading: I don’t know how to reply from a digest)

Like Kate I think the poster had some point with using an array of pointers. Indeed, it plays havoc with prefetching and TLB, but I also don’t like it from a conceptual point of view. That array doesn’t own the objects, so unless you manage ownership elsewhere you have a potential memory leak problem.

I struggle with this design. Currently I’m solving it by having only base objects, and the “derived class constructor” actually only installs a lambda expression (stored in a std::function) that computes the “virtual” function. If you do that, your array of objects can actually own the objets. Solution #2 is to have an array of unique pointers to the various derived objects. You can store a base class pointer to a derived class object.

Victor.

Received on 2023-03-01 14:32:46