At present, the TCPClient and UDP classes allocate a static buffer - 128bytes for TCP and 512bytes for UDP.
I was thinking about changing these to be dynamically allocated or user allocated buffers to address issue 224 (user-defined UDP buffer size) and also to decrease the memory used by TCPClient, especially when used by TCPServer, which uses upto 3 buffers:
- TCPServer declares a
_clientdata member of type TCPClient as a placeholder for the last returned client instance. So TCPServer allocates a TCPClient buffer (128 bytes).
- TCPServer assigns this _client member like this
_client = TCPClient(...)
Which potentially creates another buffer on the stack. Plus the
TCPClient instance that the caller allocates and assigns the result of
available() to, that’s potentially 3 buffers allocated.
If we changed TCPClient/UDPClient to use only a buffer pointer and a size, then only one buffer would need be allocated. A default constructor would allocate the default buffer size (using
new) so existing code continues to compile and function as before. Additional constructors would allow the caller to provide the buffer they wish to use, so the buffer size can be tailored to the application.
I wanted to solicit comments before coding these changes - maybe there’s a reason this hasn’t been done already?