Skip to content

Huffman Bug Fix#699

Merged
dr-orlovsky merged 2 commits intorust-bitcoin:masterfrom
JeremyRubin:huffman-cleanups
Nov 23, 2021
Merged

Huffman Bug Fix#699
dr-orlovsky merged 2 commits intorust-bitcoin:masterfrom
JeremyRubin:huffman-cleanups

Conversation

@JeremyRubin
Copy link
Copy Markdown
Contributor

I noticed one cleanup & one bugfix while looking into the huffman algorithm:

  1. the cleanup: we can use a u128 to guarantee no overflows, and saturating_add to guarantee reasonable behavior in any case
  2. the bug: the binary heap is a max heap so the behavior ends up merging the nodes of the most likely entries repeatedly. a huffman encoder requires merging the least likely elements, so it should be reversed.

@JeremyRubin
Copy link
Copy Markdown
Contributor Author

a currently failing test #700

that passes if rebased on this branch.

Copy link
Copy Markdown
Collaborator

@Kixunil Kixunil left a comment

Choose a reason for hiding this comment

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

I think u128 is not supported on all platforms. Could it be a problem for embedded?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

If it can never happen, why not just have +?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

i'd rather define the behavior correctly here should it ever overflow rather than wrapping around. Perhaaps defensively, but it'd be possible someone were to update the types/sizes later without appreciating this point.

@JeremyRubin
Copy link
Copy Markdown
Contributor Author

I think u128 is supported on all platforms, either with a native type or a wrapper. We could also do the same trick with u32 and u64 and it wouldn't be an issue either (u32 is plenty IMO).

Copy link
Copy Markdown
Member

@sanket1729 sanket1729 left a comment

Choose a reason for hiding this comment

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

Thanks for testing this. ACK modulo removing the error variant.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you also remove the Error variant from the error enum?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

@sanket1729 sanket1729 added the bug label Nov 15, 2021
@sanket1729 sanket1729 added this to the 0.28.0 milestone Nov 15, 2021
Copy link
Copy Markdown
Member

@sanket1729 sanket1729 left a comment

Choose a reason for hiding this comment

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

ACK f2a6827

Copy link
Copy Markdown
Collaborator

@dr-orlovsky dr-orlovsky left a comment

Choose a reason for hiding this comment

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

utACK f2a6827

@dr-orlovsky dr-orlovsky merged commit 5286d0a into rust-bitcoin:master Nov 23, 2021
dr-orlovsky added a commit that referenced this pull request Dec 11, 2021
1518517 Decrease Huffman weight type to 32 bits (Jeremy Rubin)

Pull request description:

  This builds on #699 but is the more bikesheddable part since it changes the API.

  > u32 of weight should be enough for any branch.
  -- Bill Gates

ACKs for top commit:
  dr-orlovsky:
    utACK 1518517
  Kixunil:
    ACK 1518517

Tree-SHA512: 9c507ae6129dda8dc069b0a142181a78cf89cb3ebf9d2169c46662822cb4ea9ed075bf484528f5399fe0ed383a425174a702e2d685f31c246f5a86c46ed17c3a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants