<commit_before>// Copyright 2009 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 v1

// Simple byte buffer for marshaling data.

import (
	"bytes"
	"errors"
	"io"
	"sync"
	"unicode/utf8"
)

type grower interface {
	Grow(n int)
}

type truncater interface {
	Truncate(n int)
	Reset()
}

type bytesReader interface {
	Bytes() []byte
	String() string
}

type runeWriter interface {
	WriteRune(r rune) (n int, err error)
}

type stringWriter interface {
	WriteString(s string) (n int, err error)
}

type lener interface {
	Len() int
}

type rewinder interface {
	Rewind(n int) (err error)
}

// TODO(pquerna): continue to reduce these interfaces

type EncodingBuffer interface {
	io.Writer
	io.WriterTo
	io.ByteWriter
	stringWriter
	truncater
	grower
	rewinder
}

type DecodingBuffer interface {
	io.ReadWriter
	io.ByteWriter
	stringWriter
	runeWriter
	truncater
	grower
	bytesReader
	lener
}

var pools [14]sync.Pool
var pool64 *sync.Pool

func init() {
	var i uint
	// TODO(pquerna): add science here around actual pool sizes.
	for i = 6; i < 20; i++ {
		n := 1 << i
		pools[poolNum(n)].New = func() interface{} { return make([]byte, 0, n) }
	}
	pool64 = &pools[0]
}

func poolNum(i int) int {
	// TODO(pquerna): convert to log2 w/ bsr asm instruction:
	// 	<https://groups.google.com/forum/#!topic/golang-nuts/uAb5J1_y7ns>
	if i <= 64 {
		return 0
	} else if i <= 128 {
		return 1
	} else if i <= 256 {
		return 2
	} else if i <= 512 {
		return 3
	} else if i <= 1024 {
		return 4
	} else if i <= 2048 {
		return 5
	} else if i <= 4096 {
		return 6
	} else if i <= 8192 {
		return 7
	} else if i <= 16384 {
		return 8
	} else if i <= 32768 {
		return 9
	} else if i <= 65536 {
		return 10
	} else if i <= 131072 {
		return 11
	} else if i <= 262144 {
		return 12
	} else if i <= 524288 {
		return 13
	} else {
		return -1
	}
}

// Send a buffer to the Pool to reuse for other instances.
// You may no longer utilize the content of the buffer, since it may be used
// by other goroutines.
func Pool(b []byte) {
	c := cap(b)
	pn := poolNum(c)
	if pn != -1 {
		pools[pn].Put(b[0:0])
	}
	// if we didn't have a slot for this []byte, we just drop it and let the GC<commit_msg>Don't pool buffer smaller than 64 bytes.<commit_after>// Copyright 2009 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 v1

// Simple byte buffer for marshaling data.

import (
	"bytes"
	"errors"
	"io"
	"sync"
	"unicode/utf8"
)

type grower interface {
	Grow(n int)
}

type truncater interface {
	Truncate(n int)
	Reset()
}

type bytesReader interface {
	Bytes() []byte
	String() string
}

type runeWriter interface {
	WriteRune(r rune) (n int, err error)
}

type stringWriter interface {
	WriteString(s string) (n int, err error)
}

type lener interface {
	Len() int
}

type rewinder interface {
	Rewind(n int) (err error)
}

// TODO(pquerna): continue to reduce these interfaces

type EncodingBuffer interface {
	io.Writer
	io.WriterTo
	io.ByteWriter
	stringWriter
	truncater
	grower
	rewinder
}

type DecodingBuffer interface {
	io.ReadWriter
	io.ByteWriter
	stringWriter
	runeWriter
	truncater
	grower
	bytesReader
	lener
}

var pools [14]sync.Pool
var pool64 *sync.Pool

func init() {
	var i uint
	// TODO(pquerna): add science here around actual pool sizes.
	for i = 6; i < 20; i++ {
		n := 1 << i
		pools[poolNum(n)].New = func() interface{} { return make([]byte, 0, n) }
	}
	pool64 = &pools[0]
}

func poolNum(i int) int {
	// TODO(pquerna): convert to log2 w/ bsr asm instruction:
	// 	<https://groups.google.com/forum/#!topic/golang-nuts/uAb5J1_y7ns>
	if i <= 64 {
		return 0
	} else if i <= 128 {
		return 1
	} else if i <= 256 {
		return 2
	} else if i <= 512 {
		return 3
	} else if i <= 1024 {
		return 4
	} else if i <= 2048 {
		return 5
	} else if i <= 4096 {
		return 6
	} else if i <= 8192 {
		return 7
	} else if i <= 16384 {
		return 8
	} else if i <= 32768 {
		return 9
	} else if i <= 65536 {
		return 10
	} else if i <= 131072 {
		return 11
	} else if i <= 262144 {
		return 12
	} else if i <= 524288 {
		return 13
	} else {
		return -1
	}
}

// Send a buffer to the Pool to reuse for other instances.
// You may no longer utilize the content of the buffer, since it may be used
// by other goroutines.
func Pool(b []byte) {
	if b == nil {
		return
	}
	c := cap(b)
	if c < 64 {
		return
	}
	pn := poolNum(c)
	if pn != -1 {
		pools[pn].Put(b[0:0])
	}
	// if we didn't have a slot for this []byte, we just drop it and let the GC
{'fraction_non_alphanumeric': 0.11486048020765736, 'fraction_numerical': 0.045857668180834955, 'mean_word_length': 2.958904109589041, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '4767773', 'n_tokens_mistral': 1951, 'n_tokens_neox': 1717, 'n_words': 633}