Topic: Best practice to avoid dynamic memory allocations.
Hi there! I'm a new user exploring whether wolfSSL can provide better performance for my use case compared to OpenSSL.
I'm aiming to use it in a low-latency service(as TLS client), so I need to avoid any calls to malloc or free during encryption and decryption, as they would impact latency. This does not means malloc or free are forbidden, but I want to avoid them as possible as I can.
To get started with the library, I wrote a simple echo client using BIO model(which I used in OpenSSL), I just replace all openssl symbols with wolfssl's(like wolfSSL_BIO_write vs BIO_write, etc), and it works well.
However, when I tried setting a custom malloc function to print each time it was called, I discovered that every time I perform a write or feed data to the rbio, it triggers a malloc.
My question is: Is there a way to avoid these malloc/free calls? I have a few ideas but am unsure which approach is best or supported by the library:
1. Is there a configuration or setting that would prevent the library from freeing an already allocated buffer and instead reuse it later? The buffer could grow to meet the need(I don't care how many memory it cost if it's reasonable), and if it’s large enough, it should avoid triggering any further malloc calls (or at least very few).
2. From reading the documentation, I know I can use the static buffer allocation option to avoid system-level malloc, but I’ve been unable to get my TLS client working with the details from this manual. Are there any full TLS client examples that use static buffers?
I think approach 1 might be the easiest solution, but I haven’t found relevant documentation. Did I miss something?
Thank you for your help!
Below is output of my print_malloc, the tls client sends 100000 messages and received 100000 echos, and malloc count is 200251:
malloc count 200247, size 145
malloc count 200248, size 181
malloc count 200249, size 145
malloc count 200250, size 181
malloc count 200251, size 145
nround: 100000