Skip to content

Conversation

@kripken
Copy link
Member

@kripken kripken commented Aug 9, 2017

Instead of preallocating 5 bytes, emit the minimal bytes and move the code back.

The extra copying makes us a tiny bit slower to write (less than a quarter of a percent). But larger effect in code size than I thought, about 1% smaller. I guess it's because there are often small functions, and each got an extra 4 bytes earlier.

Fixes #1121.


enum {
// the maximum amount of bytes we emit per LEB
MAX_LEB32_BYTES = 5
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our existing style for enums mostly uses Capital format rather than all caps.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

if (sizeFieldSize != MAX_LEB32_BYTES) {
// we can save some room, nice
assert(sizeFieldSize < MAX_LEB32_BYTES);
std::move(&o[start + MAX_LEB32_BYTES], &o[start + MAX_LEB32_BYTES + size], &o[start + sizeFieldSize]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this supposed to be std::memmove?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, there's an algorithm for it now too: http://en.cppreference.com/w/cpp/algorithm/move

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though in this case it probably could be memmove; I wouldn't be surprised if it compiles to the same code though

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it could turn into a memmove. Seems cleaner to write move though, imo.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, hm. OTOH, this should be std::move_backward actually since the regions overlap.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this stuff confusing so I'm not sure, but this is moving to the left (we get rid of extra padding, we shrink), which these docs say should be move,

When moving overlapping ranges, std::move is appropriate when moving to the left (beginning of the destination range is outside the source range) while std::move_backward is appropriate when moving to the right (end of the destination range is outside the source range).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, right. Never mind. :-)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this std::move is fine, I just didn't know about it :). Let's #include <algorithm> at the top of the header though.

Copy link
Member

@dschuff dschuff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with the extra include.

@kripken kripken merged commit 21d06ae into master Aug 15, 2017
@kripken kripken deleted the small-lebs branch August 15, 2017 21:25
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.

4 participants