1

I am developing a latency sensitive application in C++. There's a module where I am using boost::unordered_map. I experienced too much latency while inserting first entry to the map, after that all the entries have decent insertion time.

If I use std::unordered_map instead of boost::unordered_map this behaviour does not occur. In this case, the first insertion also takes few hundred nano seconds(100~200).

Here are some time stats which I noticed: - 1st insertion in map : around 12 micro Sec(10 ~ 12 micro Sec) - After that almost all the insertions : 200 nano Sec(approx)

Here is the sample code for illustration:

struct timespec _timeSpec ;

long getTimerTime()
{
  clock_gettime(CLOCK_REALTIME, &_timeSpec);
  return ( 1000000000 * _timeSpec.tv_sec ) +  _timeSpec.tv_nsec;
}

int main()
{
  int i = 0;

  boost::unordered_map < uint64_t , std::pair < uint64_t , bool > > _turnAroundTime ;

  while( ++i != 10 )
  {
    uint64_t t1 = getTimerTime() ;

    _turnAroundTime[i] = std::make_pair ( i, true );

    uint64_t t2 = getTimerTime() ;

    std::cout << "Latency : " << t2 - t1 << std::endl;
  }

  return 0 ;
}

1 Answers1

1

That was the first heap allocation in your program. The rest of your program consisted of a very small amount of heap allocations.

The first heap allocation may require requesting a page of memory from the OS, which is reused until exhasted. Such a request may take longer than local heap usage.

This is a theory. To determine what exactly is taking time, profile.

If this theory is true, later allocations will take longer due to requiring more OS pages. And the careful use of an allocator and bounding map size can make it not occur.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524