Skip to content

Commit

Permalink
internal/tags: Used shared memory allocation for shared tag schemas
Browse files Browse the repository at this point in the history
Reference: #31722

Rather than allocating memory for each shared tag schema, reference the same schema data. Each `schema.Schema` struct allocation is ~304 bytes, which adds up when multiplied times a few hundred resources using it. Not a big reduction by any means, but seems like a quick way to reduce some unnecessary memory usage.

Updated the two code locations which were directly mutating the result of shared tag schemas.
  • Loading branch information
bflad committed Jun 27, 2023
1 parent 2402e64 commit ead2e7d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
2 changes: 1 addition & 1 deletion internal/service/ec2/ec2_spot_instance_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func ResourceSpotInstanceRequest() *schema.Resource {
if v.Computed && !v.Optional {
continue
}
if k == names.AttrTags {
if k == names.AttrTags || k == "volume_tags" {
continue
}
v.ForceNew = true
Expand Down
10 changes: 6 additions & 4 deletions internal/service/ec2/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,10 @@ func tagsFromTagDescriptions(tds []*ec2.TagDescription) []*ec2.Tag {
}

func tagsSchemaConflictsWith(conflictsWith []string) *schema.Schema {
v := tftags.TagsSchema()
v.ConflictsWith = conflictsWith

return v
return &schema.Schema{
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
ConflictsWith: conflictsWith,
}
}
28 changes: 17 additions & 11 deletions internal/tags/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,35 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

// TagsSchema returns the schema to use for tags.
func TagsSchema() *schema.Schema {
return &schema.Schema{
var (
tagsSchema *schema.Schema = &schema.Schema{
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
}
}

func TagsSchemaComputed() *schema.Schema {
return &schema.Schema{
tagsSchemaComputed *schema.Schema = &schema.Schema{
Type: schema.TypeMap,
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
}
}

func TagsSchemaForceNew() *schema.Schema {
return &schema.Schema{
tagsSchemaForceNew *schema.Schema = &schema.Schema{
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
}
)

// TagsSchema returns the schema to use for tags.
func TagsSchema() *schema.Schema {
return tagsSchema
}

func TagsSchemaComputed() *schema.Schema {
return tagsSchemaComputed
}

func TagsSchemaForceNew() *schema.Schema {
return tagsSchemaForceNew
}

0 comments on commit ead2e7d

Please sign in to comment.