X Tutup
Skip to content

Commit 2d34cf0

Browse files
committed
resolved: use a temporary Set to speed up dns question parsing
This doesn't necessarily make things faster, because we still spend more time in dns_answer_add(), but it improves the compuational complexity of this part. If we even make dns_resource_key_equal_faster, this will become worthwhile.
1 parent 46d4d67 commit 2d34cf0

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

src/resolve/resolved-dns-packet.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "alloc-util.h"
88
#include "dns-domain.h"
99
#include "resolved-dns-packet.h"
10+
#include "set.h"
1011
#include "string-table.h"
1112
#include "strv.h"
1213
#include "unaligned.h"
@@ -2133,6 +2134,17 @@ static int dns_packet_extract_question(DnsPacket *p, DnsQuestion **ret_question)
21332134
if (!question)
21342135
return -ENOMEM;
21352136

2137+
_cleanup_set_free_ Set *keys = NULL; /* references to keys are kept by Question */
2138+
2139+
keys = set_new(&dns_resource_key_hash_ops);
2140+
if (!keys)
2141+
return log_oom();
2142+
2143+
r = set_reserve(keys, n * 2); /* Higher multipliers give slightly higher efficiency through
2144+
* hash collisions, but the gains quickly drop of after 2. */
2145+
if (r < 0)
2146+
return r;
2147+
21362148
for (i = 0; i < n; i++) {
21372149
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
21382150
bool cache_flush;
@@ -2147,7 +2159,14 @@ static int dns_packet_extract_question(DnsPacket *p, DnsQuestion **ret_question)
21472159
if (!dns_type_is_valid_query(key->type))
21482160
return -EBADMSG;
21492161

2150-
r = dns_question_add(question, key);
2162+
r = set_put(keys, key);
2163+
if (r < 0)
2164+
return r;
2165+
if (r == 0)
2166+
/* Already in the Question, let's skip */
2167+
continue;
2168+
2169+
r = dns_question_add_raw(question, key);
21512170
if (r < 0)
21522171
return r;
21532172
}

src/resolve/resolved-dns-question.c

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,36 @@ static DnsQuestion *dns_question_free(DnsQuestion *q) {
3232

3333
DEFINE_TRIVIAL_REF_UNREF_FUNC(DnsQuestion, dns_question, dns_question_free);
3434

35+
int dns_question_add_raw(DnsQuestion *q, DnsResourceKey *key) {
36+
/* Insert without checking for duplicates. */
37+
38+
assert(key);
39+
assert(q);
40+
41+
if (q->n_keys >= q->n_allocated)
42+
return -ENOSPC;
43+
44+
q->keys[q->n_keys++] = dns_resource_key_ref(key);
45+
return 0;
46+
}
47+
3548
int dns_question_add(DnsQuestion *q, DnsResourceKey *key) {
36-
size_t i;
3749
int r;
3850

3951
assert(key);
4052

4153
if (!q)
4254
return -ENOSPC;
4355

44-
for (i = 0; i < q->n_keys; i++) {
56+
for (size_t i = 0; i < q->n_keys; i++) {
4557
r = dns_resource_key_equal(q->keys[i], key);
4658
if (r < 0)
4759
return r;
4860
if (r > 0)
4961
return 0;
5062
}
5163

52-
if (q->n_keys >= q->n_allocated)
53-
return -ENOSPC;
54-
55-
q->keys[q->n_keys++] = dns_resource_key_ref(key);
56-
return 0;
64+
return dns_question_add_raw(q, key);
5765
}
5866

5967
int dns_question_matches_rr(DnsQuestion *q, DnsResourceRecord *rr, const char *search_domain) {

src/resolve/resolved-dns-question.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ int dns_question_new_address(DnsQuestion **ret, int family, const char *name, bo
2222
int dns_question_new_reverse(DnsQuestion **ret, int family, const union in_addr_union *a);
2323
int dns_question_new_service(DnsQuestion **ret, const char *service, const char *type, const char *domain, bool with_txt, bool convert_idna);
2424

25+
int dns_question_add_raw(DnsQuestion *q, DnsResourceKey *key);
2526
int dns_question_add(DnsQuestion *q, DnsResourceKey *key);
2627

2728
int dns_question_matches_rr(DnsQuestion *q, DnsResourceRecord *rr, const char *search_domain);

0 commit comments

Comments
 (0)
X Tutup