Skip to content

Add fakenamedtuple and use it for sys.implementation and os.uname() #1191

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
wants to merge 3 commits into from

Conversation

dpgeorge
Copy link
Member

This is my attempt at making an efficient (in ROM) namedtuple-like object, and using it to provide sys.implementation and os.uname().

See #1173 for background.

TODO:

  1. Decide if we want fakenamedtuple, or just use ordinary namedtuple (will lead to simpler code, but bigger in ROM).
  2. Provide a config variable to selectively compile these things.
  3. Decide exactly what info to provide in sys.implementation and os.uname() fields. The ones I provided are a good start I think.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.14%) to 93.01% when pulling 3c33846 on fakenamedtuple into 2bb5f41 on master.

@danicampora
Copy link
Member

Nice :-), I'll take a look at it today.

@pfalcon
Copy link
Contributor

pfalcon commented Apr 20, 2015

Well, I'd say this is neat solution (so neat, that someone may ask whether we need namedtuple, not fakenamedtuple, but the answer is that of course we need, because each namedtuple should be a separate type, while fakenamedtuple "fakes" it by having all its objects being of same type).

So, I guess the criteria should be: do we get any size savings with this patch, for expected 2-occurrence usage, comparing to encoding those 2 usages as proper namedtuples? If we do, it's certainly worth it.

But then would be nice to have more descriptive name than "fakednamedtuple".

@danicampora
Copy link
Member

I agree with Paul, the name could be improved.

On Apr 21, 2015, at 1:26 AM, Paul Sokolovsky notifications@github.com wrote:

Well, I'd say this is neat solution (so neat, that someone may ask whether we need namedtuple, not fakenamedtuple, but the answer is that of course we need, because each namedtuple should be a separate type, while fakenamedtuple "fakes" it by having all its objects being of same type).

So, I guess the criteria should be: do we get any size savings with this patch, for expected 2-occurrence usage, comparing to encoding those 2 usages as proper namedtuples? If we do, it's certainly worth it.

But then would be nice to have more descriptive name than "fakednamedtuple".


Reply to this email directly or view it on GitHub.

@dpgeorge
Copy link
Member Author

So, I guess the criteria should be: do we get any size savings with this patch

I haven't got precise numbers, but my original calculations said that 2 instances make it just worth it.

There are a few places in stmhal that I would have used a (fake)namedtuple had it been possible to put them in ROM (eg can.recv's return value). I think namedtuples are a great way to allow for new features while retaining backwards compat. Also it allows ports without much space to selectively include only relevant entries in a (fake)namedtuple and still be compatible with a full implementation of that tuple.

And don't forget wifi drivers wanting to use this as return values for scan() and ifconfig() etc.

With this efficient fakenamedtuple (or whatever it'll end up being named) I think we will start to use it liberally without fear of excessive ROM usage and make a much more user-friendly API.

Other name choices:

  • namespace
  • idtuple
  • attrtuple (I like this one)

@danicampora
Copy link
Member

  1. Decide exactly what info to provide in sys.implementation and os.uname() fields. The ones I provided are a good start I think.

I think the ones you already did are good enough for now.

Regarding the name choices, attrtuple is fine. What about statictuple?

@pfalcon
Copy link
Contributor

pfalcon commented Apr 21, 2015

I like attrtuple.

@dpgeorge
Copy link
Member Author

statictuple is already a thing, you just make static tuples :) With fakenamedtuple you can make static const versions and instances on the heap (eg as return results from wifi.scan()).

@danicampora
Copy link
Member

Make it attrtuple then :-)

@dpgeorge
Copy link
Member Author

Reworked and merged in 5aa311d, c3184ae and d8837ce.

It's called attrtuple and is enabled by MICROPY_PY_ATTRTUPLE.

@dpgeorge dpgeorge closed this Apr 21, 2015
@dpgeorge dpgeorge deleted the fakenamedtuple branch April 21, 2015 15:10
@dpgeorge
Copy link
Member Author

FYI, here are the stats on ROM usage, all for Thumb2 arch.

Without namedtuple, attrtuple costs 208 bytes of ROM.

With namedtuple, attrtuple costs 120 bytes of ROM (due to shared print function).

A new namedtuple type costs 16+N words, where N=number of fields in the tuple. Making an instance costs 2+N words.

A new attrtuple "type" costs N words (an array of qstrs). Making an instance costs 3+N words.

If you make M instances of a given tuple then the difference between namedtuple and attrtuple is 16-M words. That means if you make only 1 instance you save 15 words by using attrtuple. But if you make 16 or more instances it's better to use a namedtuple.

In the case of 1 instance (eg sys.implementation, os.uname()) you save 15 words = 60 bytes ROM by using attrtuple. So if you use attrtuple two times then it exactly breaks even (since savings = 2*60 = 120 = cost of attrtuple code).

Summary: attrtuple is worth it. Use it.

@danicampora
Copy link
Member

Summary: attrtuple is worth it. Use it.

Yes sir ;-)

tannewt added a commit to tannewt/circuitpython that referenced this pull request Feb 12, 2019
This changes a number of things in displayio:
* Introduces BuiltinFont and Glyph so the built in font can be used by libraries. For boards with
  a font it is available as board.TERMINAL_FONT. Fixes micropython#1172
* Remove _load_row from Bitmap in favor of bitmap[] access. Index can be x/y tuple or overall index. Fixes micropython#1191
* Add width and height properties to Bitmap.
* Add insert and [] access to Group. Fixes micropython#1518
* Add index param to pop on Group.
* Terminal no longer takes unicode character info. It takes a BuiltinFont instead.
* Fix Terminal's handling of [###D vt100 commands used when up arrowing into repl history.
* Add x and y positions to Group plus scale as well.
* Add bitmap accessor for BuiltinFont
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.

4 participants