Skip to content

UGLY CODE SIZE HACK#492

Draft
psychon wants to merge 1 commit intomasterfrom
parse-events-directly
Draft

UGLY CODE SIZE HACK#492
psychon wants to merge 1 commit intomasterfrom
parse-events-directly

Conversation

@psychon
Copy link
Copy Markdown
Owner

@psychon psychon commented Jun 19, 2020

On master, each events has an impl for TryFrom<&[u8]>. protocol::Event
uses this impl to parse the event and then wrap it in the Event enum.
This means that the result from the TryFrom implementation has to be
copied into the Event enum and the generated code actually contains such
a copy. Actually, it contains many such copy sequences, one per event.

This commit changes the structure a bit. Each event gets a function
ugly_hack(&[u8]) -> Result<Event, ParseError>. Thus, protocol::Event
only has to figure out which event this is and then call that event's
ugly_hack(). This function then immediately constructs an instance of
Event, thus avoiding the copy-bytes-around in the caller.

This commit changes the size of the stripped xclock_utc example in a
release build from 503 KiB to 495 KiB. This is a reduction in binary
size by about 2.5 %.

The assembler code for the full library goes from 159662 to 158847 lines
(0.5% less), but the object file (.o) keeps its size / does not change.

You might notice from the function name "ugly_hack" that I am not really
happy with this implementation. Instead, I wanted to check if the
general approach has merits. Apparently it does.

This is related to #488.

On master, each events has an impl for TryFrom<&[u8]>. protocol::Event
uses this impl to parse the event and then wrap it in the Event enum.
This means that the result from the TryFrom implementation has to be
copied into the Event enum and the generated code actually contains such
a copy. Actually, it contains many such copy sequences, one per event.

This commit changes the structure a bit. Each event gets a function
ugly_hack(&[u8]) -> Result<Event, ParseError>. Thus, protocol::Event
only has to figure out which event this is and then call that event's
ugly_hack(). This function then immediately constructs an instance of
Event, thus avoiding the copy-bytes-around in the caller.

This commit changes the size of the stripped xclock_utc example in a
release build from 503 KiB to 495 KiB. This is a reduction in binary
size by about 2.5 %.

The assembler code for the full library goes from 159662 to 158847 lines
(0.5% less), but the object file (.o) keeps its size / does not change.

You might notice from the function name "ugly_hack" that I am not really
happy with this implementation. Instead, I wanted to check if the
general approach has merits. Apparently it does.
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