Search
Write a publication
Pull to refresh
4
0
Send message

Go SDK
Будучи скромным гофером хочу отметить, что решение тех или иных задач - требует понимание базы и идиом языка. Конечно, есть замечательная документация, есть не менее замечательный хаб, но пока самые-самые сердечки это репозитория go, где в центре внимания легендарный пакет runtime и sync.
Вот несколько примеров, как описан sync.Map в коде, комментарии к которому и реализация методов отвечает на многие вопросы

//...
// Load, LoadAndDelete, LoadOrStore, Swap, CompareAndSwap, and CompareAndDelete
// are read operations; Delete, LoadAndDelete, Store, and Swap are write operations;
// LoadOrStore is a write operation when it returns loaded set to false;
// CompareAndSwap is a write operation when it returns swapped set to true;
// and CompareAndDelete is a write operation when it returns deleted set to true.
type Map struct {
//...
// Range may be O(N) with the number of elements in the map even if f returns
// false after a constant number of calls.
func (m *Map) Range(f func(key, value any) bool) {
	// We need to be able to iterate over all of the keys that were already
	// present at the start of the call to Range.
	// If read.amended is false, then read.m satisfies that property without
	// requiring us to hold m.mu for a long time.
	read := m.loadReadOnly()
	if read.amended {
		// m.dirty contains keys not in read.m. Fortunately, Range is already O(N)
		// (assuming the caller does not break out early)...
    ...

Всем приятного копания базы и профессионального роста.

Tags:
Total votes 3: ↑3 and ↓0+4
Comments0

В go 1.22 провели изящный рефакторинг части когда по увеличение емкости среза на x1.25 относительно порога значения емкости в 256.
Не большой тест на arm M1 Pro:

const threshold = 256 
//v 1.21
func BenchmarkName121(b *testing.B) {  
  var newcap = threshold  
  var newLen = 100_000_000_000  
  for 0 < newcap && newcap < newLen {   
    newcap += (newcap + 3*threshold) / 4 
  }  
  if newcap <= 0 {   
    newcap = newLen  
  }
}

//v 1.22
func BenchmarkName122(b *testing.B) {
  var newcap = threshold 
  var newLen = 100_000_000_000  
  for {    
    newcap += (newcap + 3*threshold) >> 2    
    if uint(newcap) >= uint(newLen) {   
      break   
    }  
  }
}

В 8 из 10 прогонов результат одинаковый, в оставшихся - 121 показывает лучший результат
В 8 из 10 прогонов результат одинаковый, в оставшихся - 121 показывает лучший результат

Tags:
Total votes 2: ↑2 and ↓0+2
Comments1

Information

Rating
Does not participate
Registered
Activity