github.com/trim21/go-phpserialize@v0.0.22-0.20240301204449-2fca0319b3f0/internal/encoder/compiler_race.go (about)

     1  //go:build race
     2  
     3  package encoder
     4  
     5  import (
     6  	"sync"
     7  )
     8  
     9  var setsMu sync.RWMutex
    10  
    11  func compileToGetCodeSet(typeID uintptr) (encoder, error) {
    12  	if typeID > typeAddr.MaxTypeAddr || typeID < typeAddr.BaseTypeAddr {
    13  		return compileToGetEncoderSlowPath(typeID)
    14  	}
    15  	index := (typeID - typeAddr.BaseTypeAddr) >> typeAddr.AddrShift
    16  	setsMu.RLock()
    17  	if codeSet := cachedEncoder[index]; codeSet != nil {
    18  		encoder, err := compileTypeID(typeID)
    19  		if err != nil {
    20  			setsMu.RUnlock()
    21  			return nil, err
    22  		}
    23  		setsMu.RUnlock()
    24  		return encoder, nil
    25  	}
    26  	setsMu.RUnlock()
    27  
    28  	encoder, err := compileTypeID(typeID)
    29  	if err != nil {
    30  		return nil, err
    31  	}
    32  	setsMu.Lock()
    33  	cachedEncoder[index] = encoder
    34  	setsMu.Unlock()
    35  	return encoder, nil
    36  }