Ericsson / ered

An Erlang client library for Valkey/Redis Cluster
MIT License
16 stars 8 forks source link

Micro-optimize command-to-RESP #34

Closed zuiderkwast closed 1 year ago

zuiderkwast commented 1 year ago

This avoids a few nested lists, thus a few allocations.

zuiderkwast commented 1 year ago

It's hard to get stable figures, but here is my best try.

Each test run encodes a large pipeline of commands (7 commands of 4821 bytes in total) one million times. The times are nanoseconds per encode.

Run With this PR main branch Improvement %
1 7319 8100 9.64
2 6962 8221 15.31
3 8988 8400 -7.00
4 8144 8091 -0.65
5 8279 8094 -2.28
6 8092 8481 4.58
7 7397 7892 6.27
8 7039 7832 10.12
9 7464 7645 2.36
10 8105 8114 0.11
Avg 7778.9 8087 3.81

It's easier to motivate it theoretically.

Each nested list which is replaced by its elements inlined in the outer list saves one cons cell, i.e. one allocation of 2 words (16 bytes). So with fewer allocations, it's also less work for the garbage collector.

Example: [... , $\r, $\n, ...] instead of [... , "\r\n" , ... ] saves one cons cell.