Skip to content
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

Micro-optimisations #367

Draft
wants to merge 30 commits into
base: master
Choose a base branch
from
Draft

Conversation

Gobot1234
Copy link
Collaborator

@Gobot1234 Gobot1234 commented Apr 11, 2022

In no particular order:

  • Remove abc.ABC as base class for Message - after asking there was no reason for this, saves a bit of Message type creation time
  • Use frozensets for contains checks - as all the TYPE_x strings and integers are interned by the compiler there's no reason to not do this, this makes container checks ~4x faster (200ns vs 50ns) but this is obviously performed a lot of times in BetterProto.
  • Inline functions where possible - this inlines the _pack_fmt call and pre-compiles the struct.Struct instances for each packing string.
  • Don't create a bytearray for each call to _serialize_single - initially when I made this optimisation I don't think I realised there wasn't much point in doing this as it's called so frequently so avoiding the global lookup and call and then taking the hit of concatenating the bytes is fine.
  • Optimise for the normal case for dict.getitem - Calling dict.get when the key is very likely to be there is unneccesary.
  • Get rid of __raw_get - This avoids calling Message.__getattribute__ the call frame and super() initialisation.
  • Added __slots__ to betterproto.Message this reduces size in memory and doesn't change code for the end user (they can still add dynamic attributes)
  • Optimise from_dict slightly better - Reduces the complexity of from_dict, the number of comparisions and dictionary lookups (might also need reverting)

@Gobot1234 Gobot1234 marked this pull request as draft April 11, 2022 23:50
@Gobot1234 Gobot1234 marked this pull request as ready for review April 23, 2022 10:36
I remember initially being confused as to how this ever worked as enum members to my knowledge were never callable, however, as it turns out Casing.CAMEL and SNAKE never actually were enum members
@Gobot1234 Gobot1234 marked this pull request as draft August 31, 2022 18:04
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.

1 participant