Skip to content

possible ~200 byte savings #4242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jepler opened this issue Feb 22, 2021 · 1 comment · Fixed by #4689
Closed

possible ~200 byte savings #4242

jepler opened this issue Feb 22, 2021 · 1 comment · Fixed by #4689

Comments

@jepler
Copy link

jepler commented Feb 22, 2021

cost: may use more stack

Most (all?) usb string descriptors use ASCII, but are stored as uint16_t[], and on metro m0 express these total 273 elements. If we "compress" them (store 8-bit values) we save half of that (546->273 bytes).

Tag the string pointers with the low bit to distinguish them; when requested, decompress them to a stack buffer, something like (untested)

      if ((intptr_t)desc_str & 1) {
        desc_str = (uint8_t*)((intptr_t)desc_str - 1);
        uint8_t desc_sz = desc_str[0];
        uint16_t desc_buf[desc_sc/2];
        buf[0] = desc_sz | 0x300;
        for(int i=1; i<desc_sz/2; i++) {
          desc_buf[i] = desc_str[i];
        }

This depends on it being OK for the buffer to not be static--I don't know if when tud_control_xfer returns, the buffer is "done with" or not.

This might work out or not, just writing it up in case someone wants to look into it further or in case of further need. It might need changes in tinyusb, or we might be able to do it inside tud_descriptor_string_cb, not sure.

@dhalbert
Copy link
Collaborator

dhalbert commented Feb 22, 2021

This is a nice savings for descriptors in flash. We have to make sure they are really transmitted before giving up the stack buffer. Once we go to some kind of dynamic descriptors, they will be constructed at runtime anyway, so we will just store the 8-bit strings, or even concatenate them at runtime. (There's a lot of duplication of "CircuitPython", for instance.)

@dhalbert dhalbert added this to the Long term milestone Feb 22, 2021
@dhalbert dhalbert added the usb label Feb 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants