-
Notifications
You must be signed in to change notification settings - Fork 219
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
Remove @dataclass, add slots #144
Conversation
I am not sure if optimising for performance over usability by default is a good thing. While, I can understand the motivation for this change, I feel that dropping dataclasses means you are loosing a lot of niceties that it brings to the table. I am also unclear on how much of a gain we are talking here. We might also want to talk about making it optional, ie. dataclasses vs slots at compile time. As an additional note, the alternative is to move from dataclasses to something that already fascilitates slots. The attrs project is a good option for that. Obviously, everything has trade-offs. :) |
It isn't dropping anything internally, it just removes the decorator. |
Ah, I might have spoke too soon then. Will go through the changes. Ignore my reactionary comment. :) |
Some tests ResultsTabled results are for a Message of: class Message(betterproto.Message):
foo: int = betterproto.uint32_field(0)
bar: int = betterproto.uint32_field(1)
baz: int = betterproto.uint32_field(2) unless specified otherwise. Important results:
Size of InstanceTested with 0, 3, 5, 10, 50 field large message instances. Using pympler.asizesof due to sys.getsizeof not being recursive.
Slots make messages significantly more linear in the amount of memory usage they end up using Runtime overhead
This is ridiculously faster with MessageMeta at around 20x Attribute Access
So it's about 40% faster with slots Instantiation
Currently instantiation with the current implementation is faster, however only marginally and I think the instantiation can be improved on the new model, or if not we can just use the dataclasses implementation, if it doesn't hurt runtime overhead particularly. |
I've optimized init more and it's now faster (by a couple of usec) than the dataclasses approach. |
Tests again will fail until #130 is merged |
# Conflicts: # src/betterproto/__init__.py # src/betterproto/templates/template.py.j2
poe bench currently returns
|
Not too sure why the getattr call isn't much of an improvement as it should be the same just |
I'm going to close this for now. I'm sure this can be done better but it requires a lot of work. |
Adds a custom metaclass for Message to quickly create dataclasses for Messages whilst also slotting them. Internally should still be basically the same as a dataclass.
Benefits:
This is incorporates 2 breaking changes:
@dataclass
from all generated outputs, since this is for V2 however I think it is fine, there are a couple of options if you want to give warning like monkey patching the dataclass decorator to do nothing for Message subclasses.betterproto.Placeholder
, but if necessary it could be remedied by implementingMessageMeta.__getattribute__
.Closes #50