<commit_before>package main

import (
	"fmt"
	"runtime"
	"time"

	"github.com/smartystreets/go-disruptor"
)

const (
	BufferSize = 1024 * 64
	BufferMask = BufferSize - 1
	Iterations = 1000000 * 100
)

var ringBuffer = [BufferSize]int64{}

func main() {
	runtime.GOMAXPROCS(2)

	written, read := disruptor.NewCursor(), disruptor.NewCursor()
	reader := disruptor.NewReader(read, written, written, SampleConsumer{})

	started := time.Now()
	reader.Start()
	publish(written, read)
	reader.Stop()
	finished := time.Now()
	fmt.Println(Iterations, finished.Sub(started))
}

// func publish(written *disruptor.Cursor, upstream disruptor.Barrier) {
// 	writer := disruptor.NewWriter(written, upstream, BufferSize)
// 	for sequence := disruptor.InitialSequenceValue; sequence <= Iterations; {
// 		sequence = writer.Reserve()
// 		ringBuffer[sequence&BufferMask] = sequence
// 		writer.Commit(sequence)
// 	}
// }

func publish(written *disruptor.Cursor, upstream disruptor.Barrier) {
	previous := disruptor.InitialSequenceValue
	gate := disruptor.InitialSequenceValue

	for previous <= Iterations {
		next := previous + 1

		for next-BufferSize > gate {
			gate = upstream.Read(next)
		}

		ringBuffer[next&BufferMask] = next
		written.Store(next)
		previous = next
	}
}

type SampleConsumer struct{}

func (this SampleConsumer) Consume(lower, upper int64) {
	for lower <= upper {
		message := ringBuffer[lower&BufferMask]
		if message != lower {
			fmt.Println("Race condition", message, lower)
			panic("Race condition")
		}
		lower++
	}
}
<commit_msg>Initializing outside of loop.<commit_after>package main

import (
	"fmt"
	"runtime"
	"time"

	"github.com/smartystreets/go-disruptor"
)

const (
	BufferSize = 1024 * 64
	BufferMask = BufferSize - 1
	Iterations = 1000000 * 100
)

var ringBuffer = [BufferSize]int64{}

func main() {
	runtime.GOMAXPROCS(2)

	written, read := disruptor.NewCursor(), disruptor.NewCursor()
	reader := disruptor.NewReader(read, written, written, SampleConsumer{})

	started := time.Now()
	reader.Start()
	publish(written, read)
	reader.Stop()
	finished := time.Now()
	fmt.Println(Iterations, finished.Sub(started))
}

// func publish(written *disruptor.Cursor, upstream disruptor.Barrier) {
// 	sequence := disruptor.InitialSequenceValue
// 	writer := disruptor.NewWriter(written, upstream, BufferSize)
// 	for sequence <= Iterations {
// 		sequence = writer.Reserve()
// 		ringBuffer[sequence&BufferMask] = sequence
// 		writer.Commit(sequence)
// 	}
// }

func publish(written *disruptor.Cursor, upstream disruptor.Barrier) {
	previous := disruptor.InitialSequenceValue
	gate := disruptor.InitialSequenceValue

	for previous <= Iterations {
		next := previous + 1

		for next-BufferSize > gate {
			gate = upstream.Read(next)
		}

		ringBuffer[next&BufferMask] = next
		written.Store(next)
		previous = next
	}
}

type SampleConsumer struct{}

func (this SampleConsumer) Consume(lower, upper int64) {
	for lower <= upper {
		message := ringBuffer[lower&BufferMask]
		if message != lower {
			fmt.Println("Race condition", message, lower)
			panic("Race condition")
		}
		lower++
	}
}

{'fraction_non_alphanumeric': 0.13288288288288289, 'fraction_numerical': 0.014800514800514801, 'mean_word_length': 4.332761578044597, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, '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': '2044610', 'n_tokens_mistral': 1111, 'n_tokens_neox': 1012, 'n_words': 223}