presence.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright 2024 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package impl
  5. import (
  6. "sync/atomic"
  7. "unsafe"
  8. )
  9. // presenceSize represents the size of a presence set, which should be the largest index of the set+1
  10. type presenceSize uint32
  11. // presence is the internal representation of the bitmap array in a generated protobuf
  12. type presence struct {
  13. // This is a pointer to the beginning of an array of uint32
  14. P unsafe.Pointer
  15. }
  16. func (p presence) toElem(num uint32) (ret *uint32) {
  17. const (
  18. bitsPerByte = 8
  19. siz = unsafe.Sizeof(*ret)
  20. )
  21. // p.P points to an array of uint32, num is the bit in this array that the
  22. // caller wants to check/manipulate. Calculate the index in the array that
  23. // contains this specific bit. E.g.: 76 / 32 = 2 (integer division).
  24. offset := uintptr(num) / (siz * bitsPerByte) * siz
  25. return (*uint32)(unsafe.Pointer(uintptr(p.P) + offset))
  26. }
  27. // Present checks for the presence of a specific field number in a presence set.
  28. func (p presence) Present(num uint32) bool {
  29. if p.P == nil {
  30. return false
  31. }
  32. return Export{}.Present(p.toElem(num), num)
  33. }
  34. // SetPresent adds presence for a specific field number in a presence set.
  35. func (p presence) SetPresent(num uint32, size presenceSize) {
  36. Export{}.SetPresent(p.toElem(num), num, uint32(size))
  37. }
  38. // SetPresentUnatomic adds presence for a specific field number in a presence set without using
  39. // atomic operations. Only to be called during unmarshaling.
  40. func (p presence) SetPresentUnatomic(num uint32, size presenceSize) {
  41. Export{}.SetPresentNonAtomic(p.toElem(num), num, uint32(size))
  42. }
  43. // ClearPresent removes presence for a specific field number in a presence set.
  44. func (p presence) ClearPresent(num uint32) {
  45. Export{}.ClearPresent(p.toElem(num), num)
  46. }
  47. // LoadPresenceCache (together with PresentInCache) allows for a
  48. // cached version of checking for presence without re-reading the word
  49. // for every field. It is optimized for efficiency and assumes no
  50. // simltaneous mutation of the presence set (or at least does not have
  51. // a problem with simultaneous mutation giving inconsistent results).
  52. func (p presence) LoadPresenceCache() (current uint32) {
  53. if p.P == nil {
  54. return 0
  55. }
  56. return atomic.LoadUint32((*uint32)(p.P))
  57. }
  58. // PresentInCache reads presence from a cached word in the presence
  59. // bitmap. It caches up a new word if the bit is outside the
  60. // word. This is for really fast iteration through bitmaps in cases
  61. // where we either know that the bitmap will not be altered, or we
  62. // don't care about inconsistencies caused by simultaneous writes.
  63. func (p presence) PresentInCache(num uint32, cachedElement *uint32, current *uint32) bool {
  64. if num/32 != *cachedElement {
  65. o := uintptr(num/32) * unsafe.Sizeof(uint32(0))
  66. q := (*uint32)(unsafe.Pointer(uintptr(p.P) + o))
  67. *current = atomic.LoadUint32(q)
  68. *cachedElement = num / 32
  69. }
  70. return (*current & (1 << (num % 32))) > 0
  71. }
  72. // AnyPresent checks if any field is marked as present in the bitmap.
  73. func (p presence) AnyPresent(size presenceSize) bool {
  74. n := uintptr((size + 31) / 32)
  75. for j := uintptr(0); j < n; j++ {
  76. o := j * unsafe.Sizeof(uint32(0))
  77. q := (*uint32)(unsafe.Pointer(uintptr(p.P) + o))
  78. b := atomic.LoadUint32(q)
  79. if b > 0 {
  80. return true
  81. }
  82. }
  83. return false
  84. }
  85. // toRaceDetectData finds the preceding RaceDetectHookData in a
  86. // message by using pointer arithmetic. As the type of the presence
  87. // set (bitmap) varies with the number of fields in the protobuf, we
  88. // can not have a struct type containing the array and the
  89. // RaceDetectHookData. instead the RaceDetectHookData is placed
  90. // immediately before the bitmap array, and we find it by walking
  91. // backwards in the struct.
  92. //
  93. // This method is only called from the race-detect version of the code,
  94. // so RaceDetectHookData is never an empty struct.
  95. func (p presence) toRaceDetectData() *RaceDetectHookData {
  96. var template struct {
  97. d RaceDetectHookData
  98. a [1]uint32
  99. }
  100. o := (uintptr(unsafe.Pointer(&template.a)) - uintptr(unsafe.Pointer(&template.d)))
  101. return (*RaceDetectHookData)(unsafe.Pointer(uintptr(p.P) - o))
  102. }
  103. func atomicLoadShadowPresence(p **[]byte) *[]byte {
  104. return (*[]byte)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(p))))
  105. }
  106. func atomicStoreShadowPresence(p **[]byte, v *[]byte) {
  107. atomic.CompareAndSwapPointer((*unsafe.Pointer)(unsafe.Pointer(p)), nil, unsafe.Pointer(v))
  108. }
  109. // findPointerToRaceDetectData finds the preceding RaceDetectHookData
  110. // in a message by using pointer arithmetic. For the methods called
  111. // directy from generated code, we don't have a pointer to the
  112. // beginning of the presence set, but a pointer inside the array. As
  113. // we know the index of the bit we're manipulating (num), we can
  114. // calculate which element of the array ptr is pointing to. With that
  115. // information we find the preceding RaceDetectHookData and can
  116. // manipulate the shadow bitmap.
  117. //
  118. // This method is only called from the race-detect version of the
  119. // code, so RaceDetectHookData is never an empty struct.
  120. func findPointerToRaceDetectData(ptr *uint32, num uint32) *RaceDetectHookData {
  121. var template struct {
  122. d RaceDetectHookData
  123. a [1]uint32
  124. }
  125. o := (uintptr(unsafe.Pointer(&template.a)) - uintptr(unsafe.Pointer(&template.d))) + uintptr(num/32)*unsafe.Sizeof(uint32(0))
  126. return (*RaceDetectHookData)(unsafe.Pointer(uintptr(unsafe.Pointer(ptr)) - o))
  127. }