// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package tls

import (
	
	
	
	
)

// weakCertCache provides a cache of *x509.Certificates, allowing multiple
// connections to reuse parsed certificates, instead of re-parsing the
// certificate for every connection, which is an expensive operation.
type weakCertCache struct{ sync.Map }

func ( *weakCertCache) ( []byte) (*x509.Certificate, error) {
	if ,  := .Load(string());  {
		if  := .(weak.Pointer[x509.Certificate]).Value();  != nil {
			return , nil
		}
	}

	,  := x509.ParseCertificate()
	if  != nil {
		return nil, 
	}

	 := weak.Make()
	if ,  := .LoadOrStore(string(), ); ! {
		runtime.AddCleanup(, func( any) { .CompareAndDelete(string(), ) }, any(string()))
	} else if  := .(weak.Pointer[x509.Certificate]).Value();  != nil {
		return , nil
	} else {
		if .CompareAndSwap(string(), , ) {
			runtime.AddCleanup(, func( any) { .CompareAndDelete(string(), ) }, any(string()))
		}
	}
	return , nil
}

var globalCertCache = new(weakCertCache)