<commit_before>package cache

import (
	"fmt"
	"unsafe"
)

// No cuncurrent access for now!
type Cache struct {
	hc uint32
	mc uint32

	length uint32
	buffer []byte

	storage map[string][]float64
}

func (c *Cache) String() string {
	return fmt.Sprintf("Cache{hits: %d (%.2f%%), misses: %d (%.2f%%)}",
		c.hc, float64(c.hc)/float64(c.hc+c.mc)*100,
		c.mc, float64(c.mc)/float64(c.hc+c.mc)*100)
}

func New(length uint32, space uint32) *Cache {
	return &Cache{
		length:  length,
		buffer:  make([]byte, 8*length),
		storage: make(map[string][]float64, space),
	}
}

func (c *Cache) Key(sequence []uint64) string {
	for i := uint32(0); i < c.length; i++ {
		*(*uint64)(unsafe.Pointer(&c.buffer[8*i])) = sequence[i]
	}
	return string(c.buffer)
}

func (c *Cache) Get(key string) []float64 {
	value := c.storage[key]

	if value != nil {
		c.hc++
	} else {
		c.mc++
	}

	return value
}

func (c *Cache) Set(key string, value []float64) {
	c.storage[key] = value
}

func (c *Cache) Flush() {
	c.storage = make(map[string][]float64, len(c.storage))
}<commit_msg>Sped up the key generation in pkg/cache<commit_after>package cache

import (
	"fmt"
	"reflect"
	"unsafe"
)

// No cuncurrent access for now!
type Cache struct {
	depth   int
	mapping map[string][]float64

	hc uint32
	mc uint32
}

func (c *Cache) String() string {
	return fmt.Sprintf("Cache{hits: %d (%.2f%%), misses: %d (%.2f%%)}",
		c.hc, float64(c.hc)/float64(c.hc+c.mc)*100,
		c.mc, float64(c.mc)/float64(c.hc+c.mc)*100)
}

func New(depth  uint32, capacity uint32) *Cache {
	return &Cache{
		depth:   int(depth),
		mapping: make(map[string][]float64, capacity),
	}
}

func (c *Cache) Key(trace []uint64) string {
	const (
		sizeOfUInt64 = 8
	)

	sliceHeader := *(*reflect.SliceHeader)(unsafe.Pointer(&trace))

	stringHeader := reflect.StringHeader{
		Data: sliceHeader.Data,
		Len:  sizeOfUInt64 * c.depth,
	}

	return *(*string)(unsafe.Pointer(&stringHeader))
}

func (c *Cache) Get(key string) []float64 {
	value := c.mapping[key]

	if value != nil {
		c.hc++
	} else {
		c.mc++
	}

	return value
}

func (c *Cache) Set(key string, value []float64) {
	c.mapping[key] = value
}

func (c *Cache) Flush() {
	c.mapping = make(map[string][]float64, len(c.mapping))
}
{'fraction_non_alphanumeric': 0.18488529014844804, 'fraction_numerical': 0.038686459739091315, 'mean_word_length': 4.066059225512529, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6154638', 'n_tokens_mistral': 976, 'n_tokens_neox': 895, 'n_words': 212}