// 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.

//go:build goexperiment.jsonv2

package json

import (
	
	
)

// stringCache is a cache for strings converted from a []byte.
type stringCache = [256]string // 256*unsafe.Sizeof(string("")) => 4KiB

// makeString returns the string form of b.
// It returns a pre-allocated string from c if present, otherwise
// it allocates a new string, inserts it into the cache, and returns it.
func makeString( *stringCache,  []byte) string {
	const (
		 = 2   // single byte strings are already interned by the runtime
		 = 256 // large enough for UUIDs, IPv6 addresses, SHA-256 checksums, etc.
	)
	if  == nil || len() <  || len() >  {
		return string()
	}

	// Compute a hash from the fixed-width prefix and suffix of the string.
	// This ensures hashing a string is a constant time operation.
	var  uint32
	switch {
	case len() >= 8:
		 := binary.LittleEndian.Uint64([:8])
		 := binary.LittleEndian.Uint64([len()-8:])
		 = hash64(uint32(), uint32(>>32)) ^ hash64(uint32(), uint32(>>32))
	case len() >= 4:
		 := binary.LittleEndian.Uint32([:4])
		 := binary.LittleEndian.Uint32([len()-4:])
		 = hash64(, )
	case len() >= 2:
		 := binary.LittleEndian.Uint16([:2])
		 := binary.LittleEndian.Uint16([len()-2:])
		 = hash64(uint32(), uint32())
	}

	// Check the cache for the string.
	 :=  % uint32(len(*))
	if  := (*)[];  == string() {
		return 
	}
	 := string()
	(*)[] = 
	return 
}

// hash64 returns the hash of two uint32s as a single uint32.
func hash64(,  uint32) uint32 {
	// If avalanche=true, this is identical to XXH32 hash on a 8B string:
	//	var b [8]byte
	//	binary.LittleEndian.PutUint32(b[:4], lo)
	//	binary.LittleEndian.PutUint32(b[4:], hi)
	//	return xxhash.Sum32(b[:])
	const (
		 = 0x9e3779b1
		 = 0x85ebca77
		 = 0xc2b2ae3d
		 = 0x27d4eb2f
		 = 0x165667b1
	)
	 :=  + uint32(8)
	 +=  * 
	 = bits.RotateLeft32(, 17) * 
	 +=  * 
	 = bits.RotateLeft32(, 17) * 
	// Skip final mix (avalanche) step of XXH32 for performance reasons.
	// Empirical testing shows that the improvements in unbiased distribution
	// does not outweigh the extra cost in computational complexity.
	const  = false
	if  {
		 ^=  >> 15
		 *= 
		 ^=  >> 13
		 *= 
		 ^=  >> 16
	}
	return 
}