duanpao4172 2014-03-05 02:44
浏览 8
已采纳

如何在Go中使用清晰的结构值优化性能?

My API server accept post request and the request body is JSON,so I create two struct object to accept JSON string and persist it into database.But everytime I accept a request I need create the struct object over and over again,I try to clear the struct instead of recreate it.And the demo code like this following:

//The two struct
type Card struct {
  Number string
  Type   string
}

type Person struct {
  Name string
  Cards []Card
}

var p Person

//parse JSON to the struct object
func init() {
  str := `{"name":"aaa","cards":[{"number":"1","type":"visa"},{"number":"2","type":"mastercard"}]}`
  json.Unmarshal([]byte(str), &p)
}

func PersistToDatabase() {
  var err error
  tx, err := db.Begin()
  if err != nil {
    return
  }

  defer func() {
    if err != nil && tx != nil {
      if err := tx.Rollback(); err != nil {
        return
      }
    }
  }

  finish := make(chan bool)

  stmt1, err := tx.Prepare(`insert into tb1(name) values(?)`)
  if err != nil {
    panic(err.Error())
  }
  defer stmt1.Close()

  stmt2, err := tx.Prepare(`insert into tb2(name, num, type) values(?, ?, ?)`)
  if err != nil {
    panic(err.Error())
  }
  defer stmt2.Close()

  go func() {
    defer func() { finish <- true }()
    if _, err = stmt1.Exec(p.name); err != nil {
      log.Println("stmt1.Exec: ", err.Error())
      return
    }

    for _, x := range p.Cards {
      if _, err = stmt2.Exec(p.name, x.Number, x.Type); err != nil {
        log.Println("stmt2.Exec: ", err.Error())
          return
      }
    }
  }
  <-finish
  //clear struct object
  p.Name = ""
  p.Cards = nil //have anything do this better?
}

In the code bottom I clear the Name element but let the Cards element to be nil,how can I do better? And do my databases operation have any problem?

  • 写回答

1条回答 默认 最新

  • dongliang_bj2016 2014-03-05 04:06
    关注

    A struct is not an object[1]. While fundamentally they're both mishmashes of ordered data in some way or another, a struct is effectively just a list of variables. No constructor is called. No memory beyond that of its member elements is allocated, and it's not allocated in any special place. (Granted this probably isn't too expensive even in most OO languages, optimizers are pretty cool sometimes) If I have

    type A struct {
        I int
        S string
    }
    
    var MyA A
    

    It's not really substantially different from

    var (
        I int
        S string
    )
    

    Writing the zero struct of a type MyA = A{} is effectively equivalent to doing MyA.I = 0; MyA.S = "" which is, in turn, effectively equivalent to I = 0; S = "". There should be no (significant) performance overhead doing any of the above.

    This doesn't mean structs aren't useful. They're exceptionally useful, conceptually, for defining methods on, for filling in data such as in cases like JSON. But what they're not is significantly different. Finally, and this is the most important thing, is there a significant performance bottleneck you have profiled in this block of code? If not (and I suspect not), don't worry about it, it's probably fine. :)

    [1] Java or C#-style object, which are theoretically (even if not in practice) more expensive. Not C++ objects which are different (and more like Go structs).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等