git.gammaspectra.live/P2Pool/go-json@v0.99.0/internal/encoder/compiler_race.go (about)

     1  //go:build race
     2  // +build race
     3  
     4  package encoder
     5  
     6  import (
     7  	"sync"
     8  )
     9  
    10  var setsMu sync.RWMutex
    11  
    12  func CompileToGetCodeSet(ctx *RuntimeContext, typeptr uintptr) (*OpcodeSet, error) {
    13  	if typeptr > typeAddr.MaxTypeAddr || typeptr < typeAddr.BaseTypeAddr {
    14  		codeSet, err := compileToGetCodeSetSlowPath(typeptr)
    15  		if err != nil {
    16  			return nil, err
    17  		}
    18  		return getFilteredCodeSetIfNeeded(ctx, codeSet)
    19  	}
    20  	index := (typeptr - typeAddr.BaseTypeAddr) >> typeAddr.AddrShift
    21  	setsMu.RLock()
    22  	if codeSet := cachedOpcodeSets[index]; codeSet != nil {
    23  		filtered, err := getFilteredCodeSetIfNeeded(ctx, codeSet)
    24  		if err != nil {
    25  			setsMu.RUnlock()
    26  			return nil, err
    27  		}
    28  		setsMu.RUnlock()
    29  		return filtered, nil
    30  	}
    31  	setsMu.RUnlock()
    32  
    33  	codeSet, err := newCompiler().compile(typeptr)
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  	filtered, err := getFilteredCodeSetIfNeeded(ctx, codeSet)
    38  	if err != nil {
    39  		return nil, err
    40  	}
    41  	setsMu.Lock()
    42  	cachedOpcodeSets[index] = codeSet
    43  	setsMu.Unlock()
    44  	return filtered, nil
    45  }