valkey/utils
uriyage 8d899d7464 Improve multithreaded performance with memory prefetching (#861)
This PR utilizes the IO threads to execute commands in batches, allowing
us to prefetch the dictionary data in advance.

After making the IO threads asynchronous and offloading more work to
them in the first 2 PRs, the `lookupKey` function becomes a main
bottle-neck and it takes about 50% of the main-thread time (Tested with
SET command). This is because the Valkey dictionary is a straightforward
but inefficient chained hash implementation. While traversing the hash
linked lists, every access to either a dictEntry structure, pointer to
key, or a value object requires, with high probability, an expensive
external memory access.

### Memory Access Amortization

Memory Access Amortization (MAA) is a technique designed to optimize the
performance of dynamic data structures by reducing the impact of memory
access latency. It is applicable when multiple operations need to be
executed concurrently. The principle behind it is that for certain
dynamic data structures, executing operations in a batch is more
efficient than executing each one separately.

Rather than executing operations sequentially, this approach interleaves
the execution of all operations. This is done in such a way that
whenever a memory access is required during an operation, the program
prefetches the necessary memory and transitions to another operation.
This ensures that when one operation is blocked awaiting memory access,
other memory accesses are executed in parallel, thereby reducing the
average access latency.

We applied this method in the development of `dictPrefetch`, which takes
as parameters a vector of keys and dictionaries. It ensures that all
memory addresses required to execute dictionary operations for these
keys are loaded into the L1-L3 caches when executing commands.
Essentially, `dictPrefetch` is an interleaved execution of dictFind for
all the keys.


**Implementation details**

When the main thread iterates over the `clients-pending-io-read`, for
clients with ready-to-execute commands (i.e., clients for which the IO
thread has parsed the commands), a batch of up to 16 commands is
created. Initially, the command's argv, which were allocated by the IO
thread, is prefetched to the main thread's L1 cache. Subsequently, all
the dict entries and values required for the commands are prefetched
from the dictionary before the command execution. Only then will the
commands be executed.

---------

Signed-off-by: Uri Yagelnik <uriy@amazon.com>
2024-09-03 09:00:45 -07:00
..
create-cluster Enable protected-configs, debug and module commands in create-cluster script (#701) 2024-06-27 12:27:09 -07:00
graphs/commits-over-time
hyperloglog Copyright update to reflect IP transfer from salvatore to Redis (#740) 2024-09-03 09:00:45 -07:00
lru Fix typo and rename Redis to Valkey in the utils/lru/README (#314) 2024-04-14 11:40:01 -07:00
releasetools Adds workflows to build release binaries and push to S3 (#315) 2024-04-16 07:01:36 -07:00
req-res-validator Add reply_schema to command json files (internal for now) (#10273) 2023-03-11 10:14:16 +02:00
srandmember
build-static-symbols.tcl Copyright update to reflect IP transfer from salvatore to Redis (#740) 2024-09-03 09:00:45 -07:00
corrupt_rdb.c Copyright update to reflect IP transfer from salvatore to Redis (#740) 2024-09-03 09:00:45 -07:00
gen-test-certs.sh Update redis legacy word when run TLS cert file (#572) 2024-05-30 13:09:29 -04:00
generate-command-code.py Remove trademarked language in code comments (#223) 2024-04-09 10:24:03 +02:00
generate-commands-json.py Update redis to valkey in generate-commands-json.py (#238) 2024-04-18 21:31:49 +02:00
generate-fmtargs.py Improve multithreaded performance with memory prefetching (#861) 2024-09-03 09:00:45 -07:00
generate-module-api-doc.rb Minor fix in module API doc script (#494) 2024-05-14 01:12:30 +02:00
generate-unit-test-header.py Fix invalid escape sequence in utils, minor cleanup in python script (#948) 2024-09-03 09:00:45 -07:00
install_server.sh Rename redis_init_script file and its content (#357) 2024-04-24 10:05:11 +02:00
module-api-since.rb Update script to generate Valkey Module API docs (#406) 2024-04-30 18:24:18 +02:00
redis-copy.rb Copyright update to reflect IP transfer from salvatore to Redis (#740) 2024-09-03 09:00:45 -07:00
redis-sha1.rb Copyright update to reflect IP transfer from salvatore to Redis (#740) 2024-09-03 09:00:45 -07:00
reply_schema_linter.js Add reply_schema to command json files (internal for now) (#10273) 2023-03-11 10:14:16 +02:00
req-res-log-validator.py Remove trademarked language in code comments (#223) 2024-04-09 10:24:03 +02:00
speed-regression.tcl Copyright update to reflect IP transfer from salvatore to Redis (#740) 2024-09-03 09:00:45 -07:00
systemd-valkey_multiple_servers@.service Rename systemd files and content to valkey from redis (#234) 2024-04-08 14:37:56 -04:00
systemd-valkey_server.service Rename systemd files and content to valkey from redis (#234) 2024-04-08 14:37:56 -04:00
tracking_collisions.c Copyright update to reflect IP transfer from salvatore to Redis (#740) 2024-09-03 09:00:45 -07:00
valkey_init_script Rename redis_init_script file and its content (#357) 2024-04-24 10:05:11 +02:00
valkey_init_script.tpl Rename redis_init_script file and its content (#357) 2024-04-24 10:05:11 +02:00
whatisdoing.sh