Skip to content

Skip encoding defaults - #94

Merged
jcrist merged 5 commits into
masterfrom
skip-encoding-defaults
Mar 31, 2022
Merged

Skip encoding defaults#94
jcrist merged 5 commits into
masterfrom
skip-encoding-defaults

Conversation

@jcrist

@jcrist jcrist commented Mar 31, 2022

Copy link
Copy Markdown
Member

This adds an option to skip encoding fields if their value matches the default value. This is done on a best-effort basis - supporting generic == tests has a measurable performance cost. Rather, we check for common and efficient cases, opting for an efficient 90% solution rather than an inefficient 100% solution. In python code, the check would be something like:

def is_default(x, default):
    if x is default:
        return True
    if type(x) != type(default):
        return False
    if type(x) in (list, set, dict) and (len(x) == len(default) == 0):
        return True
    return False

If the value matches the default value, it's not encoded in the message body. For some messages with lots of optional fields, this can have a measurable impact on encoding an decoding performance.

Todo:

  • Maybe pick a better kwarg name
  • mypy support
  • Docs
  • Tests
  • Benchmark changes to ensure negligible performance slowdowns in happy path

jcrist added 5 commits March 31, 2022 01:47
The extra branching in `json_encode_struct` led to a minor slowdown,
splitting things out a bit reduces the cost here to something I'm more
comfortable with.
@jcrist jcrist changed the title WIP - skip encoding defaults Skip encoding defaults Mar 31, 2022
@jcrist
jcrist merged commit 9e2f8bd into master Mar 31, 2022
@jcrist
jcrist deleted the skip-encoding-defaults branch March 31, 2022 23:29
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