Skip to content

Use string internment for constant strings #1412

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
dralley opened this issue Sep 24, 2019 · 5 comments
Closed

Use string internment for constant strings #1412

dralley opened this issue Sep 24, 2019 · 5 comments
Labels
RFC Request for comments

Comments

@dralley
Copy link
Contributor

dralley commented Sep 24, 2019

CPython and many other VMs do this, as it can save memory. Mostly it is a feature, but it results in slightly different behavior of the is keyword between RustPython and CPython. Theoretically if someone were relying on that behavior (not terribly common I would imagine, but I'm sure some people do) then their would not work the same on RustPython.

https://medium.com/@bdov_/https-medium-com-bdov-python-objects-part-iii-string-interning-625d3c7319de

@dralley dralley added the RFC Request for comments label Sep 24, 2019
@windelbouwman
Copy link
Contributor

In general I think this is a good idea. Both for performance reasons as well as for CPython compliance. Is there any limit to the amount of strings which are interned?

@dralley
Copy link
Contributor Author

dralley commented Sep 26, 2019

I don't think so. I have not looked deeply at the CPython implementation yet, but it seems like the vast majority of compile-time constants (apart from some exceptions) are interned. I'm not sure if any strings are interned at runtime.

https://rushter.com/blog/python-strings-and-memory/

function and class names
variable names
argument names
constants (all strings that are defined in the code)
keys of dictionaries
names of attributes

@coolreader18
Copy link
Member

Strings passed to sys.intern() are, but the VM doesn't decide to intern any strings other than constants.

@dralley
Copy link
Contributor Author

dralley commented Sep 26, 2019

@youknowone
Copy link
Member

@coolreader18 implemented it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RFC Request for comments
Projects
None yet
Development

No branches or pull requests

4 participants