<commit_before>package expr

import "sync"

var pointSlicePool *sync.Pool

// Pool tells the expr library which pool to use for temporary []schema.Point
// this lets the expr package effectively create and drop point slices as needed
// it is recommended you use the same pool in your application, e.g. to get slices
// when loading the initial data, and to return the buffers back to the pool once
// the output from this package's processing is no longer needed.
func Pool(p *sync.Pool) {
	pointSlicePool = p
}<commit_msg>utility function for the pool to get appropriately sized slice<commit_after>package expr

import (
	"sync"

	"github.com/grafana/metrictank/schema"
)

var pointSlicePool *sync.Pool

// Pool tells the expr library which pool to use for temporary []schema.Point
// this lets the expr package effectively create and drop point slices as needed
// it is recommended you use the same pool in your application, e.g. to get slices
// when loading the initial data, and to return the buffers back to the pool once
// the output from this package's processing is no longer needed.
func Pool(p *sync.Pool) {
	pointSlicePool = p
}

// pointSlicePoolGet returns a pointslice of at least minCap capacity.
// similar code lives also in api.Fix(). at some point we should really clean up our pool code.
func pointSlicePoolGet(minCap int) []schema.Point {
	// 3 attempts to get a sufficiently sized slice from the pool. if it fails, allocate a new one
	for attempt := 1; attempt < 4; attempt++ {
		candidate := pointSlicePool.Get().([]schema.Point)
		if cap(candidate) >= minCap {
			return candidate
		}
		pointSlicePool.Put(candidate)
	}
	return make([]schema.Point, 0, minCap)
}
{'fraction_non_alphanumeric': 0.07756068679692125, 'fraction_numerical': 0.002368265245707519, 'mean_word_length': 3.9705882352941178, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '5469267', 'n_tokens_mistral': 476, 'n_tokens_neox': 455, 'n_words': 230}