-
Notifications
You must be signed in to change notification settings - Fork 322
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
zstd: Replace bytes.Equal with smaller comparisons #695
Conversation
This replaces some bytes.Equal calls, which compile down to calls to runtime.memequal, with uint32 or string comparisons that become CMPL instructions (or a series of CMPBs, in one case).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bytes.Equal
is doing a string conversion so it is only a semantic change.
But it looks better.
Thanks. I see you've changed the commit message, but I assure you this is not a purely cosmetic change. The issue isn't really string == vs bytes.Equal, it's var vs const. If you compile the following file... package comparisons
import "bytes"
const sc = "abcd"
var sv = "abcd"
var bv = []byte("abcd")
func eqSc(p []byte) bool { return string(p[:4]) == sc }
func eqSv(p []byte) bool { return string(p[:4]) == sv }
func eqBv(p []byte) bool { return bytes.Equal(p[:4], bv) } then you'll find that eqSv and eqBv compile to practically identical code, a bounds check and a call to runtime.memequal. But eqSc compiles to a bounds check and
because the compiler knows the length of both arguments and the value of the latter. |
I tried comparing the The functions are typically called once per decode (per frame), so I don't expect to see it being anything measurable either way. I do think the code is nicer (except the |
…7575) This PR contains the following updates: | Package | Type | Update | Change | |---|---|---|---| | [github.com/klauspost/compress](https://togithub.com/klauspost/compress) | indirect | patch | `v1.15.11` -> `v1.15.15` | --- ### Release Notes <details> <summary>klauspost/compress</summary> ### [`v1.15.15`](https://togithub.com/klauspost/compress/releases/tag/v1.15.15) [Compare Source](https://togithub.com/klauspost/compress/compare/v1.15.14...v1.15.15) ##### What's Changed - zstd: Add delta encoding support by [@​greatroar](https://togithub.com/greatroar) in [https://github.com/klauspost/compress/pull/728](https://togithub.com/klauspost/compress/pull/728) - huff0: Reduce bounds checking by [@​greatroar](https://togithub.com/greatroar) in [https://github.com/klauspost/compress/pull/734](https://togithub.com/klauspost/compress/pull/734) - huff0: Assembler improvements by [@​greatroar](https://togithub.com/greatroar) in [https://github.com/klauspost/compress/pull/736](https://togithub.com/klauspost/compress/pull/736) - deflate: Improve level 7-9 by [@​klauspost](https://togithub.com/klauspost) in [https://github.com/klauspost/compress/pull/739](https://togithub.com/klauspost/compress/pull/739) - gzhttp: Add SuffixETag() and DropETag() options to prevent ETag collisions on compressed responses by [@​willbicks](https://togithub.com/willbicks) in [https://github.com/klauspost/compress/pull/740](https://togithub.com/klauspost/compress/pull/740) - zstd: Don't allocate dataStorage when using byteBuf by [@​klauspost](https://togithub.com/klauspost) in [https://github.com/klauspost/compress/pull/741](https://togithub.com/klauspost/compress/pull/741) - huff0: Speed up compression of short blocks by [@​greatroar](https://togithub.com/greatroar) in [https://github.com/klauspost/compress/pull/744](https://togithub.com/klauspost/compress/pull/744) - zstd: Handle dicts by pointer, always by [@​greatroar](https://togithub.com/greatroar) in [https://github.com/klauspost/compress/pull/743](https://togithub.com/klauspost/compress/pull/743) - fse: Optimize compression by [@​greatroar](https://togithub.com/greatroar) in [https://github.com/klauspost/compress/pull/745](https://togithub.com/klauspost/compress/pull/745) - Retract v1.14.1-v.1.14.3 by [@​klauspost](https://togithub.com/klauspost) in [https://github.com/klauspost/compress/pull/742](https://togithub.com/klauspost/compress/pull/742) ##### New Contributors - [@​willbicks](https://togithub.com/willbicks) made their first contribution in [https://github.com/klauspost/compress/pull/740](https://togithub.com/klauspost/compress/pull/740) **Full Changelog**: klauspost/compress@v1.15.14...v1.15.15 ### [`v1.15.14`](https://togithub.com/klauspost/compress/releases/tag/v1.15.14) [Compare Source](https://togithub.com/klauspost/compress/compare/v1.15.13...v1.15.14) #### What's Changed - flate: Improve speed in big stateless blocks. by [@​klauspost](https://togithub.com/klauspost) in [https://github.com/klauspost/compress/pull/718](https://togithub.com/klauspost/compress/pull/718) - zstd: Trigger BCE by switching on lengths by [@​greatroar](https://togithub.com/greatroar) in [https://github.com/klauspost/compress/pull/716](https://togithub.com/klauspost/compress/pull/716) - zstd: Shave some instructions off the amd64 asm by [@​greatroar](https://togithub.com/greatroar) in [https://github.com/klauspost/compress/pull/720](https://togithub.com/klauspost/compress/pull/720) - export NoGzipResponseWriter for custom ResponseWriter wrappers by [@​harshavardhana](https://togithub.com/harshavardhana) in [https://github.com/klauspost/compress/pull/722](https://togithub.com/klauspost/compress/pull/722) - s2: Add example for indexing and existing stream by [@​klauspost](https://togithub.com/klauspost) in [https://github.com/klauspost/compress/pull/723](https://togithub.com/klauspost/compress/pull/723) - tests: Tweak fuzz tests by [@​klauspost](https://togithub.com/klauspost) in [https://github.com/klauspost/compress/pull/719](https://togithub.com/klauspost/compress/pull/719) #### New Contributors - [@​harshavardhana](https://togithub.com/harshavardhana) made their first contribution in [https://github.com/klauspost/compress/pull/722](https://togithub.com/klauspost/compress/pull/722) **Full Changelog**: klauspost/compress@v1.15.13...v1.15.14 ### [`v1.15.13`](https://togithub.com/klauspost/compress/releases/tag/v1.15.13) [Compare Source](https://togithub.com/klauspost/compress/compare/v1.15.12...v1.15.13) #### What's Changed - zstd: Add MaxEncodedSize to encoder by [@​klauspost](https://togithub.com/klauspost) in [https://github.com/klauspost/compress/pull/691](https://togithub.com/klauspost/compress/pull/691) - zstd: Improve "best" end search by [@​klauspost](https://togithub.com/klauspost) in [https://github.com/klauspost/compress/pull/693](https://togithub.com/klauspost/compress/pull/693) - zstd: Replace bytes.Equal with smaller comparisons by [@​greatroar](https://togithub.com/greatroar) in [https://github.com/klauspost/compress/pull/695](https://togithub.com/klauspost/compress/pull/695) - zstd: Faster CRC checking/skipping by [@​greatroar](https://togithub.com/greatroar) in [https://github.com/klauspost/compress/pull/696](https://togithub.com/klauspost/compress/pull/696) - zstd: Rewrite matchLen to make it inlineable by [@​greatroar](https://togithub.com/greatroar) in [https://github.com/klauspost/compress/pull/701](https://togithub.com/klauspost/compress/pull/701) - zstd: Write table clearing in a way that the compiler recognizes by [@​greatroar](https://togithub.com/greatroar) in [https://github.com/klauspost/compress/pull/702](https://togithub.com/klauspost/compress/pull/702) - zstd: Use individual reset threshold by [@​klauspost](https://togithub.com/klauspost) in [https://github.com/klauspost/compress/pull/703](https://togithub.com/klauspost/compress/pull/703) - huff0: Check for zeros earlier in Scratch.countSimple by [@​greatroar](https://togithub.com/greatroar) in [https://github.com/klauspost/compress/pull/704](https://togithub.com/klauspost/compress/pull/704) - zstd: Improve best compression's match selection by [@​greatroar](https://togithub.com/greatroar) in [https://github.com/klauspost/compress/pull/705](https://togithub.com/klauspost/compress/pull/705) - zstd: Select best match using selection trees by [@​greatroar](https://togithub.com/greatroar) in [https://github.com/klauspost/compress/pull/706](https://togithub.com/klauspost/compress/pull/706) - zstd: sync xxhash with final accepted patch upstream by [@​lizthegrey](https://togithub.com/lizthegrey) in [https://github.com/klauspost/compress/pull/707](https://togithub.com/klauspost/compress/pull/707) - zstd: Import xxhash v2.2.0 by [@​greatroar](https://togithub.com/greatroar) in [https://github.com/klauspost/compress/pull/708](https://togithub.com/klauspost/compress/pull/708) **Full Changelog**: klauspost/compress@v1.15.12...v1.15.13 ### [`v1.15.12`](https://togithub.com/klauspost/compress/releases/tag/v1.15.12) [Compare Source](https://togithub.com/klauspost/compress/compare/v1.15.11...v1.15.12) ##### What's Changed - zstd: Tweak decoder allocs. by [@​klauspost](https://togithub.com/klauspost) in [https://github.com/klauspost/compress/pull/680](https://togithub.com/klauspost/compress/pull/680) - gzhttp: Always delete `HeaderNoCompression` by [@​klauspost](https://togithub.com/klauspost) in [https://github.com/klauspost/compress/pull/683](https://togithub.com/klauspost/compress/pull/683) **Full Changelog**: klauspost/compress@v1.15.11...v1.15.12 </details> --- ### Configuration 📅 **Schedule**: Branch creation - "before 3am on the first day of the month" (UTC), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Renovate Bot](https://togithub.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNC4xMDkuMSIsInVwZGF0ZWRJblZlciI6IjM0LjEwOS4xIn0=-->
This PR contains the following updates: | Package | Type | Update | Change | |---|---|---|---| | [github.com/klauspost/compress](https://togithub.com/klauspost/compress) | indirect | minor | `v1.15.11` -> `v1.16.0` | --- ### ⚠ Dependency Lookup Warnings ⚠ Warnings were logged while processing this repo. Please check the Dependency Dashboard for more information. --- ### Release Notes <details> <summary>klauspost/compress</summary> ### [`v1.16.0`](https://togithub.com/klauspost/compress/releases/tag/v1.16.0) [Compare Source](https://togithub.com/klauspost/compress/compare/v1.15.15...v1.16.0) #### What's Changed - s2: Add Dictionary support by [@​klauspost](https://togithub.com/klauspost) in [https://github.com/klauspost/compress/pull/685](https://togithub.com/klauspost/compress/pull/685) - s2: Add Compression Size Estimate by [@​klauspost](https://togithub.com/klauspost) in [https://github.com/klauspost/compress/pull/752](https://togithub.com/klauspost/compress/pull/752) - s2: Add support for custom stream encoder by [@​klauspost](https://togithub.com/klauspost) in [https://github.com/klauspost/compress/pull/755](https://togithub.com/klauspost/compress/pull/755) - s2: Add LZ4 block converter by [@​klauspost](https://togithub.com/klauspost) in [https://github.com/klauspost/compress/pull/748](https://togithub.com/klauspost/compress/pull/748) - s2: Support io.ReaderAt in ReadSeeker by [@​klauspost](https://togithub.com/klauspost) in [https://github.com/klauspost/compress/pull/747](https://togithub.com/klauspost/compress/pull/747) - s2c/s2sx: Use concurrent decoding by [@​klauspost](https://togithub.com/klauspost) in [https://github.com/klauspost/compress/pull/746](https://togithub.com/klauspost/compress/pull/746) - tests: Upgrade to Go 1.20 by [@​klauspost](https://togithub.com/klauspost) in [https://github.com/klauspost/compress/pull/749](https://togithub.com/klauspost/compress/pull/749) - Update all (command) dependencies by [@​klauspost](https://togithub.com/klauspost) in [https://github.com/klauspost/compress/pull/758](https://togithub.com/klauspost/compress/pull/758) **Full Changelog**: klauspost/compress@v1.15.15...v1.16.0 ### [`v1.15.15`](https://togithub.com/klauspost/compress/releases/tag/v1.15.15) [Compare Source](https://togithub.com/klauspost/compress/compare/v1.15.14...v1.15.15) #### What's Changed - zstd: Add delta encoding support by [@​greatroar](https://togithub.com/greatroar) in [https://github.com/klauspost/compress/pull/728](https://togithub.com/klauspost/compress/pull/728) - huff0: Reduce bounds checking by [@​greatroar](https://togithub.com/greatroar) in [https://github.com/klauspost/compress/pull/734](https://togithub.com/klauspost/compress/pull/734) - huff0: Assembler improvements by [@​greatroar](https://togithub.com/greatroar) in [https://github.com/klauspost/compress/pull/736](https://togithub.com/klauspost/compress/pull/736) - deflate: Improve level 7-9 by [@​klauspost](https://togithub.com/klauspost) in [https://github.com/klauspost/compress/pull/739](https://togithub.com/klauspost/compress/pull/739) - gzhttp: Add SuffixETag() and DropETag() options to prevent ETag collisions on compressed responses by [@​willbicks](https://togithub.com/willbicks) in [https://github.com/klauspost/compress/pull/740](https://togithub.com/klauspost/compress/pull/740) - zstd: Don't allocate dataStorage when using byteBuf by [@​klauspost](https://togithub.com/klauspost) in [https://github.com/klauspost/compress/pull/741](https://togithub.com/klauspost/compress/pull/741) - huff0: Speed up compression of short blocks by [@​greatroar](https://togithub.com/greatroar) in [https://github.com/klauspost/compress/pull/744](https://togithub.com/klauspost/compress/pull/744) - zstd: Handle dicts by pointer, always by [@​greatroar](https://togithub.com/greatroar) in [https://github.com/klauspost/compress/pull/743](https://togithub.com/klauspost/compress/pull/743) - fse: Optimize compression by [@​greatroar](https://togithub.com/greatroar) in [https://github.com/klauspost/compress/pull/745](https://togithub.com/klauspost/compress/pull/745) - Retract v1.14.1-v.1.14.3 by [@​klauspost](https://togithub.com/klauspost) in [https://github.com/klauspost/compress/pull/742](https://togithub.com/klauspost/compress/pull/742) #### New Contributors - [@​willbicks](https://togithub.com/willbicks) made their first contribution in [https://github.com/klauspost/compress/pull/740](https://togithub.com/klauspost/compress/pull/740) **Full Changelog**: klauspost/compress@v1.15.14...v1.15.15 ### [`v1.15.14`](https://togithub.com/klauspost/compress/releases/tag/v1.15.14) [Compare Source](https://togithub.com/klauspost/compress/compare/v1.15.13...v1.15.14) #### What's Changed - flate: Improve speed in big stateless blocks. by [@​klauspost](https://togithub.com/klauspost) in [https://github.com/klauspost/compress/pull/718](https://togithub.com/klauspost/compress/pull/718) - zstd: Trigger BCE by switching on lengths by [@​greatroar](https://togithub.com/greatroar) in [https://github.com/klauspost/compress/pull/716](https://togithub.com/klauspost/compress/pull/716) - zstd: Shave some instructions off the amd64 asm by [@​greatroar](https://togithub.com/greatroar) in [https://github.com/klauspost/compress/pull/720](https://togithub.com/klauspost/compress/pull/720) - export NoGzipResponseWriter for custom ResponseWriter wrappers by [@​harshavardhana](https://togithub.com/harshavardhana) in [https://github.com/klauspost/compress/pull/722](https://togithub.com/klauspost/compress/pull/722) - s2: Add example for indexing and existing stream by [@​klauspost](https://togithub.com/klauspost) in [https://github.com/klauspost/compress/pull/723](https://togithub.com/klauspost/compress/pull/723) - tests: Tweak fuzz tests by [@​klauspost](https://togithub.com/klauspost) in [https://github.com/klauspost/compress/pull/719](https://togithub.com/klauspost/compress/pull/719) #### New Contributors - [@​harshavardhana](https://togithub.com/harshavardhana) made their first contribution in [https://github.com/klauspost/compress/pull/722](https://togithub.com/klauspost/compress/pull/722) **Full Changelog**: klauspost/compress@v1.15.13...v1.15.14 ### [`v1.15.13`](https://togithub.com/klauspost/compress/releases/tag/v1.15.13) [Compare Source](https://togithub.com/klauspost/compress/compare/v1.15.12...v1.15.13) #### What's Changed - zstd: Add MaxEncodedSize to encoder by [@​klauspost](https://togithub.com/klauspost) in [https://github.com/klauspost/compress/pull/691](https://togithub.com/klauspost/compress/pull/691) - zstd: Improve "best" end search by [@​klauspost](https://togithub.com/klauspost) in [https://github.com/klauspost/compress/pull/693](https://togithub.com/klauspost/compress/pull/693) - zstd: Replace bytes.Equal with smaller comparisons by [@​greatroar](https://togithub.com/greatroar) in [https://github.com/klauspost/compress/pull/695](https://togithub.com/klauspost/compress/pull/695) - zstd: Faster CRC checking/skipping by [@​greatroar](https://togithub.com/greatroar) in [https://github.com/klauspost/compress/pull/696](https://togithub.com/klauspost/compress/pull/696) - zstd: Rewrite matchLen to make it inlineable by [@​greatroar](https://togithub.com/greatroar) in [https://github.com/klauspost/compress/pull/701](https://togithub.com/klauspost/compress/pull/701) - zstd: Write table clearing in a way that the compiler recognizes by [@​greatroar](https://togithub.com/greatroar) in [https://github.com/klauspost/compress/pull/702](https://togithub.com/klauspost/compress/pull/702) - zstd: Use individual reset threshold by [@​klauspost](https://togithub.com/klauspost) in [https://github.com/klauspost/compress/pull/703](https://togithub.com/klauspost/compress/pull/703) - huff0: Check for zeros earlier in Scratch.countSimple by [@​greatroar](https://togithub.com/greatroar) in [https://github.com/klauspost/compress/pull/704](https://togithub.com/klauspost/compress/pull/704) - zstd: Improve best compression's match selection by [@​greatroar](https://togithub.com/greatroar) in [https://github.com/klauspost/compress/pull/705](https://togithub.com/klauspost/compress/pull/705) - zstd: Select best match using selection trees by [@​greatroar](https://togithub.com/greatroar) in [https://github.com/klauspost/compress/pull/706](https://togithub.com/klauspost/compress/pull/706) - zstd: sync xxhash with final accepted patch upstream by [@​lizthegrey](https://togithub.com/lizthegrey) in [https://github.com/klauspost/compress/pull/707](https://togithub.com/klauspost/compress/pull/707) - zstd: Import xxhash v2.2.0 by [@​greatroar](https://togithub.com/greatroar) in [https://github.com/klauspost/compress/pull/708](https://togithub.com/klauspost/compress/pull/708) **Full Changelog**: klauspost/compress@v1.15.12...v1.15.13 ### [`v1.15.12`](https://togithub.com/klauspost/compress/releases/tag/v1.15.12) [Compare Source](https://togithub.com/klauspost/compress/compare/v1.15.11...v1.15.12) #### What's Changed - zstd: Tweak decoder allocs. by [@​klauspost](https://togithub.com/klauspost) in [https://github.com/klauspost/compress/pull/680](https://togithub.com/klauspost/compress/pull/680) - gzhttp: Always delete `HeaderNoCompression` by [@​klauspost](https://togithub.com/klauspost) in [https://github.com/klauspost/compress/pull/683](https://togithub.com/klauspost/compress/pull/683) **Full Changelog**: klauspost/compress@v1.15.11...v1.15.12 </details> --- ### Configuration 📅 **Schedule**: Branch creation - "before 3am on the first day of the month" (UTC), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Renovate Bot](https://togithub.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNC4xMDkuMSIsInVwZGF0ZWRJblZlciI6IjM0LjE1NC4wIn0=-->
This replaces some bytes.Equal calls, which compile down to calls to runtime.memequal, with uint32 or string comparisons that become CMPL instructions (or a series of CMPBs, in one case).
The code is several dozen instructions shorter. Benchmarks show no regressions: