Skip to content

docs/reference/speed_python: Clarify RAM usage of string concatenation. - #19602

Open
agatti wants to merge 1 commit into
micropython:masterfrom
agatti:update-string-folding-docs
Open

docs/reference/speed_python: Clarify RAM usage of string concatenation.#19602
agatti wants to merge 1 commit into
micropython:masterfrom
agatti:update-string-folding-docs

Conversation

@agatti

@agatti agatti commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR adds a brief clarification of what happens when strings are concatenated, and how that should be avoided if concatenation occurs between string literals.

The parser won't perform any literal folding, so multiple string literal fragments concatenated together will be added to the QSTR table even if all they are used for is to build another string.

This is a follow-up of #19598, in which having literal strings folding in the parser was deemed not worthwhile, and a documentation update could have served a similar purpose.

Testing

The documentation was built with make html to be sure it looked correct.

Generative AI

I did not use generative AI tools when creating this PR.

@agatti agatti added the docs label Aug 11, 2026
Comment thread docs/reference/speed_python.rst Outdated
the concatenation do not appear elsewhere, that's RAM that could have been saved
by storing the final version of the string in the source to begin with. Long
strings can be split using the string concatenation operator ``\`` and still be
considered one single string by MicroPython:

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.

Perhaps worth pointing out that string continuation ( "A" "B" """"C"""" ) is a Python feature, and works with both single and triple quoted strings.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for mentioning triple-quoted strings, I've added a mention to them (along with cleaning things up).

I originally added a triple-quoted string to the code snippet, but then decided against, since they're meant to be used to encode multi-line strings. Combining regular and multiline strings would then need a brief mention of the fact that the latter will have embedded newlines in them, and so on. It felt a bit out of place in a section that mentioned performance and memory savings.

@agatti
agatti force-pushed the update-string-folding-docs branch 2 times, most recently from 9d8917c to 8f97d41 Compare August 11, 2026 13:54
This commit adds a brief clarification of what happens when strings are
concatenated, and how that should be avoided if concatenation occurs
between string literals.

The parser won't perform any literal folding, so multiple string literal
fragments concatenated together will be added to the QSTR table even if
all they are used for is to build another string.

Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
@agatti
agatti force-pushed the update-string-folding-docs branch from 8f97d41 to 31d0253 Compare August 11, 2026 13:59
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