Skip to content

Commit

Permalink
Improve performance of AttachResourceConfigTransformer on big graphs
Browse files Browse the repository at this point in the history
The current implementation of `AttachResourceConfigTransformer` has performance of `O(N^2)` to the number of vertices in the graph, and this leads to significant performance degradation on huge graphs.

The given implementation decreases the complexity to `O(N)` by performing direct lookup of a resource in the `ManagedResources` or `DataResources` depending on the object type.

Signed-off-by: Alex Ott <alexott@gmail.com>
  • Loading branch information
alexott committed Apr 26, 2024
1 parent 660ff86 commit 30396fd
Showing 1 changed file with 16 additions and 55 deletions.
71 changes: 16 additions & 55 deletions internal/terraform/transform_attach_config_resource.go
Expand Up @@ -6,6 +6,7 @@ package terraform
import (
"log"

"github.com/hashicorp/terraform/internal/addrs"
"github.com/hashicorp/terraform/internal/configs"
"github.com/hashicorp/terraform/internal/dag"
)
Expand Down Expand Up @@ -48,63 +49,23 @@ func (t *AttachResourceConfigTransformer) Transform(g *Graph) error {
log.Printf("[TRACE] AttachResourceConfigTransformer: %q (%T) has no configuration available", dag.VertexName(v), v)
continue
}

for _, r := range config.Module.ManagedResources {
rAddr := r.Addr()

if rAddr != addr.Resource {
// Not the same resource
continue
}

log.Printf("[TRACE] AttachResourceConfigTransformer: attaching to %q (%T) config from %s", dag.VertexName(v), v, r.DeclRange)
arn.AttachResourceConfig(r)

// attach the provider_meta info
if gnapmc, ok := v.(GraphNodeAttachProviderMetaConfigs); ok {
log.Printf("[TRACE] AttachResourceConfigTransformer: attaching provider meta configs to %s", dag.VertexName(v))
if config == nil {
log.Printf("[TRACE] AttachResourceConfigTransformer: no config set on the transformer for %s", dag.VertexName(v))
continue
}
if config.Module == nil {
log.Printf("[TRACE] AttachResourceConfigTransformer: no module in config for %s", dag.VertexName(v))
continue
}
if config.Module.ProviderMetas == nil {
log.Printf("[TRACE] AttachResourceConfigTransformer: no provider metas defined for %s", dag.VertexName(v))
continue
}
gnapmc.AttachProviderMetaConfigs(config.Module.ProviderMetas)
}
var m map[string]*configs.Resource
if addr.Resource.Mode == addrs.ManagedResourceMode {
m = config.Module.ManagedResources
} else if addr.Resource.Mode == addrs.DataResourceMode {
m = config.Module.DataResources
}
for _, r := range config.Module.DataResources {
rAddr := r.Addr()

if rAddr != addr.Resource {
// Not the same resource
continue
}

log.Printf("[TRACE] AttachResourceConfigTransformer: attaching to %q (%T) config from %#v", dag.VertexName(v), v, r.DeclRange)
arn.AttachResourceConfig(r)

// attach the provider_meta info
if gnapmc, ok := v.(GraphNodeAttachProviderMetaConfigs); ok {
log.Printf("[TRACE] AttachResourceConfigTransformer: attaching provider meta configs to %s", dag.VertexName(v))
if config == nil {
log.Printf("[TRACE] AttachResourceConfigTransformer: no config set on the transformer for %s", dag.VertexName(v))
continue
}
if config.Module == nil {
log.Printf("[TRACE] AttachResourceConfigTransformer: no module in config for %s", dag.VertexName(v))
continue
}
if config.Module.ProviderMetas == nil {
log.Printf("[TRACE] AttachResourceConfigTransformer: no provider metas defined for %s", dag.VertexName(v))
continue
if m != nil {
coord := addr.Resource.String()
if r, ok := m[coord]; ok && r.Addr() == addr.Resource {
log.Printf("[TRACE] AttachResourceConfigTransformer: attaching to %q (%T) config from %#v", dag.VertexName(v), v, r.DeclRange)
arn.AttachResourceConfig(r)
if gnapmc, ok := v.(GraphNodeAttachProviderMetaConfigs); ok {
log.Printf("[TRACE] AttachResourceConfigTransformer: attaching provider meta configs to %s", dag.VertexName(v))
if config.Module != nil && config.Module.ProviderMetas != nil {
gnapmc.AttachProviderMetaConfigs(config.Module.ProviderMetas)
}
}
gnapmc.AttachProviderMetaConfigs(config.Module.ProviderMetas)
}
}
}
Expand Down

0 comments on commit 30396fd

Please sign in to comment.