]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
ntp: optimize resizing of hash table with sources
authorMiroslav Lichvar <mlichvar@redhat.com>
Tue, 2 Feb 2016 11:00:58 +0000 (12:00 +0100)
committerMiroslav Lichvar <mlichvar@redhat.com>
Tue, 2 Feb 2016 11:02:16 +0000 (12:02 +0100)
ntp_sources.c

index e6ac8b0b4b89116b2b54e994ada6c642e7174e28..96fb14296ca3a885b1c40f532d8d4775077ba486 100644 (file)
@@ -57,7 +57,8 @@ typedef struct {
                                    sources from the pool respond first */
 } SourceRecord;
 
-/* Hash table of SourceRecord, the size should be a power of two */
+/* Hash table of SourceRecord, its size is a power of two and it's never
+   more than half full */
 static ARR_Instance records;
 
 /* Number of sources in the hash table */
@@ -233,12 +234,12 @@ find_slot(NTP_Remote_Address *remote_addr, int *slot, int *found)
 }
 
 /* ================================================== */
-
 /* Check if hash table of given size is sufficient to contain sources */
+
 static int
 check_hashtable_size(unsigned int sources, unsigned int size)
 {
-  return sources * 2 + 1 < size;
+  return sources * 2 <= size;
 }
 
 /* ================================================== */
@@ -256,7 +257,7 @@ rehash_records(void)
   memcpy(temp_records, ARR_GetElements(records), old_size * sizeof (SourceRecord));
 
   /* The size of the hash table is always a power of two */
-  for (new_size = 4; !check_hashtable_size(n_sources, new_size); new_size *= 2)
+  for (new_size = 1; !check_hashtable_size(n_sources, new_size); new_size *= 2)
     ;
 
   ARR_SetSize(records, new_size);