Browse Source

H2O: Reduce thread contention in the cache (#4262)

This commit also contains a minor code cleanup.
Anton Kirilov 6 years ago
parent
commit
4f952657c3
2 changed files with 6 additions and 3 deletions
  1. 5 1
      frameworks/C/h2o/src/cache.c
  2. 1 2
      frameworks/C/h2o/src/world.c

+ 5 - 1
frameworks/C/h2o/src/cache.c

@@ -24,6 +24,9 @@
 #include "cache.h"
 #include "utility.h"
 
+// Increasing the number of caches by the following factor reduces contention; must be a power of 2.
+#define CONCURRENCY_FACTOR 4
+
 static h2o_cache_t *get_cache(cache_t *cache, h2o_cache_hashcode_t keyhash)
 {
 	assert(is_power_of_2(cache->cache_num));
@@ -39,9 +42,10 @@ int cache_create(size_t concurrency,
 	int ret = EXIT_SUCCESS;
 
 	memset(cache, 0, sizeof(*cache));
+	assert(is_power_of_2(CONCURRENCY_FACTOR));
 	// Rounding up to a power of 2 simplifies the calculations a little bit, and, as any increase in
 	// the number of caches, potentially reduces thread contention.
-	cache->cache_num = round_up_to_power_of_2(concurrency);
+	cache->cache_num = CONCURRENCY_FACTOR * round_up_to_power_of_2(concurrency);
 	cache->cache_num = MAX(cache->cache_num, 1);
 	capacity = (capacity + cache->cache_num - 1) / cache->cache_num;
 	cache->cache = malloc(cache->cache_num * sizeof(*cache->cache));

+ 1 - 2
frameworks/C/h2o/src/world.c

@@ -116,12 +116,11 @@ static void cleanup_multiple_query(multiple_query_ctx_t *query_ctx)
 		free_json_generator(query_ctx->gen,
 		                    &query_ctx->ctx->json_generator,
 		                    &query_ctx->ctx->json_generator_num,
-							query_ctx->ctx->config->max_json_generator);
+		                    query_ctx->ctx->config->max_json_generator);
 
 	free(query_ctx);
 }
 
-
 static void cleanup_multiple_query_request(void *data)
 {
 	multiple_query_ctx_t * const query_ctx = *(multiple_query_ctx_t **) data;