Skip to content

usb-device-cdc: optmise CDCInterface constructor - #1138

Merged
dpgeorge merged 2 commits into
micropython:masterfrom
dpgeorge:usb-device-cdc-optimise
Jul 29, 2026
Merged

usb-device-cdc: optmise CDCInterface constructor#1138
dpgeorge merged 2 commits into
micropython:masterfrom
dpgeorge:usb-device-cdc-optimise

Conversation

@dpgeorge

@dpgeorge dpgeorge commented Jul 15, 2026

Copy link
Copy Markdown
Member

Summary

Don't set CDCInterface instance attributes that will just be reset by self.init().

Saves 30 bytes of bytecode.

Also simplify the associated example.

Testing

Built, and tested the example on RPI_PICO. It still works.

Trade-offs and Alternatives

This makes it less clear what exactly are the instance attributes, because their initialisation is done in .init(), not the constructor. The added comment tries to work around that.

Generative AI

Not used.

cdc = CDCInterface()
cdc.init(timeout=0) # zero timeout makes this non-blocking, suitable for os.dupterm()
# Zero timeout makes this non-blocking, suitable for os.dupterm().
cdc = CDCInterface(timeout=0)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We don't have to make this change, but I thought it was a little cleaner to set the timeout in the constructor.

@dpgeorge
dpgeorge requested a review from projectgus July 15, 2026 07:03
@dpgeorge

Copy link
Copy Markdown
Member Author

@projectgus curious what you think about this change? I can close the PR if you don't think it's a good idea.

@projectgus projectgus left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks like a nice optimisation to me!

dpgeorge added 2 commits July 29, 2026 17:57
Don't set instance attributes that will just be reset by `self.init()`.

Saves 30 bytes of bytecode.

Signed-off-by: Damien George <damien@micropython.org>
Makes the example a little bit simpler.

Signed-off-by: Damien George <damien@micropython.org>
@dpgeorge
dpgeorge force-pushed the usb-device-cdc-optimise branch from 1498098 to dbb3b45 Compare July 29, 2026 07:59
@dpgeorge
dpgeorge merged commit dbb3b45 into micropython:master Jul 29, 2026
5 checks passed
@dpgeorge
dpgeorge deleted the usb-device-cdc-optimise branch July 29, 2026 08:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants