Skip to content

add move semantic like esp-dash - #134

Open
mathieucarbou wants to merge 4 commits into
ayushsharma82:devfrom
mathieucarbou:dev
Open

mathieucarbou wants to merge 4 commits into
ayushsharma82:devfrom
mathieucarbou:dev

Conversation

@mathieucarbou

Copy link
Copy Markdown
Contributor

No description provided.

@github-actions

github-actions Bot commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

This PR is marked as stale because it has been open 45 days with no activity. You can remove stale label or comment if this PR is still valid.

@github-actions github-actions Bot added the Stale label Oct 6, 2026
ayushsharma82 and others added 3 commits November 6, 2026 21:28
Point readers who are building a full product at SOFTT's engineering
services, with a banner and a free engineering review link (UTM-tagged).
…ring-readme

docs: add SOFTT Engineering section to README
@ayushsharma82

Copy link
Copy Markdown
Owner

@mathieucarbou v2 is going to be deprecated in this week. Check v3 branch and see if it holds in your application.

@mathieucarbou

mathieucarbou commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor Author

@mathieucarbou v2 is going to be deprecated in this week. Check v3 branch and see if it holds in your application.

I've already looked at #137 and checked the code. I won't switch to the new version.

I think you missed the whole point of the WSL_HIGH_PERF flag in this PR. ESPAsyncWS already maintains a WS message queue per client. This message queue is ordered and also delivery is done as fast as possible within the client pcb buffer (there is already a lwip buffer per client under the hood). The whole point of WSL_HIGH_PERF was to bypass any application buffering in Webserial in order to deliver messages as fast as possible to the WS layer without any extra memory allocation.

The idea behind the WSL_HIGH_PERF flag is obvious: let the user's responsibility to write complete lines.. This gives the user code the responsibility to correctly send whole lines (/n) that the lib can stream. And IMO this is not a big constrain because most of users app are streaming lines anymore to Webserial (commands and answers). And those who cannot or use vprintf or things like that could either buffer on their side or buffer with the little option that was included in WSL_HIGH_PERF to handle the vprintf grouping.

Here is an example on how to use esp_log_set_vprintf with ESP-IDF or Arduino to redirect applications logs using the WSL_HIGH_PERF mode:

https://github.com/mathieucarbou/YaSolR/blob/4b8ecf9d867ae8b1ba43ce50c1ba512924400efb/src/yasolr_logging.cpp#L56

So the complexity introduced by the logstream class and its buffering per client is unnecessary complex and slower.

I know you are trying to simplify user's life by allowing them to log in WebSerial character by character, by overcoming any bad and wrong use of the lib. And this is partly caused by the fact that WebSerial is implementing Print class. That's where the problem is. With the WSL_HIGH_PERF option, it is like we don't need to extend Print and the contract could even be removed. Only a send(String line) function would be enough. With the new PR, the memory cost associated with the new design is huge and will impact all users..

To join both needs, I don't think the design of the PR is right. I think a cleaner design would have been to remove public Print from WebSerial class, introduce a send(line) function and remove all the buffering from it.

Then, provide another class that is a wrapper, for users who need that:

class WebSerialAdapter: public Print {
   explicit WebSerialAdapter(WebSerialClass& delegate);
}

This is a breaking API change but only at 1 line: when users construct theit WebSerial, for those who really need the Print api they would do:

WebSerial s;
WebSerialAdapter adapter(s);

And use the adapter instead of the direct WebSerial class.

The adapter would be responsible to consolidate all the vprintf mess and then send it to WebSerial, eventually doing something else.
But you don't need a per clientId slot or any complexity and above all you do not need to force all users to go through this complex buffering layer that is already handled by AsyncWS and AsyncTCP internals / lwip.

update:

Here are some memory impact examples:

image

This is huge!

Note: I saw also that wsl_stream is also providing other features but like application level ACK, reconnect, etc but these have to be handled IMO elsewhere. WS spec already has a ping/pong facility that can be used and ACK also can be checked by looking at the return of the send methods of AsyncWS.

@ayushsharma82

Copy link
Copy Markdown
Owner

@mathieucarbou Okay but I won't be continuing to support WSL_HIGH_PERF mechanism from v3, it adds a lot of user friction & confusion. Also I believe the buffering logic should be independent from whatever WebSocket server we implement in WebSerial in the future rather than creating AsyncWebServer a hard dependency.

@mathieucarbou

mathieucarbou commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor Author

