| 84 | } |
| 85 | |
| 86 | func getDecoder(params ZstdDecoderParams, maxDecodedSize int) (*zstd.Decoder, error) { |
| 87 | key := zstdDecoderKey{params, maxDecodedSize} |
| 88 | if ret, ok := zstdDecMap.Load(key); ok { |
| 89 | return ret.(*zstd.Decoder), nil |
| 90 | } |
| 91 | |
| 92 | zstdDecoderInitMu.Lock() |
| 93 | defer zstdDecoderInitMu.Unlock() |
| 94 | |
| 95 | if ret, ok := zstdDecMap.Load(key); ok { |
| 96 | return ret.(*zstd.Decoder), nil |
| 97 | } |
| 98 | |
| 99 | opts := []zstd.DOption{zstd.WithDecoderConcurrency(0)} |
| 100 | if maxDecodedSize > 0 { |
| 101 | opts = append(opts, zstd.WithDecoderMaxMemory(uint64(maxDecodedSize))) |
| 102 | } |
| 103 | dec, err := zstd.NewReader(nil, opts...) |
| 104 | if err != nil { |
| 105 | return nil, err |
| 106 | } |
| 107 | zstdDecMap.Store(key, dec) |
| 108 | return dec, nil |
| 109 | } |
| 110 | |
| 111 | func zstdDecompress(params ZstdDecoderParams, dst, src []byte, maxDecodedSize int) ([]byte, error) { |
| 112 | dec, err := getDecoder(params, maxDecodedSize) |