<commit_before>package slice_test

import (
	"math/rand"
	"os"
	"testing"
	"time"
)

////////////////
// Benchmarks //
////////////////

/////////////////
// Size of int //
/////////////////

const (
	// On 32-bit platforms: 0xFFFFFFFF
	// On 64-bit platforms: 0xFFFFFFFFFFFFFFFF
	maxUint = ^uint(0)
	// On 32-bit platforms: 2
	// On 64-bit platforms: 3
	logSizeOfUintInBytes = maxUint>>8&1 + maxUint>>16&1 + maxUint>>32&1
	// On 32-bit platforms: 4
	// On 64-bit platforms: 8
	sizeOfUintInBytes = 1 << logSizeOfUintInBytes
	// On 32-bit platforms: 32
	// On 64-bit platforms: 64
	sizeOfUintInBits = sizeOfUintInBytes << 3
)

///////////////
// Page size //
///////////////

var (
	pageSizeInBytes uint
	intsPerPage     uint
)

func init() {
	pageSizeInBytes = uint(os.Getpagesize()) // Usually 4096
	// On 32-bit platforms: usually 1024
	// On 64-bit platforms: usually 512
	intsPerPage = pageSizeInBytes / sizeOfUintInBytes
}

////////////////////////////////////////////
// Random number generator initialization //
////////////////////////////////////////////

var (
	seed int64
	src  rand.Source
	rng  *rand.Rand
)

func init() {
	seed = time.Now().UnixNano()
	src = rand.NewSource(seed)
	rng = rand.New(src)
}

///////////////
// Data size //
///////////////

const (
	B  = 1
	kB = 1024
	MB = 1024 * 1024
	GB = 1024 * 1024 * 1024
)

const (
	memoryPerSliceInBytes = 256 * MB // Configurable
)

var (
	pagesPerSlice               uint
	intsPerSlice                uint
	indexIncrementForSinglePage uint
	indexIncrementForCrossPage  uint
)

func init() {
	var n int
	pagesPerSlice = memoryPerSliceInBytes / pageSizeInBytes
	intsPerSlice = memoryPerSliceInBytes / sizeOfUintInBytes

	// If intsPerPage is 512, we would like to generate a random odd integer in
	// the range [1, 511] as the increment, so that it always "generates" (in
	// mathematical sense) all indexes in modulo 512. The increment could be
	// generated by first generating a random integer in the range [0, 255]
	// (0 <= r <= 255), then multiply by 2 (0 <= r <= 510), and then add 1
	// (1 <= r <= 511).
	n = int(intsPerPage >> 1)
	indexIncrementForSinglePage = uint(rng.Intn(n))<<1 + 1

	// If intsPerPage is 512, and pagesPerSlice is 16, then the valid range of
	// an index is [0, 512*16). We would like to generate an odd integer in the
	// range [513, 512*16-1] as the increment, so that it always "cross page"
	// and "generate" (in mathematical sense) all indexes in modulo (512*16).
	// Since any odd integer in [513, 512*16-1] is any odd integer in
	// [512, 512*16-2] + 1, and that is any odd integer in<commit_msg>Got rid of constants defined in sub-package, and used the parent package definitions<commit_after>package slice_test

import (
	"math/rand"
	"os"
	"testing"
	"time"

	"github.com/asukakenji/go-benchmarks"
)

////////////////
// Benchmarks //
////////////////

///////////////
// Page size //
///////////////

var (
	pageSizeInBytes uint
	intsPerPage     uint
)

func init() {
	pageSizeInBytes = uint(os.Getpagesize()) // Usually 4096
	// On 32-bit platforms: usually 1024
	// On 64-bit platforms: usually 512
	intsPerPage = pageSizeInBytes / benchmarks.SizeOfUintInBytes
}

////////////////////////////////////////////
// Random number generator initialization //
////////////////////////////////////////////

var (
	seed int64
	src  rand.Source
	rng  *rand.Rand
)

func init() {
	seed = time.Now().UnixNano()
	src = rand.NewSource(seed)
	rng = rand.New(src)
}

///////////////
// Data size //
///////////////

const (
	B  = 1
	kB = 1024
	MB = 1024 * 1024
	GB = 1024 * 1024 * 1024
)

const (
	memoryPerSliceInBytes = 256 * MB // Configurable
)

var (
	pagesPerSlice               uint
	intsPerSlice                uint
	indexIncrementForSinglePage uint
	indexIncrementForCrossPage  uint
)

func init() {
	var n int
	pagesPerSlice = memoryPerSliceInBytes / pageSizeInBytes
	intsPerSlice = memoryPerSliceInBytes / benchmarks.SizeOfUintInBytes

	// If intsPerPage is 512, we would like to generate a random odd integer in
	// the range [1, 511] as the increment, so that it always "generates" (in
	// mathematical sense) all indexes in modulo 512. The increment could be
	// generated by first generating a random integer in the range [0, 255]
	// (0 <= r <= 255), then multiply by 2 (0 <= r <= 510), and then add 1
	// (1 <= r <= 511).
	n = int(intsPerPage >> 1)
	indexIncrementForSinglePage = uint(rng.Intn(n))<<1 + 1

	// If intsPerPage is 512, and pagesPerSlice is 16, then the valid range of
	// an index is [0, 512*16). We would like to generate an odd integer in the
	// range [513, 512*16-1] as the increment, so that it always "cross page"
	// and "generate" (in mathematical sense) all indexes in modulo (512*16).
	// Since any odd integer in [513, 512*16-1] is any odd integer in
	// [512, 512*16-2] + 1, and that is any odd integer in
{'fraction_non_alphanumeric': 0.1821561338289963, 'fraction_numerical': 0.05720776538620405, 'mean_word_length': 2.2612794612794613, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '1522810', 'n_tokens_mistral': 1792, 'n_tokens_neox': 1524, 'n_words': 534}