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

Test potential MustApplyAutoAliases speedup #1414

Merged
merged 6 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions pkg/tfbridge/auto_aliasing.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,12 @@ func (info *ProviderInfo) ApplyAutoAliases() error {
f()
}

if err := md.Set(artifact, aliasMetadataKey, hist); err != nil {
// Set fails only when `hist` is not serializable. Because `hist` is
// composed of marshallable, non-cyclic types, this is impossible.
contract.AssertNoErrorf(err, "History failed to serialize")
if isTfgen() {
if err := md.Set(artifact, aliasMetadataKey, hist); err != nil {
// Set fails only when `hist` is not serializable. Because `hist` is
// composed of marshallable, non-cyclic types, this is impossible.
contract.AssertNoErrorf(err, "History failed to serialize")
}
}

return nil
Expand Down Expand Up @@ -208,10 +210,12 @@ func aliasResource(
) {
prev, hasPrev := hist[tfToken]
if !hasPrev {
// It's not in the history, so it must be new. Stick it in the history for
// next time.
hist[tfToken] = &tokenHistory[tokens.Type]{
Current: computed.Tok,
if isTfgen() {
// It's not in the history, so it must be new. Stick it in the history for
// next time.
hist[tfToken] = &tokenHistory[tokens.Type]{
Current: computed.Tok,
}
}
} else {
// We don't do this eagerly because aliasResource is called while
Expand All @@ -226,7 +230,7 @@ func aliasResource(
// Note: If the user explicitly sets a MaxItemOne value, that value is respected
// and overwrites the current history.'

if res == nil {
if res == nil || hist[tfToken] == nil {
mjeffryes marked this conversation as resolved.
Show resolved Hide resolved
return
}

Expand Down Expand Up @@ -261,7 +265,7 @@ func applyResourceMaxItemsOneAliasing(
hasH = hasH || fieldHasHist
hasI = hasI || fieldHasInfo

if !hasH {
if !hasH && isTfgen() {
delete(*hist, k)
}
if !hasI {
Expand Down Expand Up @@ -320,19 +324,26 @@ func applyMaxItemsOneAliasing(schema shim.Schema, h *fieldHistory, info *SchemaI
// MaxItemsOne does not apply, so do nothing
} else if info.MaxItemsOne != nil {
// The user has overwritten the value, so we will just record that.
h.MaxItemsOne = info.MaxItemsOne
hasH = true
if isTfgen() {
h.MaxItemsOne = info.MaxItemsOne
hasH = true
}
} else if h.MaxItemsOne != nil {
// If we have a previous value in the history, we keep it as is.
info.MaxItemsOne = h.MaxItemsOne
hasI = true
} else {
} else if isTfgen() {
// There is no history for this value, so we bake it into the
// alias history.
h.MaxItemsOne = BoolRef(IsMaxItemsOne(schema, info))
hasH = true
}

// if h.Elem is null and we're not trying to generate updated history, we're done
if h.Elem == nil && !isTfgen() {
return hasH, hasI
}

// Ensure that the h.Elem and info.Elem fields are non-nil so they can be
// safely recursed on.
//
Expand Down Expand Up @@ -467,7 +478,6 @@ func aliasDataSource(
}

applyResourceMaxItemsOneAliasing(ds, &hist[tfToken].Fields, &computed.Fields)

}

func aliasOrRenameDataSource(
Expand Down
40 changes: 40 additions & 0 deletions pkg/tfbridge/runtime.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package tfbridge

import (
"runtime/debug"
"strings"
)

type runtimeStage int

const (
unknownStage runtimeStage = iota
tfgenStage
resourceStage
)

// Holds runtime flags
type runtimeInfo struct {
stage runtimeStage
}

var runtime = initRuntimeInfo()

func initRuntimeInfo() runtimeInfo {
Copy link
Member

Choose a reason for hiding this comment

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

Beautiful.

buildInfo, _ := debug.ReadBuildInfo()
stage := unknownStage
if buildInfo != nil {
if strings.Contains(buildInfo.Path, "pulumi-tfgen") {
stage = tfgenStage
} else if strings.Contains(buildInfo.Path, "pulumi-resource") {
stage = resourceStage
}
}
return runtimeInfo{
stage: stage,
}
}

func isTfgen() bool {
return runtime.stage != resourceStage
}
Loading