antifuchs / ratelimit_meter

A leaky-bucket-as-a-meter rate-limiting implementation in Rust
MIT License
33 stars 6 forks source link

Fix memory leaks #18

Closed antifuchs closed 6 years ago

antifuchs commented 6 years ago

This PR tracks the work on plugging the memory leak in both decision algorithms, discovered in #17. The current strategy is to rewrite the whole thing using mutexes (with a crate-internal wrapper).

I've also made a test (using getrusage) to check if a lot of calls leak more memory than they should. The bounds are correct on my laptop, I hope they'll be ok on Travis too.

This change improves single-threaded performance and makes multi-threaded performance a bit worse (which was tainted by the memory issue though, so who's to say).

Benchmarks before:

     Running target/release/deps/multi_threaded-d8bd7867e9908eda

running 2 tests
test bench_gcra_20threads         ... bench:         138 ns/iter (+/- 2,129)
test bench_leaky_bucket_20threads ... bench:         567 ns/iter (+/- 11,141)

test result: ok. 0 passed; 0 failed; 0 ignored; 2 measured; 0 filtered out

     Running target/release/deps/single_threaded-b5dced51ae82c67e

running 6 tests
test bench_allower                 ... bench:          22 ns/iter (+/- 11)
test bench_gcra                    ... bench:         145 ns/iter (+/- 64)
test bench_gcra_bulk               ... bench:         249 ns/iter (+/- 539)
test bench_leaky_bucket            ... bench:         164 ns/iter (+/- 47)
test bench_leaky_bucket_bulk       ... bench:         178 ns/iter (+/- 100)
test bench_threadsafe_leaky_bucket ... bench:         200 ns/iter (+/- 134)

Benchmarks after:

     Running target/release/deps/multi_threaded-bab1ac060974eb03

running 2 tests
test bench_gcra_20threads         ... bench:      29,887 ns/iter (+/- 42,982)
test bench_leaky_bucket_20threads ... bench:     108,383 ns/iter (+/- 33,229)

test result: ok. 0 passed; 0 failed; 0 ignored; 2 measured; 0 filtered out

     Running target/release/deps/single_threaded-c89da25a5fa0b4d2

running 6 tests
test bench_allower                 ... bench:          20 ns/iter (+/- 2)
test bench_gcra                    ... bench:          80 ns/iter (+/- 23)
test bench_gcra_bulk               ... bench:         102 ns/iter (+/- 13)
test bench_leaky_bucket            ... bench:          75 ns/iter (+/- 8)
test bench_leaky_bucket_bulk       ... bench:          77 ns/iter (+/- 24)
test bench_threadsafe_leaky_bucket ... bench:          76 ns/iter (+/- 16)

test result: ok. 0 passed; 0 failed; 0 ignored; 6 measured; 0 filtered out