I am using an std::map<int, T*>
for frequent lookups and some emplacements. There are never any deletions. Since I know an upper bound for the int's used as key (which is around 16 million), I figured I could trade some memory for runtime by using:
std::vector<T*> mapReplacement;
mapReplacement.resize(max_int, nullptr);
However my runtime got significantly worse, even when not measuring the resize operation. Vector Lookups and "insertion" to already allocated space should be constant, while the map should have O(log(n)) insertion and lookup. The number of elements inserted is also in the hundreds of thousands, so it's not even that scarce... What am I not seeing here?
Edit 1: I am neither inserting, nor iterating/searching the vector, but only ever accessing by index. A somewhat representative use-case would look like this:
std::vector<T*> mapReplacement;
struct DataStructure {
void getInfluenced(int neighbor);
void doSomeWork(int vertex){
for (int i : neighbors(vertex)){
if (influence(vertex, i)){
if (mapReplacement[i] == nullptr)
mapReplacement[i] = new DataStructure();
mapReplacement[i]->getInfluenced(vertex);
}
}
}
}
int main(){
mapReplacement.resize(huge_scalar_field.vertex_count);
for (int i : huge_scalar_field){
if (isInterestingVertex(i)){
if (mapReplacement[i] == nullptr)
mapReplacement[i] = new DataStructure();
mapReplacement[i]->doSomeWork(i);
}
}
}
Basically I'm iterating over a huge scalar field (16777216 vertices). Some of the vertices (544008 entries in the vector/map) in the field are "interesting" and need a "DataStructure" to represent them. However per interesting vertex some neighborhood of that vertex is somehow influenced by it. Those influenced vertices now too need to be represented by a "DataStructure" and might be also "interesting". That's about every use of the vector/map in my code, however I did not have time to test if that's a minimal example for my problem, or if the problem only arises because of some weird interaction with the contents of "isInterestingVertex", "neighbor", "influence", "doSomeWork" and "getInfluenced".
Edit2: The specs of the system are: Kubuntu 17.04 64bit Kernel Version 4.10.0-42-generic, g++ 6.3.0-12ubuntu2, Intel Core I7-4770K, 16GB DDR3 RAM (of which around 4GB are used during runtime), imported and used libraries are Boost and HPX (however the problem occurs in a purely sequential run as well).