Skip to content
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

Bounds checks elimination #348

Merged
merged 2 commits into from
Jun 16, 2024
Merged

Conversation

mhr3
Copy link
Contributor

@mhr3 mhr3 commented Jun 15, 2024

I've noticed msgp's integers.go isn't using compiler tricks to remove bounds checks, after adding those benchmarks show pretty nice speedup:

name                   old time/op  new time/op  delta
IntegersUnix/Get-8     2.23ns ± 1%  0.30ns ± 4%  -86.55%  (p=0.000 n=9+10)
IntegersUnix/Put-8     2.63ns ± 0%  0.80ns ±32%  -69.45%  (p=0.000 n=9+10)
IntegersPrefix/u16-8   0.66ns ±10%  0.41ns ± 0%  -38.71%  (p=0.000 n=10+8)
IntegersPrefix/u32-8   1.10ns ±33%  0.48ns ±25%  -56.60%  (p=0.000 n=10+9)
IntegersPrefix/u64-8   1.73ns ±38%  0.52ns ± 0%  -70.14%  (p=0.000 n=10+9)
Integers/Int64/Put-8   1.64ns ±13%  0.53ns ± 1%  -67.63%  (p=0.000 n=10+8)
Integers/Int64/Get-8   0.30ns ± 3%  0.29ns ± 4%   -3.73%  (p=0.006 n=8+10)
Integers/Int32/Put-8   0.94ns ± 4%  0.46ns ±10%  -50.81%  (p=0.000 n=10+8)
Integers/Int32/Get-8   0.31ns ± 3%  0.29ns ± 3%   -5.12%  (p=0.000 n=8+10)
Integers/Int16/Put-8   0.65ns ± 4%  0.48ns ±42%  -26.61%  (p=0.002 n=10+10)
Integers/Int16/Get-8   0.30ns ± 3%  0.29ns ± 1%   -3.03%  (p=0.000 n=8+9)
Integers/Uint64/Put-8  1.56ns ± 3%  0.62ns ±27%  -60.40%  (p=0.000 n=8+10)
Integers/Uint64/Get-8  0.30ns ± 3%  0.30ns ± 1%     ~     (p=0.127 n=10+10)
Integers/Uint32/Put-8  0.96ns ± 2%  0.45ns ± 7%  -52.98%  (p=0.000 n=9+8)
Integers/Uint32/Get-8  0.31ns ± 3%  0.30ns ± 2%   -2.29%  (p=0.002 n=9+9)
Integers/Uint16/Put-8  0.65ns ± 3%  0.41ns ± 8%  -36.59%  (p=0.000 n=10+9)
Integers/Uint16/Get-8  0.34ns ±28%  0.30ns ± 2%     ~     (p=0.075 n=10+9)

The most significant speedup can be seen in the unix put/get functions - for some reason the compiler wasn't inlining them, but after using functions from the binary pkg it now is. (though doing the same for all the other functions showed a slight slowdown, so I didn't change those)

@philhofer philhofer merged commit c029b2c into tinylib:master Jun 16, 2024
5 checks passed
@philhofer
Copy link
Member

Nice, thanks.

@klauspost
Copy link
Collaborator

Awesome find!

func getMint64(b []byte) int64 {
	return int64(binary.BigEndian.Uint64(b[1:9]))
}

... is a shorter version with only one check (spits out same code on amd64) - which is also inlined.

No need to change

@klauspost
Copy link
Collaborator

If you can get m.R.Next(...) and m.R.Peek(...) to inline the "happy path" that would probably give a similar speedup.

Tried it briefly, but even...

func (r *Reader) Next(n int) ([]byte, error) {
	// Have happy path be short and possible to inline.
	if len(r.data)-r.n >= n && r.state == nil {
		return r.data[r.n:], nil
	}
	return r.next(n)
}

