Skip to content

Extremly slow performance when constructing large arrays of basic type #194

Description

@ax7e

Hi, there are several Stack Overflow questions regarding performance issues when creating large arrays of basic types. For example, in this question (https://stackoverflow.com/questions/74609901/how-create-a-big-array-in-shared-memory-with-boostinterprocessmanaged-shard), the poster also experienced this problem. After profiling the code, I noticed that the root cause of the problem seems to be in this section (

virtual void construct_n(void *mem
, std::size_t num
, std::size_t &constructed) BOOST_OVERRIDE
{
T* memory = static_cast<T*>(mem);
for(constructed = 0; constructed < num; ++constructed){
this->construct(memory++, IsIterator(), index_tuple_t());
this->do_increment(IsIterator(), index_tuple_t());
}
}
), which attempts to call the constructor for every element in the array.

Is it possible to write code to handle cases where the array is composed of basic types, and then initialize it using memset?

To reproduce

#include <boost/interprocess/managed_shared_memory.hpp>
namespace bip = boost::interprocess;

int main() {
    auto   id_   = "shmTest";
    size_t size_ = 2ul << 30;

    bip::shared_memory_object::remove(id_);
    bip::managed_shared_memory sm(bip::create_only, id_, size_ + 1024);

    auto data_ = sm.construct<char>("Data")[size_]('\0');
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions