Skip to content

Instantly share code, notes, and snippets.

@kodelint
Last active April 16, 2022 16:52

Revisions

  1. kodelint revised this gist Apr 16, 2022. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions golang-struct-memory-allocation-optimized.go
    Original file line number Diff line number Diff line change
    @@ -4,13 +4,13 @@ import "fmt"
    import "unsafe"

    type TerraformResource struct {
    Cloud string // 16 Bytes
    Name string // 16 Bytes
    PluginVersion string // 16 Bytes
    TerraformVersion string // 16 Bytes
    ModuleVersionMajor int32 // 4 Bytes
    HaveDSL bool // 1 Byte
    IsVersionControlled bool // 1 Byte
    Cloud string // 16 bytes
    Name string // 16 bytes
    PluginVersion string // 16 bytes
    TerraformVersion string // 16 bytes
    ModuleVersionMajor int32 // 4 bytes
    HaveDSL bool // 1 byte
    IsVersionControlled bool // 1 byte
    }

    func main() {
  2. kodelint revised this gist Mar 28, 2022. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions golang-struct-memory-allocation-optimized.go
    Original file line number Diff line number Diff line change
    @@ -9,8 +9,8 @@ type TerraformResource struct {
    PluginVersion string // 16 Bytes
    TerraformVersion string // 16 Bytes
    ModuleVersionMajor int32 // 4 Bytes
    HaveDSL bool // 2 Byte
    IsVersionControlled bool // 2 Byte
    HaveDSL bool // 1 Byte
    IsVersionControlled bool // 1 Byte
    }

    func main() {
  3. kodelint revised this gist Mar 28, 2022. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions golang-struct-memory-allocation-optimized.go
    Original file line number Diff line number Diff line change
    @@ -9,8 +9,8 @@ type TerraformResource struct {
    PluginVersion string // 16 Bytes
    TerraformVersion string // 16 Bytes
    ModuleVersionMajor int32 // 4 Bytes
    HaveDSL bool // 1 Byte
    IsVersionControlled bool // 1 Byte
    HaveDSL bool // 2 Byte
    IsVersionControlled bool // 2 Byte
    }

    func main() {
  4. kodelint renamed this gist Mar 28, 2022. 1 changed file with 0 additions and 0 deletions.
  5. kodelint created this gist Mar 28, 2022.
    35 changes: 35 additions & 0 deletions golang-struct-memory-allocation.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    package main

    import "fmt"
    import "unsafe"

    type TerraformResource struct {
    Cloud string // 16 Bytes
    Name string // 16 Bytes
    PluginVersion string // 16 Bytes
    TerraformVersion string // 16 Bytes
    ModuleVersionMajor int32 // 4 Bytes
    HaveDSL bool // 1 Byte
    IsVersionControlled bool // 1 Byte
    }

    func main() {
    var d TerraformResource
    d.Cloud = "aws"
    d.Name = "ec2"
    d.HaveDSL = true
    d.PluginVersion = "3.64"
    d.TerraformVersion = "1.1"
    d.ModuleVersionMajor = 1
    d.IsVersionControlled = true
    fmt.Println("==============================================================")
    fmt.Printf("Total Memory Usage StructType:d %T => [%d]\n", d, unsafe.Sizeof(d))
    fmt.Println("==============================================================")
    fmt.Printf("Cloud Field StructType:d.Cloud %T => [%d]\n", d.Cloud, unsafe.Sizeof(d.Cloud))
    fmt.Printf("Name Field StructType:d.Name %T => [%d]\n", d.Name, unsafe.Sizeof(d.Name))
    fmt.Printf("HaveDSL Field StructType:d.HaveDSL %T => [%d]\n", d.HaveDSL, unsafe.Sizeof(d.HaveDSL))
    fmt.Printf("PluginVersion Field StructType:d.PluginVersion %T => [%d]\n", d.PluginVersion, unsafe.Sizeof(d.PluginVersion))
    fmt.Printf("ModuleVersionMajor Field StructType:d.IsVersionControlled %T => [%d]\n", d.IsVersionControlled, unsafe.Sizeof(d.IsVersionControlled))
    fmt.Printf("TerraformVersion Field StructType:d.TerraformVersion %T => [%d]\n", d.TerraformVersion, unsafe.Sizeof(d.TerraformVersion))
    fmt.Printf("ModuleVersionMajor Field StructType:d.ModuleVersionMajor %T => [%d]\n", d.ModuleVersionMajor, unsafe.Sizeof(d.ModuleVersionMajor))
    }