Skip to content

Commit

Permalink
refactor: avoid unnecessary calls to platforms.DefaultSpec()
Browse files Browse the repository at this point in the history
I came across a number of calls to `platforms.DefaultSpec()`
that were not necessary. That prompted me to take a look
various other similar places.

At least on Windows, the call is a has some cost till
doing a sycall `RtlGetNtVersionNumbers()`.

Signed-off-by: Anthony Nandaa <[email protected]>
  • Loading branch information
profnandaa committed Feb 18, 2025
1 parent 9facb2c commit bd98381
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 7 deletions.
4 changes: 3 additions & 1 deletion exporter/containerimage/exptypes/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func ParsePlatforms(meta map[string][]byte) (Platforms, error) {
return ps, nil
}

p := platforms.DefaultSpec()
var p ocispecs.Platform
if imgConfig, ok := meta[ExporterImageConfigKey]; ok {
var img ocispecs.Image
err := json.Unmarshal(imgConfig, &img)
Expand All @@ -51,6 +51,8 @@ func ParsePlatforms(meta map[string][]byte) (Platforms, error) {
} else if img.OS != "" || img.Architecture != "" {
return Platforms{}, errors.Errorf("invalid image config: os and architecture must be specified together")
}
} else {
p = platforms.DefaultSpec()
}
p = platforms.Normalize(p)
pk := platforms.FormatAll(p)
Expand Down
4 changes: 3 additions & 1 deletion frontend/dockerfile/builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,11 @@ func Build(ctx context.Context, c client.Client) (_ *client.Result, err error) {
return nil, nil, nil, err
}

p := platforms.DefaultSpec()
var p ocispecs.Platform
if platform != nil {
p = *platform
} else {
p = platforms.DefaultSpec()
}
scanTargets.Store(platforms.FormatAll(platforms.Normalize(p)), scanTarget)

Expand Down
4 changes: 3 additions & 1 deletion frontend/dockerui/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@ func (bc *Client) Build(ctx context.Context, fn BuildFunc) (*ResultBuilder, erro
}
}

p := platforms.DefaultSpec()
var p ocispecs.Platform
if tp != nil {
p = *tp
} else {
p = platforms.DefaultSpec()
}

// in certain conditions we allow input platform to be extended from base image
Expand Down
8 changes: 6 additions & 2 deletions frontend/dockerui/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,11 @@ func (bc *Client) BuildOpts() client.BuildOpts {
func (bc *Client) init() error {
opts := bc.bopts.Opts

defaultBuildPlatform := platforms.Normalize(platforms.DefaultSpec())
var defaultBuildPlatform ocispecs.Platform
if workers := bc.bopts.Workers; len(workers) > 0 && len(workers[0].Platforms) > 0 {
defaultBuildPlatform = workers[0].Platforms[0]
} else {
defaultBuildPlatform = platforms.Normalize(platforms.DefaultSpec())
}
buildPlatforms := []ocispecs.Platform{defaultBuildPlatform}
targetPlatforms := []ocispecs.Platform{}
Expand Down Expand Up @@ -459,9 +461,11 @@ func (bc *Client) NamedContext(name string, opt ContextOpt) (*NamedContext, erro
}
name = strings.TrimSuffix(reference.FamiliarString(named), ":latest")

pp := platforms.DefaultSpec()
var pp ocispecs.Platform
if opt.Platform != nil {
pp = *opt.Platform
} else {
pp = platforms.DefaultSpec()
}
pname := name + "::" + platforms.FormatAll(platforms.Normalize(pp))
nc, err := bc.namedContext(name, pname, opt)
Expand Down
4 changes: 3 additions & 1 deletion solver/llbsolver/ops/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (e *ExecOp) CacheMap(ctx context.Context, g session.Group, index int) (*sol
}
op.Meta.ProxyEnv = nil

p := platforms.DefaultSpec()
var p ocispecs.Platform
if e.platform != nil {
p = ocispecs.Platform{
OS: e.platform.OS,
Expand All @@ -132,6 +132,8 @@ func (e *ExecOp) CacheMap(ctx context.Context, g session.Group, index int) (*sol
OSVersion: e.platform.OSVersion,
OSFeatures: e.platform.OSFeatures,
}
} else {
p = platforms.DefaultSpec()
}

// Special case for cache compatibility with buggy versions that wrongly
Expand Down
6 changes: 5 additions & 1 deletion source/containerimage/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (is *Source) Identifier(scheme, ref string, attrs map[string]string, platfo
func (is *Source) Resolve(ctx context.Context, id source.Identifier, sm *session.Manager, vtx solver.Vertex) (source.SourceInstance, error) {
var (
p *puller
platform = platforms.DefaultSpec()
platform ocispecs.Platform
pullerUtil *pull.Puller
mode resolver.ResolveMode
recordType client.UsageRecordType
Expand Down Expand Up @@ -126,6 +126,10 @@ func (is *Source) Resolve(ctx context.Context, id source.Identifier, sm *session
default:
return nil, errors.Errorf("unknown resolver type: %v", is.ResolverType)
}

if &platform == nil {
platform = platforms.DefaultSpec()
}
pullerUtil = &pull.Puller{
ContentStore: is.ContentStore,
Platform: platform,
Expand Down

0 comments on commit bd98381

Please sign in to comment.