I am coding a simulation C, in which, given the sequence of rules for checking, we break it into βslicesβ and check each slice. (The basic idea is that order is important, and the actual value of a rule is influenced by some rules above it, we can make a βsliceβ with each rule and only those rules that overlap with it. Then we check the slices, which are usually much smaller the whole sequence.)
My problem is as follows.
I have a structure (policy) that contains an array of structures (rules) and int (length). My initial implementation used malloc and realloc liberally:
struct{ struct rule *rules; int length; }policy; ... struct policy makePolicy(int length) { struct policy newPolicy; newPolicy.rules = malloc(length * sizeof(struct rule)); newPolicy.length = length; return newPolicy; } ... struct policy makeSlice(struct policy inPol, int rulePos) { if(rulePos > inPol.length - 1){ printf("Slice base outside policy \n"); exit(1); } struct slice = makePolicy(inPol.length);
Since it uses malloc'ed memory, I assume it uses a lot of heap. Now I am trying to connect to an experimental parallel machine that does not have malloc.
Unfortunately, I went and allocated everything using fixed-size arrays.
Now here is the shocker.
New code is slower. Much slower.
(The source code used to wait for minutes in a row when the fragment length was 200, or maybe an hour more than 300 ... now this happens when the length of the slice is 70, 80 ... and was counting the hours, say 120. Not yet 200.)
The only thing is that the slices are now assigned the same memory as the full policy (MAXBUFLEN - 10000), but the whole seems to have absolutely no memory. "top" shows that the total memory consumed is rather modest, with dozens of megabytes, as before. (And, of course, since I keep the length, I don't get hung up on everything, just the part with the real rules.)
Can anyone really help explain why it suddenly got so much slower?