]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
Replace for loops with memcpy
authorIgor Putovny <igor.putovny@nic.cz>
Thu, 9 May 2024 15:57:28 +0000 (17:57 +0200)
committerIgor Putovny <igor.putovny@nic.cz>
Thu, 30 May 2024 10:30:00 +0000 (12:30 +0200)
proto/aggregator/aggregator.c

index 5d336abcd265978746182ad46e4f2fcb1eae2fbc..079e97117190a22a5ff7cf5922e075f9702babda 100644 (file)
@@ -357,23 +357,18 @@ compute_buckets_union(struct trie_node *node, const struct trie_node *left, cons
   assert(right != NULL);
   assert(node  != NULL);
 
-  struct aggregator_bucket *input_buckets[64] = { 0 };
-  int input_count = 0;
+  struct aggregator_bucket *input_buckets[MAX_POTENTIAL_BUCKETS_COUNT * 2] = { 0 };
+  const int input_count = left->potential_buckets_count + right->potential_buckets_count;
 
-  for (int i = 0; i < left->potential_buckets_count; i++)
-    input_buckets[input_count++] = left->potential_buckets[i];
-
-  for (int i = 0; i < right->potential_buckets_count; i++)
-    input_buckets[input_count++] = right->potential_buckets[i];
+  memcpy(input_buckets, left->potential_buckets, sizeof(left->potential_buckets[0]) * left->potential_buckets_count);
+  memcpy(&input_buckets[left->potential_buckets_count], right->potential_buckets, sizeof(right->potential_buckets[0]) * right->potential_buckets_count);
+  qsort(input_buckets, input_count, sizeof(input_buckets[0]), aggregator_bucket_compare_wrapper);
 
-  qsort(input_buckets, input_count, sizeof(struct aggregator_bucket *), aggregator_bucket_compare_wrapper);
-
-  struct aggregator_bucket *output_buckets[64] = { 0 };
+  struct aggregator_bucket *output_buckets[MAX_POTENTIAL_BUCKETS_COUNT * 2] = { 0 };
   int output_count = 0;
 
   for (int i = 0; i < input_count; i++)
   {
-
     if (output_count != 0 && output_buckets[output_count - 1] == input_buckets[i])
       continue;
 
@@ -389,13 +384,8 @@ compute_buckets_union(struct trie_node *node, const struct trie_node *left, cons
     for (int j = i + 1; j < output_count; j++)
       assert(output_buckets[i] != output_buckets[j]);
 
-  for (int i = 0; i < output_count; i++)
-  {
-    if (node->potential_buckets_count >= MAX_POTENTIAL_BUCKETS_COUNT)
-      break;
-
-    node->potential_buckets[node->potential_buckets_count++] = output_buckets[i];
-  }
+  node->potential_buckets_count = output_count < MAX_POTENTIAL_BUCKETS_COUNT ? output_count : MAX_POTENTIAL_BUCKETS_COUNT;
+  memcpy(node->potential_buckets, output_buckets, sizeof(node->potential_buckets[0]) * node->potential_buckets_count);
 }
 
 /*