// next is a helper function for Next to be called when the buffer does not contain n entries.
func (r *Reader) next(n int) ([]byte, error) {
...

...is too costly.

Also m.R.Peek(1) is so common that could have a specialized call func (r *Reader) PeekByte() (byte, error) - but again I couldn't get it to inline with some basic attempts.

It was slightly faster by having the "happy path" first, but nothing major. Maybe like 5%.

@mhr3
Copy link
Contributor Author

mhr3 commented Jun 17, 2024

Awesome find!

func getMint64(b []byte) int64 {
	return int64(binary.BigEndian.Uint64(b[1:9]))
}

... is a shorter version with only one check (spits out same code on amd64) - which is also inlined.

No need to change

I did try this, but from my benchmarking this was teeny bit slower (on arm64), did not check the generated assembly though.

@mhr3 mhr3 deleted the bounds-checks-elimination branch June 19, 2024 23:48
mx-psi pushed a commit to open-telemetry/opentelemetry-collector-contrib that referenced this pull request Jul 9, 2024
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [github.com/tinylib/msgp](https://github.com/tinylib/msgp) |
`v1.1.9` -> `v1.2.0` |
[![age](https://developer.mend.io/api/mc/badges/age/go/github.com%2ftinylib%2fmsgp/v1.2.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/go/github.com%2ftinylib%2fmsgp/v1.2.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/go/github.com%2ftinylib%2fmsgp/v1.1.9/v1.2.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/go/github.com%2ftinylib%2fmsgp/v1.1.9/v1.2.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

> [!WARNING]
> Some dependencies could not be looked up. Check the Dependency
Dashboard for more information.

---

### Release Notes

<details>
<summary>tinylib/msgp (github.com/tinylib/msgp)</summary>

### [`v1.2.0`](https://github.com/tinylib/msgp/releases/tag/v1.2.0)

[Compare
Source](https://github.com/tinylib/msgp/compare/v1.1.9...v1.2.0)

##### What's Changed

- omitzero implementation by
[@&#8203;bradleypeabody](https://github.com/bradleypeabody) in
[tinylib/msgp#334
- `replace` directive by
[@&#8203;infastin](https://github.com/infastin) in
[tinylib/msgp#346
- Support custom struct tags by
[@&#8203;very-amused](https://github.com/very-amused) in
[tinylib/msgp#347
- Add map\[string]struct{} support by
[@&#8203;klauspost](https://github.com/klauspost) in
[tinylib/msgp#339
- Add `any` support by [@&#8203;VirrageS](https://github.com/VirrageS)
in
[tinylib/msgp#350
- support for `any` maps in msgp.AppendIntf like in (\*Writer).WriteIntf
by [@&#8203;eliottness](https://github.com/eliottness) in
[tinylib/msgp#340
- allownil: Allocate 0 length slices by
[@&#8203;klauspost](https://github.com/klauspost) in
[tinylib/msgp#336
- Fix ignored tag directives on unnamed fields by
[@&#8203;klauspost](https://github.com/klauspost) in
[tinylib/msgp#352
- Added `time.Duration` case to AppendIntf by
[@&#8203;RiaanLaubscher](https://github.com/RiaanLaubscher) in
[tinylib/msgp#353
- Bounds checks elimination by [@&#8203;mhr3](https://github.com/mhr3)
in
[tinylib/msgp#348
- test AppendIntf against ReadIntfBytes by
[@&#8203;eliottness](https://github.com/eliottness) in
[tinylib/msgp#341
- Support illumos. by [@&#8203;jperkin](https://github.com/jperkin) in
[tinylib/msgp#343

##### New Contributors

- [@&#8203;eliottness](https://github.com/eliottness) made their first
contribution in
[tinylib/msgp#340
- [@&#8203;jperkin](https://github.com/jperkin) made their first
contribution in
[tinylib/msgp#343
- [@&#8203;very-amused](https://github.com/very-amused) made their
first contribution in
[tinylib/msgp#347
- [@&#8203;RiaanLaubscher](https://github.com/RiaanLaubscher) made
their first contribution in
[tinylib/msgp#353
- [@&#8203;infastin](https://github.com/infastin) made their first
contribution in
[tinylib/msgp#346

**Full Changelog**:
tinylib/msgp@v1.1.9...v1.2.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "on tuesday" (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 [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/open-telemetry/opentelemetry-collector-contrib).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy40MjUuMSIsInVwZGF0ZWRJblZlciI6IjM3LjQyNS4xIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6WyJkZXBlbmRlbmNpZXMiLCJyZW5vdmF0ZWJvdCJdfQ==-->

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: opentelemetrybot <107717825+opentelemetrybot@users.noreply.github.com>
codeboten added a commit to open-telemetry/opentelemetry-collector-contrib that referenced this pull request Aug 27, 2024
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [github.com/tinylib/msgp](https://github.com/tinylib/msgp) |
`v1.1.9` -> `v1.2.0` |
[![age](https://developer.mend.io/api/mc/badges/age/go/github.com%2ftinylib%2fmsgp/v1.2.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/go/github.com%2ftinylib%2fmsgp/v1.2.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/go/github.com%2ftinylib%2fmsgp/v1.1.9/v1.2.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/go/github.com%2ftinylib%2fmsgp/v1.1.9/v1.2.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

> [!WARNING]
> Some dependencies could not be looked up. Check the Dependency
Dashboard for more information.

---

### Release Notes

<details>
<summary>tinylib/msgp (github.com/tinylib/msgp)</summary>

### [`v1.2.0`](https://github.com/tinylib/msgp/releases/tag/v1.2.0)

[Compare
Source](https://github.com/tinylib/msgp/compare/v1.1.9...v1.2.0)

#### What's Changed

- omitzero implementation by
[@&#8203;bradleypeabody](https://github.com/bradleypeabody) in
[tinylib/msgp#334
- `replace` directive by
[@&#8203;infastin](https://github.com/infastin) in
[tinylib/msgp#346
- Support custom struct tags by
[@&#8203;very-amused](https://github.com/very-amused) in
[tinylib/msgp#347
- Add map\[string]struct{} support by
[@&#8203;klauspost](https://github.com/klauspost) in
[tinylib/msgp#339
- Add `any` support by [@&#8203;VirrageS](https://github.com/VirrageS)
in
[tinylib/msgp#350
- support for `any` maps in msgp.AppendIntf like in (\*Writer).WriteIntf
by [@&#8203;eliottness](https://github.com/eliottness) in
[tinylib/msgp#340
- allownil: Allocate 0 length slices by
[@&#8203;klauspost](https://github.com/klauspost) in
[tinylib/msgp#336
- Fix ignored tag directives on unnamed fields by
[@&#8203;klauspost](https://github.com/klauspost) in
[tinylib/msgp#352
- Added `time.Duration` case to AppendIntf by
[@&#8203;RiaanLaubscher](https://github.com/RiaanLaubscher) in
[tinylib/msgp#353
- Bounds checks elimination by [@&#8203;mhr3](https://github.com/mhr3)
in
[tinylib/msgp#348
- test AppendIntf against ReadIntfBytes by
[@&#8203;eliottness](https://github.com/eliottness) in
[tinylib/msgp#341
- Support illumos. by [@&#8203;jperkin](https://github.com/jperkin) in
[tinylib/msgp#343

#### New Contributors

- [@&#8203;eliottness](https://github.com/eliottness) made their first
contribution in
[tinylib/msgp#340
- [@&#8203;jperkin](https://github.com/jperkin) made their first
contribution in
[tinylib/msgp#343
- [@&#8203;very-amused](https://github.com/very-amused) made their
first contribution in
[tinylib/msgp#347
- [@&#8203;RiaanLaubscher](https://github.com/RiaanLaubscher) made
their first contribution in
[tinylib/msgp#353
- [@&#8203;infastin](https://github.com/infastin) made their first
contribution in
[tinylib/msgp#346

**Full Changelog**:
tinylib/msgp@v1.1.9...v1.2.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "on tuesday" (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 was generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View the
[repository job
log](https://developer.mend.io/github/open-telemetry/opentelemetry-collector-contrib).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzOC4yNi4xIiwidXBkYXRlZEluVmVyIjoiMzguMjYuMSIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOlsiZGVwZW5kZW5jaWVzIiwicmVub3ZhdGVib3QiXX0=-->

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: opentelemetrybot <107717825+opentelemetrybot@users.noreply.github.com>
Co-authored-by: Alex Boten <223565+codeboten@users.noreply.github.com>
f7o pushed a commit to f7o/opentelemetry-collector-contrib that referenced this pull request Sep 12, 2024
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [github.com/tinylib/msgp](https://github.com/tinylib/msgp) |
`v1.1.9` -> `v1.2.0` |
[![age](https://developer.mend.io/api/mc/badges/age/go/github.com%2ftinylib%2fmsgp/v1.2.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/go/github.com%2ftinylib%2fmsgp/v1.2.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/go/github.com%2ftinylib%2fmsgp/v1.1.9/v1.2.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/go/github.com%2ftinylib%2fmsgp/v1.1.9/v1.2.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

> [!WARNING]
> Some dependencies could not be looked up. Check the Dependency
Dashboard for more information.

---

### Release Notes

<details>
<summary>tinylib/msgp (github.com/tinylib/msgp)</summary>

### [`v1.2.0`](https://github.com/tinylib/msgp/releases/tag/v1.2.0)

[Compare
Source](https://github.com/tinylib/msgp/compare/v1.1.9...v1.2.0)

#### What's Changed

- omitzero implementation by
[@&open-telemetry#8203;bradleypeabody](https://github.com/bradleypeabody) in
[tinylib/msgp#334
- `replace` directive by
[@&open-telemetry#8203;infastin](https://github.com/infastin) in
[tinylib/msgp#346
- Support custom struct tags by
[@&open-telemetry#8203;very-amused](https://github.com/very-amused) in
[tinylib/msgp#347
- Add map\[string]struct{} support by
[@&open-telemetry#8203;klauspost](https://github.com/klauspost) in
[tinylib/msgp#339
- Add `any` support by [@&open-telemetry#8203;VirrageS](https://github.com/VirrageS)
in
[tinylib/msgp#350
- support for `any` maps in msgp.AppendIntf like in (\*Writer).WriteIntf
by [@&open-telemetry#8203;eliottness](https://github.com/eliottness) in
[tinylib/msgp#340
- allownil: Allocate 0 length slices by
[@&open-telemetry#8203;klauspost](https://github.com/klauspost) in
[tinylib/msgp#336
- Fix ignored tag directives on unnamed fields by
[@&open-telemetry#8203;klauspost](https://github.com/klauspost) in
[tinylib/msgp#352
- Added `time.Duration` case to AppendIntf by
[@&open-telemetry#8203;RiaanLaubscher](https://github.com/RiaanLaubscher) in
[tinylib/msgp#353
- Bounds checks elimination by [@&open-telemetry#8203;mhr3](https://github.com/mhr3)
in
[tinylib/msgp#348
- test AppendIntf against ReadIntfBytes by
[@&open-telemetry#8203;eliottness](https://github.com/eliottness) in
[tinylib/msgp#341
- Support illumos. by [@&open-telemetry#8203;jperkin](https://github.com/jperkin) in
[tinylib/msgp#343

#### New Contributors

- [@&open-telemetry#8203;eliottness](https://github.com/eliottness) made their first
contribution in
[tinylib/msgp#340
- [@&open-telemetry#8203;jperkin](https://github.com/jperkin) made their first
contribution in
[tinylib/msgp#343
- [@&open-telemetry#8203;very-amused](https://github.com/very-amused) made their
first contribution in
[tinylib/msgp#347
- [@&open-telemetry#8203;RiaanLaubscher](https://github.com/RiaanLaubscher) made
their first contribution in
[tinylib/msgp#353
- [@&open-telemetry#8203;infastin](https://github.com/infastin) made their first
contribution in
[tinylib/msgp#346

**Full Changelog**:
tinylib/msgp@v1.1.9...v1.2.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "on tuesday" (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 was generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View the
[repository job
log](https://developer.mend.io/github/open-telemetry/opentelemetry-collector-contrib).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzOC4yNi4xIiwidXBkYXRlZEluVmVyIjoiMzguMjYuMSIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOlsiZGVwZW5kZW5jaWVzIiwicmVub3ZhdGVib3QiXX0=-->

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: opentelemetrybot <107717825+opentelemetrybot@users.noreply.github.com>
Co-authored-by: Alex Boten <223565+codeboten@users.noreply.github.com>
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.

3 participants