Skip to content

Conversation

kfessel
Copy link
Contributor

@kfessel kfessel commented Oct 13, 2025

Contribution description

speedup usb_cdc_acm tsrb writing and reading by avoid jumps to tsrb and superfluous irq_disable calls.

Testing procedure

read
usb cdc acm still working

Issues/PRs references

#12402 fixed tsrb hadling for usb ( loss of chars for acm) by adding irq disable around tsrb access
#15218 added isr_guards thoughout tsrb

@github-actions github-actions bot added Area: USB Area: Universal Serial Bus Area: sys Area: System labels Oct 13, 2025
@kfessel kfessel requested a review from maribu October 13, 2025 10:11
sys/tsrb/tsrb.c Outdated
Comment on lines 62 to 69
size_t tmp = n;
int cnt = 0;
unsigned irq_state = irq_disable();
while (tmp && !tsrb_empty(rb)) {
while (n-- && !tsrb_empty(rb)) {
*dst++ = _pop(rb);
tmp--;
cnt++;
}
irq_restore(irq_state);
return (n - tmp);
return (cnt);
Copy link
Contributor Author

@kfessel kfessel Oct 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are you renaming and retyping variables and restructure their use?
-- this question goes for other function you do the same thing to as well

Copy link
Contributor Author

@kfessel kfessel Oct 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename to ease readbility
retyping to fit return type
slightly change their use for readability for humans and compilers godbolt

Copy link
Contributor

@Teufelchen1 Teufelchen1 Oct 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: You could go the extra mile and rename n to length/size/remaining_bytes and cnt to copied_bytes 😏

@kfessel kfessel added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Oct 13, 2025
@riot-ci
Copy link

riot-ci commented Oct 13, 2025

Murdock results

✔️ PASSED

ea64ee8 f-trub :spdx

Success Failures Total Runtime
10552 0 10552 13m:12s

Artifacts

@crasbe
Copy link
Contributor

crasbe commented Oct 14, 2025

Do you have any benchmark numbers or so to show that it actually improves the speed?

Also I'm not to keen on the turb header idea, IMO it would be more elegant to add tsrb_functionname_unsafe or so in the same header. This would avoid a bit of duplication I guess.

@kfessel
Copy link
Contributor Author

kfessel commented Oct 15, 2025

Do you have any benchmark numbers or so to show that it actually improves the speed?

Also I'm not to keen on the turb header idea, IMO it would be more elegant to add tsrb_functionname_unsafe or so in the same header. This would avoid a bit of duplication I guess.

i would not like to have them accessible through the same header ( could go for tsrb_unsafe.h though thread safe ... unsafe sound kindof strange ( first modifications went that route but then turb became) )

benchmarking the irq enable and disable -which this is mostly about- is very difficult we got the debug_irq_disable but it itself bogs down the irq enable and disable very much especially when its called as often as in the usb function ( basically with usb_acm the debug_irq_disable was not usable even when i replaced its printout) with this change it is

when i did debug_irq_disable the two functions where regularly exceeding 5000 cycles on an same5 mcu @120MHz with this change they are barely exceeding 1500 (rest on the application stayed the same) -- but as i said this does not tell much as debug_irq_disable cause a bunch of these cycles and its mostly gone with this change.

/*
* SPDX-FileCopyrightText: 2015 Kaspar Schleiser <[email protected]>
* SPDX-FileCopyrightText: 2025 Karl Fessel <[email protected]> ML!PA Consulting GmbH
* SPDX-License-Identifier: LGPL-2.1-only
Copy link
Contributor Author

@kfessel kfessel Oct 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i am a bit unsure about that only -- just a bit if its ok like this its fine for me

@maribu
Copy link
Member

maribu commented Oct 16, 2025

What is the motivation to have this in addition to https://doc.riot-os.org/cib_8h.html ?

Wouldn't cib already solve all the non-thread-safe ringbuffer needs?

@kfessel
Copy link
Contributor Author

kfessel commented Oct 16, 2025

What is the motivation to have this in addition to https://doc.riot-os.org/cib_8h.html ?

Wouldn't cib already solve all the non-thread-safe ringbuffer needs?

cib seems to have a very strange usage pattern, does not do the buffer handling but just the index,

  • has a put function that does not put
  • a get function that does not get
  • is called buffer without addressing the buffer or its content

turb is created to provide access to tsrb without extra thread safety measures ( cause they are taken/ensured by the caller )

  • it extends the interface to tsrb and is useful for mixed access ( tsrb and turb functions combined)

  • tsrb (and thus turb) could probaly have used cib for its counter handling needs (they seem to work slightly different but similar) but the author of tsrb probably saw cib and decided its better to not do that

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

Labels

Area: sys Area: System Area: USB Area: Universal Serial Bus CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants