Skip to content

Implement __bool__ and __len__ via unary_op virtual method for all types. #238

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

Merged
merged 1 commit into from
Jan 30, 2014

Conversation

pfalcon
Copy link
Contributor

@pfalcon pfalcon commented Jan 30, 2014

Per #229

bool() and len() are just the same as neg() or invert(),
and require efficient dispatching implementation (not requiring search/lookup).
type->unary_op() is just the right choice for this short of adding
standalone virtual method(s) to already big mp_obj_type_t structure.

…pes.

__bool__() and __len__() are just the same as __neg__() or __invert__(),
and require efficient dispatching implementation (not requiring search/lookup).
type->unary_op() is just the right choice for this short of adding
standalone virtual method(s) to already big mp_obj_type_t structure.
@pfalcon
Copy link
Contributor Author

pfalcon commented Jan 30, 2014

And as a note, with this changes, uPy self-hosts unix versions of mandel.py and conwaylife.py (which was the motivation for starting to look into these changes).

@dpgeorge
Copy link
Member

It is neat. Still need to implement __bool__ for custom defined classes writting in Python.

Don't know what you mean by self hosts... mandel.py and conwaylife.py always worked with unix/micropython.

dpgeorge added a commit that referenced this pull request Jan 30, 2014
Implement __bool__ and __len__ via unary_op virtual method for all types.
@dpgeorge dpgeorge merged commit b25711e into micropython:master Jan 30, 2014
@pfalcon
Copy link
Contributor Author

pfalcon commented Jan 30, 2014

It is neat. Still need to implement bool for custom defined classes writting in Python.

Yes, as bunch of other methods - __str__, __repr__, etc. All these would follow same pattern as binary_ops you implemented - to use a "trampoline" code which will translate virtual method call into look up by name. Oh, there's __del__ too ;-). You don't think that stm would need support for such finalization? (My wild guess based on lack of such virtual method so far.)

Don't know what you mean by self hosts... mandel.py and conwaylife.py always worked with unix/micropython.

Not for me. It didn't work when I submitted patch for them (and lot of list stuff was missing then), and this time it failed on some ops for long ints, in particular, in true check (I was a bit surprised that rnd() implementation pushes numbers into long int, but that's it).

@dpgeorge
Copy link
Member

Probably need __del__. But with a GC, it would need to be explicitly called.

Examples all work for me on my 32 bit machine, and 64 bits. Do they work for you now?

@pfalcon pfalcon deleted the bool_len branch January 30, 2014 11:59
@pfalcon
Copy link
Contributor Author

pfalcon commented Jan 30, 2014

Do they work for you now?

Yes, both mandel and conwaylife work for me. I tried to time conwaylife with uPy and CPy3.3, and uPy with just by a slightest edge. But that's not informative, because most time is spent in screen output. We'll need to think about better benchmark.

Probably need del.

FFI module shows that already need ;-).

But with a GC, it would need to be explicitly called.

What about calling it at sweep stage? (Yes, sure, that makes GC all suddenly much slow, so as an option; also, intuitively, one could try to "optimize" GC implementation by splitting 2-bit flags into two 1-bit vectors (or interleaving words); unclear how much that will actually improve).

@dpgeorge
Copy link
Member

We'll need to think about better benchmark.

I just found that -O3 was not being applied to vm.c or gc.c. It now is, and uPy should be 20% faster than it has been for the last week.

What about calling it at sweep stage?

Don't think that's possible. The GC heap can contain non-uPy objects and you can't tell what's an object and what's not... Would need to split it into 2 heaps to support sweep calling del.

@pfalcon
Copy link
Contributor Author

pfalcon commented Jan 30, 2014

The GC heap can contain non-uPy objects and you can't tell what's an object and what's not...

Ah right...

Would need to split it into 2 heaps

Or force type header on all allocations to make it precise, or have impl without conservative guarantees, which may segfault once in a while ;-).

@dpgeorge
Copy link
Member

Actually, a type header, 1 bit, would be a nice addition. 2 types: either data, or a uPy object with a mp_obj_base_t as the first word, which can point to a deconstructor.

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