]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: peers: A bit of optimization when encoding cached server names.
authorFrédéric Lécaille <flecaille@haproxy.com>
Fri, 7 Jun 2019 08:34:04 +0000 (10:34 +0200)
committerWilly Tarreau <w@1wt.eu>
Fri, 7 Jun 2019 13:47:54 +0000 (15:47 +0200)
When a server name is cached we only send its cache entry ID which has
an encoded length of 1 (because smaller than PEER_ENC_2BYTES_MIN).
So, in this case we only have to encode 1, the already known encoded length
of this ID before encoding it.

Furthermore we do not have to call strlen() to compute the lengths of server
name strings thanks to this commit: "MINOR: dict: Store the length of the
dictionary entries".

src/peers.c

index 6cb01a946c3e13f0413f0c2b20b1faf24db678f2..7cc817cbc1ea8a61ef436dc61908b97d74245bfc 100644 (file)
@@ -537,23 +537,31 @@ static int peer_prepare_updatemsg(char *msg, size_t size, struct peer_prep_param
                                        dc = peer->dcache;
                                        cde.entry.key = de;
                                        cached_de = dcache_tx_insert(dc, &cde);
-                                       /* Leave enough room to encode the remaining data length. */
-                                       end = beg = cursor + PEER_MSG_ENC_LENGTH_MAXLEN;
-                                       /* Encode the dictionary entry key */
-                                       intencode(cde.id + 1, &end);
-                                       if (cached_de != &cde.entry) {
+                                       if (cached_de == &cde.entry) {
+                                               if (cde.id + 1 >= PEER_ENC_2BYTES_MIN)
+                                                       break;
+                                               /* Encode the length of the remaining data -> 1 */
+                                               intencode(1, &cursor);
+                                               /* Encode the cache entry ID */
+                                               intencode(cde.id + 1, &cursor);
+                                       }
+                                       else {
+                                               /* Leave enough room to encode the remaining data length. */
+                                               end = beg = cursor + PEER_MSG_ENC_LENGTH_MAXLEN;
+                                               /* Encode the dictionary entry key */
+                                               intencode(cde.id + 1, &end);
                                                /* Encode the length of the dictionary entry data */
-                                               value_len = strlen(de->value.key);
+                                               value_len = de->len;
                                                intencode(value_len, &end);
                                                /* Copy the data */
                                                memcpy(end, de->value.key, value_len);
                                                end += value_len;
+                                               /* Encode the length of the data */
+                                               data_len = end - beg;
+                                               intencode(data_len, &cursor);
+                                               memmove(cursor, beg, data_len);
+                                               cursor += data_len;
                                        }
-                                       /* Encode the length of the data */
-                                       data_len = end - beg;
-                                       intencode(data_len, &cursor);
-                                       memmove(cursor, beg, data_len);
-                                       cursor += data_len;
                                        break;
                                }
                        }