gtype

    使用场景

    使用得非常频繁,任何需要并发安全的场景下都适用。

    在普通的并发安全场景中,一个基本类型的变量,特别是一个struct含有若干的属性,往往使用使用互斥(读写)锁或者多把(读写)锁来进行安全管理。
    但这样的使用中,变量/struct/属性的操作性能十分低下,且由于互斥锁机制的存在往往使得操作变得相当复杂,必须小心翼翼地维护好变量/属性的并发安全控制(特别是(RW)Mutex)。

    gtype针对于最常用的基本数据类型,提供了对应的并发安全数据类型,便于在并发安全场景下更好地维护变量/属性,开发者无需在struct中再创建和维护繁琐的(RW)Mutex。由于gtype维护的是基本类型的并发安全,因此内部基本都使用了atomic原子操作来维护并发安全性,因此效率往往会比(RW)Mutex互斥锁高出数十倍。

    接口文档

    https://godoc.org/github.com/gogf/gf/container/gtype

    基准测试结果如下:

    1. john@john-B85M:~/Workspace/Go/GOPATH/src/github.com/gogf/gf/container/gtype$ go test -bench=".*" -benchmem
    2. goos: linux
    3. goarch: amd64
    4. pkg: github.com/gogf/gf/container/gtype
    5. BenchmarkInt_Set-4 300000000 5.87 ns/op 0 B/op 0 allocs/op
    6. BenchmarkInt_Val-4 2000000000 0.46 ns/op 0 B/op 0 allocs/op
    7. BenchmarkInt_Add-4 300000000 5.86 ns/op 0 B/op 0 allocs/op
    8. BenchmarkInt32_Set-4 300000000 5.87 ns/op 0 B/op 0 allocs/op
    9. BenchmarkInt32_Val-4 2000000000 0.47 ns/op 0 B/op 0 allocs/op
    10. BenchmarkInt32_Add-4 300000000 5.85 ns/op 0 B/op 0 allocs/op
    11. BenchmarkInt64_Set-4 300000000 5.88 ns/op 0 B/op 0 allocs/op
    12. BenchmarkInt64_Val-4 2000000000 0.46 ns/op 0 B/op 0 allocs/op
    13. BenchmarkInt64_Add-4 300000000 5.88 ns/op 0 B/op 0 allocs/op
    14. BenchmarkUint_Set-4 300000000 5.88 ns/op 0 B/op 0 allocs/op
    15. BenchmarkUint_Val-4 2000000000 0.46 ns/op 0 B/op 0 allocs/op
    16. BenchmarkUint_Add-4 300000000 5.87 ns/op 0 B/op 0 allocs/op
    17. BenchmarkUint32_Set-4 300000000 5.86 ns/op 0 B/op 0 allocs/op
    18. BenchmarkUint32_Val-4 2000000000 0.50 ns/op 0 B/op 0 allocs/op
    19. BenchmarkUint32_Add-4 200000000 5.86 ns/op 0 B/op 0 allocs/op
    20. BenchmarkUint64_Set-4 300000000 5.86 ns/op 0 B/op 0 allocs/op
    21. BenchmarkUint64_Val-4 2000000000 0.47 ns/op 0 B/op 0 allocs/op
    22. BenchmarkUint64_Add-4 300000000 5.85 ns/op 0 B/op 0 allocs/op
    23. BenchmarkBool_Set-4 300000000 5.85 ns/op 0 B/op 0 allocs/op
    24. BenchmarkString_Set-4 20000000 90.1 ns/op 23 B/op 1 allocs/op
    25. BenchmarkString_Val-4 2000000000 1.58 ns/op 0 B/op 0 allocs/op
    26. BenchmarkBytes_Val-4 2000000000 1.58 ns/op 0 B/op 0 allocs/op
    27. BenchmarkInterface_Set-4 50000000 30.7 ns/op 8 B/op 0 allocs/op
    28. BenchmarkInterface_Val-4 2000000000 0.74 ns/op 0 B/op 0 allocs/op
    29. BenchmarkAtomicValue_Store-4 50000000 27.3 ns/op 8 B/op 0 allocs/op
    30. BenchmarkAtomicValue_Load-4 2000000000 0.73 ns/op 0 B/op 0 allocs/op
    31. PASS
    32. ok github.com/gogf/gf/container/gtype 49.454s

    使用示例

    gtype并发安全基本类型的使用非常简单,往往就类似以下几个方法(以gtype.Int类型举例):

    1. func NewInt(value ...int) *Int
    2. func (v *Int) Add(delta int) (new int)
    3. func (v *Int) Cas(old, new int) bool
    4. func (v *Int) Clone() *Int
    5. func (v *Int) Set(value int) (old int)
    6. func (v *Int) String() string
    7. func (v *Int) Val() int
    1. 0
    2. 10
    3. 9

    示例2,JSON序列化/反序列

    gtype模块下的所有容器类型均实现了标准库json数据格式的序列化/反序列化接口。

    1. Marshal

      1. package main
      2. import (
      3. "encoding/json"
      4. "fmt"
      5. "github.com/gogf/gf/container/gtype"
      6. )
      7. type Student struct {
      8. Id *gtype.Int
      9. Name *gtype.String
      10. }
      11. s := Student{
      12. Id: gtype.NewInt(1),
      13. Name: gtype.NewString("john"),
      14. Scores: gtype.NewInterface([]int{100, 99, 98}),
      15. }
      16. b, _ := json.Marshal(s)
      17. fmt.Println(string(b))
      18. }

      执行后,输出结果:

    2. Unmarshal

      1. package main
      2. import (
      3. "encoding/json"
      4. "fmt"
      5. "github.com/gogf/gf/container/gtype"
      6. )
      7. func main() {
      8. b := []byte(`{"Id":1,"Name":"john","Scores":[100,99,98]}`)
      9. type Student struct {
      10. Id *gtype.Int
      11. Name *gtype.String
      12. Scores *gtype.Interface
      13. }
      14. s := Student{}
      15. json.Unmarshal(b, &s)
      16. fmt.Println(s)
      17. }