@mathieucarbou Okay but I won't be continuing to support WSL_HIGH_PERF mechanism from v3

That’s not what I asked ;-)

You can perfectly move all the buffering logic outside of the WebSerial class in an adapter which extends Print (see proposed design).

The new version of the library will impact heavily ALL projects whatever they use which is bad, instead of working in a better design to allow people to have an implementation closer to what they need and use.

The memory impact is huge!

Web console is mostly an accessory tool for most projects so if the price to integrate it is heavy people will just look at lighter alternatives I think or just spawn their AI tool to create something similar without the memory cost penalty. AI changing how you should think about the value of the project : value is not anymore in the UI but whether you can provide what the user is searching for so that they won’t need to spend tokens replicating the web serial UI.

With such high memory cost from a lib, only people using it for small use cases will use it as-is, but for bigger projects pretty sure people will look at crafting their own alternative because they need to save memory and processing for their app.

Side-note: esp-dash right now as no console widget but I would definitely drop webserial and create a new esp-dash component to stream server logs instead in one of my project (YaSolR). This would just be a component focused on esp-log streaming though.

@ayushsharma82

Copy link
Copy Markdown
Owner

I do agree the memory footprint being a bit bigger; though constrained users can optimize that by lowering the ring buffer to 4KB instead of 8KB (-D WSL_BUFFER_SIZE=4096) and a smaller sender task stack (-D WSL_TASK_STACK_SIZE=...) - that alone should give about 5-6KB savings.

I still think the ring buffer is the right approach for a log streamer, for a few reasons:

  1. A newly opened tab gets the recent history replayed. (a very big plus point for me)
  2. With AsyncWebServer's queue, log lines were silently discarded once it filled up. Raising the queue size costs heap at peak in the same range as the ring, and it's one more setting users have to tune in High Perf mode. I'd rather have something just works out of the box.
  3. v2 made one heap allocation per line. v3 batches lines into far fewer allocations, which matters for heap fragmentation on long-running devices.

Regarding people making something own with AI, that's really about everyone's own opinion about the library, nothing I can do to change that.

@mathieucarbou

mathieucarbou commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor Author

I do agree the memory footprint being a bit bigger; though constrained users can optimize that by lowering the ring buffer to 4KB instead of 8KB (-D WSL_BUFFER_SIZE=4096) and a smaller sender task stack (-D WSL_TASK_STACK_SIZE=...) - that alone should give about 5-6KB savings.

Is -D WSL_BUFFER_SIZE=0 supported ? In that case, if yes, can the whole streamlog code be skipped ?

I still think the ring buffer is the right approach for a log streamer, for a few reasons:

  1. A newly opened tab gets the recent history replayed. (a very big plus point for me)

this means some memory is kept on MCU for this history. And some users don't care about the history. Why would you force a behaviour that impacts memory for users that don't care about history ?

When you connect your USB cable to the MCU, does your ESP sends you back the whole history or part of it kept in memory ? No...
So why would it be different for WebSerial ?
And why a forced-feature instead of an opt-in one ?

  1. With AsyncWebServer's queue, log lines were silently discarded once it filled up. Raising the queue size costs heap at peak in the same range as the ring, and it's one more setting users have to tune in High Perf mode. I'd rather have something just works out of the box.

No: this is happened because you were not checking the return status of the send method. WebSerial was silently discarding. But not AsyncWS ;-)

And the asyncws queue is only if the network is heavily used: in theory, if messages are sent at a rate where they can be consumed (at least), then the queue won't grow. If you replace the queue by a ring buffer, what will happen ? Messages will be lost the same way.

Whatever the implementation, discarding only happens if the producer produces more than what the "consumer" (lwip client buffer) can send and ack. And if it happens, this is to prevent from a memory crash or a dead-lock.

I saw that you are blocking in the code... This is dangerous... Some core arduino operations should never be blocked (WiFi, BLE,etc). The loop should never be blocked, especially on a single core MCU.

  1. v2 made one heap allocation per line. v3 batches lines into far fewer allocations, which matters for heap fragmentation on long-running devices.

That's wrong. If used accordingly as explained in my 1st comment (by sending a whole line) - not chars or vprintf, then there is no unnecessary heap allocation except for the WS buffer that is owned by asyncws lib. I specifically did this flag in order to keep a minimal cpu and memory footprint and directly send the data to the WS layer without any intermediary buffer. The _initialBufferCapacity is set to 0 by default.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants