Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Passing a buffer around to reduce malloc/free traffic. actually not a…

…ll that useful
  • Loading branch information...
commit 79be7249e76cd684195bd3f784900df273192ecb 1 parent f42f4cd
David R. MacIver authored February 12, 2012

Showing 1 changed file with 23 additions and 9 deletions. Show diff stats Hide diff stats

  1. 32  fas_tournament.c
32  fas_tournament.c
@@ -33,6 +33,20 @@ void del_tournament(tournament *t){
33 33
 }
34 34
 
35 35
 
  36
+typedef struct {
  37
+  size_t *buffer;
  38
+} fas_optimiser;
  39
+
  40
+static fas_optimiser *new_optimiser(tournament *t){
  41
+  fas_optimiser *it = malloc(sizeof(fas_optimiser));
  42
+  it->buffer = malloc(sizeof(size_t) * t->size);
  43
+  return it;
  44
+}
  45
+
  46
+void del_optimiser(fas_optimiser *o){
  47
+  free(o);
  48
+}
  49
+
36 50
 inline double tournament_get(tournament *t, size_t i, size_t j){
37 51
   size_t n = t->size;
38 52
   assert(i >= 0); 
@@ -185,7 +199,7 @@ static void sort(size_t n, size_t *values){
185 199
   }
186 200
 }
187 201
 
188  
-static int brute_force_optimise(tournament *t, size_t n, size_t *items){
  202
+static int brute_force_optimise(fas_optimiser *o, tournament *t, size_t n, size_t *items){
189 203
 	if(n <= 1) return 0;
190 204
 	if(n == 2){
191 205
 		int c = tournament_compare(t, items[0], items[1]);
@@ -197,7 +211,7 @@ static int brute_force_optimise(tournament *t, size_t n, size_t *items){
197 211
 
198 212
   int changed = 0;
199 213
 
200  
-	size_t *working_buffer = malloc(sizeof(size_t) * n);
  214
+	size_t *working_buffer = o->buffer;
201 215
 	memcpy(working_buffer, items, n * sizeof(size_t));
202 216
   sort(n, working_buffer);
203 217
 
@@ -211,14 +225,12 @@ static int brute_force_optimise(tournament *t, size_t n, size_t *items){
211 225
 		}
212 226
   }
213 227
 
214  
-	free(working_buffer);
215  
-
216 228
 	return changed;
217 229
 }
218 230
 
219  
-static int window_optimise(tournament *t, size_t n, size_t *items, size_t window){
  231
+static int window_optimise(fas_optimiser *o, tournament *t, size_t n, size_t *items, size_t window){
220 232
   if(n <= window){
221  
-    return brute_force_optimise(t, n, items);
  233
+    return brute_force_optimise(o, t, n, items);
222 234
   }
223 235
   double last_score = score_fas_tournament(t, n, items);
224 236
   int changed_at_all = 0;
@@ -226,7 +238,7 @@ static int window_optimise(tournament *t, size_t n, size_t *items, size_t window
226 238
   while(changed){
227 239
     changed = 0;
228 240
     for(size_t i = 0; i < n - window; i++){
229  
-      changed |= brute_force_optimise(t, window, items + i); 
  241
+      changed |= brute_force_optimise(o, t, window, items + i); 
230 242
     }
231 243
     double new_score = score_fas_tournament(t, n, items);
232 244
 
@@ -401,6 +413,7 @@ int local_sort(tournament *t, size_t n, size_t *items){
401 413
 }
402 414
 
403 415
 size_t *optimal_ordering(tournament *t){
  416
+  fas_optimiser *o = new_optimiser(t);
404 417
   size_t n = t->size;
405 418
 	size_t *results = integer_range(n);
406 419
   FASDEBUG("Scoring\n");
@@ -420,9 +433,9 @@ size_t *optimal_ordering(tournament *t){
420 433
     int changed = 0;
421 434
 
422 435
     FASDEBUG("  window_optimise(5)\n");
423  
-    changed |= window_optimise(t, n, results, 5);
  436
+    changed |= window_optimise(o, t, n, results, 5);
424 437
     FASDEBUG("  window_optimise(7)\n");
425  
-    changed |= window_optimise(t, n, results, 7); 
  438
+    changed |= window_optimise(o, t, n, results, 7); 
426 439
     FASDEBUG("  local_sort\n");
427 440
     changed |= local_sort(t, n, results);
428 441
     smoothing_changed |= changed;
@@ -434,6 +447,7 @@ size_t *optimal_ordering(tournament *t){
434 447
     single_move_optimization(t, n, results);
435 448
   }
436 449
 
  450
+  del_optimiser(o);
437 451
   return results;
438 452
 }
439 453
 

0 notes on commit 79be724

Please sign in to comment.
Something went wrong with that request. Please try again.