diff options
author | Christian Grothoff <christian@grothoff.org> | 2017-02-22 13:41:33 +0100 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2017-02-22 13:41:33 +0100 |
commit | 4ba0fa6ba9f9be044c8c96ddd4d909e7d84403b5 (patch) | |
tree | a85ef8a265b9ae57750d51feec58690a15f17d02 /src/block | |
parent | 55f509b7c7809f1c8dc9db66a8df11ef55aaf628 (diff) | |
download | gnunet-4ba0fa6ba9f9be044c8c96ddd4d909e7d84403b5.tar.gz gnunet-4ba0fa6ba9f9be044c8c96ddd4d909e7d84403b5.zip |
support BF size adjustments in other plugins
Diffstat (limited to 'src/block')
-rw-r--r-- | src/block/plugin_block_template.c | 49 | ||||
-rw-r--r-- | src/block/plugin_block_test.c | 51 |
2 files changed, 99 insertions, 1 deletions
diff --git a/src/block/plugin_block_template.c b/src/block/plugin_block_template.c index 0e8107af2..f11d5ee76 100644 --- a/src/block/plugin_block_template.c +++ b/src/block/plugin_block_template.c | |||
@@ -44,6 +44,37 @@ | |||
44 | 44 | ||
45 | 45 | ||
46 | /** | 46 | /** |
47 | * How many bytes should a bloomfilter be if we have already seen | ||
48 | * entry_count responses? Note that #GNUNET_CONSTANTS_BLOOMFILTER_K | ||
49 | * gives us the number of bits set per entry. Furthermore, we should | ||
50 | * not re-size the filter too often (to keep it cheap). | ||
51 | * | ||
52 | * Since other peers will also add entries but not resize the filter, | ||
53 | * we should generally pick a slightly larger size than what the | ||
54 | * strict math would suggest. | ||
55 | * | ||
56 | * @param entry_count expected number of entries in the Bloom filter | ||
57 | * @return must be a power of two and smaller or equal to 2^15. | ||
58 | */ | ||
59 | static size_t | ||
60 | compute_bloomfilter_size (unsigned int entry_count) | ||
61 | { | ||
62 | size_t size; | ||
63 | unsigned int ideal = (entry_count * BLOOMFILTER_K) / 4; | ||
64 | uint16_t max = 1 << 15; | ||
65 | |||
66 | if (entry_count > max) | ||
67 | return max; | ||
68 | size = 8; | ||
69 | while ((size < max) && (size < ideal)) | ||
70 | size *= 2; | ||
71 | if (size > max) | ||
72 | return max; | ||
73 | return size; | ||
74 | } | ||
75 | |||
76 | |||
77 | /** | ||
47 | * Create a new block group. | 78 | * Create a new block group. |
48 | * | 79 | * |
49 | * @param ctx block context in which the block group is created | 80 | * @param ctx block context in which the block group is created |
@@ -63,6 +94,24 @@ block_plugin_template_create_group (void *cls, | |||
63 | size_t raw_data_size, | 94 | size_t raw_data_size, |
64 | va_list va) | 95 | va_list va) |
65 | { | 96 | { |
97 | unsigned int bf_size; | ||
98 | const char *guard; | ||
99 | |||
100 | guard = va_arg (va, const char *); | ||
101 | if (0 == memcmp (guard, | ||
102 | "seen-set-size", | ||
103 | strlen ("seen-set-size"))) | ||
104 | bf_size = compute_bloomfilter_size (va_arg (va, unsigned int)); | ||
105 | else if (0 == memcmp (guard, | ||
106 | "filter-size", | ||
107 | strlen ("filter-size"))) | ||
108 | bf_size = va_arg (va, unsigned int); | ||
109 | else | ||
110 | { | ||
111 | GNUNET_break (0); | ||
112 | bf_size = TEMPLATE_BF_SIZE; | ||
113 | } | ||
114 | GNUNET_break (NULL == va_arg (va, const char *)); | ||
66 | return GNUNET_BLOCK_GROUP_bf_create (cls, | 115 | return GNUNET_BLOCK_GROUP_bf_create (cls, |
67 | TEMPLATE_BF_SIZE, | 116 | TEMPLATE_BF_SIZE, |
68 | BLOOMFILTER_K, | 117 | BLOOMFILTER_K, |
diff --git a/src/block/plugin_block_test.c b/src/block/plugin_block_test.c index 615f1571b..c5483f26e 100644 --- a/src/block/plugin_block_test.c +++ b/src/block/plugin_block_test.c | |||
@@ -42,6 +42,37 @@ | |||
42 | 42 | ||
43 | 43 | ||
44 | /** | 44 | /** |
45 | * How many bytes should a bloomfilter be if we have already seen | ||
46 | * entry_count responses? Note that #GNUNET_CONSTANTS_BLOOMFILTER_K | ||
47 | * gives us the number of bits set per entry. Furthermore, we should | ||
48 | * not re-size the filter too often (to keep it cheap). | ||
49 | * | ||
50 | * Since other peers will also add entries but not resize the filter, | ||
51 | * we should generally pick a slightly larger size than what the | ||
52 | * strict math would suggest. | ||
53 | * | ||
54 | * @param entry_count expected number of entries in the Bloom filter | ||
55 | * @return must be a power of two and smaller or equal to 2^15. | ||
56 | */ | ||
57 | static size_t | ||
58 | compute_bloomfilter_size (unsigned int entry_count) | ||
59 | { | ||
60 | size_t size; | ||
61 | unsigned int ideal = (entry_count * BLOOMFILTER_K) / 4; | ||
62 | uint16_t max = 1 << 15; | ||
63 | |||
64 | if (entry_count > max) | ||
65 | return max; | ||
66 | size = 8; | ||
67 | while ((size < max) && (size < ideal)) | ||
68 | size *= 2; | ||
69 | if (size > max) | ||
70 | return max; | ||
71 | return size; | ||
72 | } | ||
73 | |||
74 | |||
75 | /** | ||
45 | * Create a new block group. | 76 | * Create a new block group. |
46 | * | 77 | * |
47 | * @param ctx block context in which the block group is created | 78 | * @param ctx block context in which the block group is created |
@@ -61,8 +92,26 @@ block_plugin_test_create_group (void *cls, | |||
61 | size_t raw_data_size, | 92 | size_t raw_data_size, |
62 | va_list va) | 93 | va_list va) |
63 | { | 94 | { |
95 | unsigned int bf_size; | ||
96 | const char *guard; | ||
97 | |||
98 | guard = va_arg (va, const char *); | ||
99 | if (0 == memcmp (guard, | ||
100 | "seen-set-size", | ||
101 | strlen ("seen-set-size"))) | ||
102 | bf_size = compute_bloomfilter_size (va_arg (va, unsigned int)); | ||
103 | else if (0 == memcmp (guard, | ||
104 | "filter-size", | ||
105 | strlen ("filter-size"))) | ||
106 | bf_size = va_arg (va, unsigned int); | ||
107 | else | ||
108 | { | ||
109 | GNUNET_break (0); | ||
110 | bf_size = TEST_BF_SIZE; | ||
111 | } | ||
112 | GNUNET_break (NULL == va_arg (va, const char *)); | ||
64 | return GNUNET_BLOCK_GROUP_bf_create (cls, | 113 | return GNUNET_BLOCK_GROUP_bf_create (cls, |
65 | TEST_BF_SIZE, | 114 | bf_size, |
66 | BLOOMFILTER_K, | 115 | BLOOMFILTER_K, |
67 | type, | 116 | type, |
68 | nonce, | 117 | nonce, |