Optimization: to predefine a piece of heap memory before using several objects - GAINS?

Are there big gains in PERFORMANCE by distributing heap memory in advance and gradually increasing it?

Consider a VERY simplified example below:

byte * heapSpace = malloc (1 000 000);
int currentWriteSpot = 0;

struct A {
  int x;
  byte * extraSpace;
  int extraSpaceLength;
};

//a1 needs 10 bytes of extra storage space:
A a1;  
a1.x = 2;
a1.extraSpace = heapSpace + currentWriteSpot;
a1.extraSpaceLength = 10;

currentWriteSpot += 10;

//a2 needs 120 bytes of extra storage space:
A a2;
a2.x = 24;
a2.extraSpace = heapSpace + currentWriteSpot;
a2.extraSpaceLength = 120;

currentWriteSpot += 120;

// ... many more elements added

for ( ... ) {
   //loop contiguously over the allocated elements, manipulating contents stored at "extraSpace"
}

free (heapSpace);

VS:

...
a1.extraSpace = malloc ( 10 );
a2.extraSpace = malloc ( 120 );
a3...
a4...
...

//do stuff

free (a1.extraSpace);
free (a2.extraSpace);
free ...
free ...
free ...

Or can it just add complexity without significant performance gains?

Thanks guys!

+3
source share
4 answers

First of all, this does not increase complexity; he reduces it. Since you already determined at the beginning of your operation that you mallocwere successful, you do not need further failure checks, which at least should have freealready been allocated and perhaps change other changes to different objects. '

, , . , malloc .

, . , , , , , . , , , .

( , 8-16 ) .

, , , , ​​ GNU obstack.
+4

, , - , ( ""!= "" ). - - "", 2 FPS .

, Win7 LFH ( , ). , , - , , .

+4

, , (, LOTS -) . . / . (, ), , , . . . - - "", .. ..

+1

You have to request the memory manager as many memory blocks as you need to separately manage - in an ideal world where we have endless optimization time, of course. If you have multiple A objects that do not have to be shielded separately, do not select them separately.

Of course, is it really worth the more intensive optimization time, this is another question.

0
source

All Articles