Skip to content

Optimize with runtime.AddCleanup + more benchmarks#12

Merged
lemire merged 3 commits into
mainfrom
experiment
Mar 26, 2026
Merged

Optimize with runtime.AddCleanup + more benchmarks#12
lemire merged 3 commits into
mainfrom
experiment

Conversation

@lemire
Copy link
Copy Markdown
Member

@lemire lemire commented Mar 25, 2026

I am fairly happy that this PR could serve the basis of our first release.

Here is what grok tells me about the change:

runtime.AddCleanup is significantly more performant than the legacy runtime.SetFinalizer primarily because it avoids object resurrection. With SetFinalizer, the finalizer function receives the object itself as an argument, which forces the garbage collector to keep the object (and everything it references) specially reachable until the finalizer runs — often requiring at least two full GC cycles before the memory can actually be reclaimed, and causing extra work for the GC's mark phase. In contrast, AddCleanup passes a separate argument (arg S) to the cleanup function and never passes the original object, so the GC can immediately treat the object as unreachable and reclaim its memory in a single cycle, even when the object is part of a reference cycle. This design eliminates unnecessary GC overhead, reduces latency in memory reclamation, supports multiple cleanups per object without additional cost, and results in roughly 2x better performance for the registration and cleanup path compared to finalizers (while still being slower than pure manual deallocation). Overall, it leads to lower memory pressure, fewer GC pauses, and more predictable resource management in long-running Go programs.

@lemire lemire requested a review from anonrig March 25, 2026 20:42
@lemire lemire merged commit 590fc5d into main Mar 26, 2026
2 checks passed
@lemire lemire deleted the experiment branch March 26, 2026 18:13
NGTmeaty added a commit to internetarchive/Zeno that referenced this pull request Apr 9, 2026
This is no longer needed due to improvements from goada and infact is no longer present in the upstream API.

See: ada-url/goada#12
NGTmeaty added a commit to internetarchive/Zeno that referenced this pull request Apr 9, 2026
* chore(deps): bump the go-modules group with 5 updates

Bumps the go-modules group with 5 updates:

| Package | From | To |
| --- | --- | --- |
| [github.com/ada-url/goada](https://github.com/ada-url/goada) | `1.1.0` | `1.2.0` |
| [github.com/getsentry/sentry-go](https://github.com/getsentry/sentry-go) | `0.43.0` | `0.44.1` |
| [github.com/grafana/pyroscope-go](https://github.com/grafana/pyroscope-go) | `1.2.7` | `1.2.8` |
| [github.com/hashicorp/consul/api](https://github.com/hashicorp/consul) | `1.33.4` | `1.33.7` |
| [github.com/samber/slog-multi](https://github.com/samber/slog-multi) | `1.7.1` | `1.8.0` |


Updates `github.com/ada-url/goada` from 1.1.0 to 1.2.0
- [Release notes](https://github.com/ada-url/goada/releases)
- [Commits](ada-url/goada@v1.1.0...v1.2.0)

Updates `github.com/getsentry/sentry-go` from 0.43.0 to 0.44.1
- [Release notes](https://github.com/getsentry/sentry-go/releases)
- [Changelog](https://github.com/getsentry/sentry-go/blob/master/CHANGELOG.md)
- [Commits](getsentry/sentry-go@v0.43.0...v0.44.1)

Updates `github.com/grafana/pyroscope-go` from 1.2.7 to 1.2.8
- [Release notes](https://github.com/grafana/pyroscope-go/releases)
- [Commits](grafana/pyroscope-go@v1.2.7...v1.2.8)

Updates `github.com/hashicorp/consul/api` from 1.33.4 to 1.33.7
- [Release notes](https://github.com/hashicorp/consul/releases)
- [Changelog](https://github.com/hashicorp/consul/blob/main/CHANGELOG.md)
- [Commits](hashicorp/consul@api/v1.33.4...api/v1.33.7)

Updates `github.com/samber/slog-multi` from 1.7.1 to 1.8.0
- [Release notes](https://github.com/samber/slog-multi/releases)
- [Commits](samber/slog-multi@v1.7.1...v1.8.0)

---
updated-dependencies:
- dependency-name: github.com/ada-url/goada
  dependency-version: 1.2.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
  dependency-group: go-modules
- dependency-name: github.com/getsentry/sentry-go
  dependency-version: 0.44.1
  dependency-type: direct:production
  update-type: version-update:semver-minor
  dependency-group: go-modules
- dependency-name: github.com/grafana/pyroscope-go
  dependency-version: 1.2.8
  dependency-type: direct:production
  update-type: version-update:semver-patch
  dependency-group: go-modules
- dependency-name: github.com/hashicorp/consul/api
  dependency-version: 1.33.7
  dependency-type: direct:production
  update-type: version-update:semver-patch
  dependency-group: go-modules
- dependency-name: github.com/samber/slog-multi
  dependency-version: 1.8.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
  dependency-group: go-modules
...

Signed-off-by: dependabot[bot] <support@github.com>

* feat: remove free after parsing URL

This is no longer needed due to improvements from goada and infact is no longer present in the upstream API.

See: ada-url/goada#12

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Jake L <NGTmeaty@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.

2 participants