The Wayback Machine - https://web.archive.org/web/20251014182252/https://github.com/moby/moby/pull/49241
Skip to content

Conversation

vvoland
Copy link
Contributor

@vvoland vvoland commented Jan 9, 2025

Deprecate in favor of runtime.NumCPU as the behavior is the same now.

- Description for the changelog

Go SDK: pkg/sysinfo: deprecate NumCPU. This utility has the same behavior as runtime.NumCPU.

- A picture of a cute animal (not mandatory but encouraged)

Comment on lines -15 to -25
// Returns bit count of 1, used by NumCPU
func popcnt(x uint64) (n byte) {
x -= (x >> 1) & 0x5555555555555555
x = (x>>2)&0x3333333333333333 + x&0x3333333333333333
x += x >> 4
x &= 0x0f0f0f0f0f0f0f0f
x *= 0x0101010101010101
return byte(x >> 56)
}

func numCPU() int {
Copy link
Member

Choose a reason for hiding this comment

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

Was the Windows implementation also updated in go's stdlib?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes

https://cs.opensource.google/go/go/+/refs/tags/go1.23.4:src/runtime/os_windows.go;l=325-344

The only difference is that it used different (possibly less efficient?) popcnt implementation.

It was updated in commit https://cs.opensource.google/go/go/+/6410e67a1eb38df3cc72cef818ed392bea907251

Copy link
Member

Choose a reason for hiding this comment

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

Nice!!!

Copy link
Member

Choose a reason for hiding this comment

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

Looks like it was in go1.6 already!? golang/go@6410e67

Screenshot 2025-01-09 at 13 51 31

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, looks like it

Comment on lines 7 to 9
// NumCPU returns the number of CPUs. It's the equivalent of [runtime.NumCPU].
// Deprecated: Use [runtime.NumCPU] instead. It will be removed in the next release.
var NumCPU = runtime.NumCPU()
Copy link
Member

Choose a reason for hiding this comment

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

This needs an empty line before the Deprecated comment, otherwise it's not detected as "Deprecated".

It's also slightly better to create a wrapper function here; both because we don't want any code from overriding the exported alias, but also because otherwise it shows up under variables on pkg.go.dev, which can be confusing.

Suggested change
// NumCPU returns the number of CPUs. It's the equivalent of [runtime.NumCPU].
// Deprecated: Use [runtime.NumCPU] instead. It will be removed in the next release.
var NumCPU = runtime.NumCPU()
// NumCPU returns the number of CPUs. It's the equivalent of [runtime.NumCPU].
//
// Deprecated: Use [runtime.NumCPU] instead. It will be removed in the next release.
func NumCPU() int {
return runtime.NumCPU()
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, good catch, let me update

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I always forget about the required newline before Deprecated 🙈
Sounds like we should teach our linter to detect that 😅

Copy link
Member

Choose a reason for hiding this comment

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

Yes, actually surprised there's no linter picking on those currently!

for the "wrapper vs direct alias"; both options are OK if things get too complicated (sometimes the wrapper means we now have to import other packages, which may not be worth it in some cases), but in this case it was trivial to change.

Deprecate in favor of `runtime.NumCPU` as the behavior is the same now.

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
Copy link
Member

@thaJeztah thaJeztah left a comment

Choose a reason for hiding this comment

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

@thaJeztah
Copy link
Member

cc @AkihiroSuda FYI; you probably can remove this from your nerdctl fork as well

Architecture: platform.Architecture(),
RegistryConfig: doWithTrace(ctx, "registry.ServiceConfig", daemon.registryService.ServiceConfig),
NCPU: doWithTrace(ctx, "sysinfo.NumCPU", sysinfo.NumCPU),
NCPU: doWithTrace(ctx, "runtime.NumCPU", runtime.NumCPU),
Copy link
Member

Choose a reason for hiding this comment

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

Nit; we could even consider removing the doWithtrace here, as it would now be tracing go stdlib; not sure if there's much use in that.

@thaJeztah thaJeztah merged commit 45fe686 into moby:master Jan 9, 2025
140 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants