<commit_before>package engi

import "runtime"

type World struct {
	entities map[string]*Entity
	systems  []Systemer

	defaultBatch *Batch
	hudBatch     *Batch

	isSetup bool
	paused  bool
	serial  bool
}

func (w *World) new() {
	if w.isSetup {
		return
	}
	w.entities = make(map[string]*Entity)
	if !headless {
		w.defaultBatch = NewBatch(Width(), Height(), batchVert, batchFrag)
		w.hudBatch = NewBatch(Width(), Height(), hudVert, hudFrag)
	}

	// Default WorldBounds values
	WorldBounds.Max = Point{Width(), Height()}

	// Initialize cameraSystem
	cam = &cameraSystem{}
	w.AddSystem(cam)

	// Short-circuit bypass if there's only 1 core
	if runtime.NumCPU() == 1 {
		w.serial = true
	} else {
		w.serial = false
	}

	w.isSetup = true
}

func (w *World) AddEntity(entity *Entity) {
	w.entities[entity.ID()] = entity

	for _, system := range w.systems {
		if entity.DoesRequire(system.Type()) {
			system.AddEntity(entity)
		}
	}
}

func (w *World) RemoveEntity(entity *Entity) {
	for _, system := range w.systems {
		if entity.DoesRequire(system.Type()) {
			system.RemoveEntity(entity)
		}
	}
	delete(w.entities, entity.ID())
}

func (w *World) AddSystem(system Systemer) {
	system.New()
	system.SetWorld(w)
	w.systems = append(w.systems, system)
}

func (w *World) Entities() []*Entity {
	entities := make([]*Entity, 0, len(w.entities))
	for _, v := range w.entities {
		entities = append(entities, v)
	}

	return entities
}

func (w *World) Systems() []Systemer {
	return w.systems
}

func (w *World) pre() {
	if !headless {
		Gl.Clear(Gl.COLOR_BUFFER_BIT)
	}
}

func (w *World) post() {}

func (w *World) update(dt float32) {
	w.pre()

	var unp *UnpauseComponent
	systemsList := w.Systems()

	for _, system := range systemsList {
		if headless && system.SkipOnHeadless() {
			continue // so skip it
		}

		system.Pre()

		complChan := make(chan struct{})
		entities := system.Entities()
		count := len(entities)
		if w.serial || count < 20 {
			for _, entity := range entities {
				if w.paused && !entity.Component(&unp) {
					continue // so skip it
				}
				system.Update(entity, dt)
			}
		} else {
			for _, entity := range entities {
				if w.paused && !entity.Component(&unp) {
					count--
					continue // so skip it
				}
				go func(entity *Entity) {
					system.Update(entity, dt)
					complChan <- struct{}{}
				}(entity)
			}
			for count > 0 {
				<-complChan
				count--
			}
		}
		system.Post()
	}

	w.post()
}

func (w *World) batch(prio PriorityLevel) *Batch {
	if prio >= HUDGround {
		return w.hudBatch
	}

	return w.defaultBatch
}
<commit_msg>Shifted allocation to a more idiomatic location<commit_after>package engi

import "runtime"

type World struct {
	entities map[string]*Entity
	systems  []Systemer

	defaultBatch *Batch
	hudBatch     *Batch

	isSetup bool
	paused  bool
	serial  bool
}

func (w *World) new() {
	if w.isSetup {
		return
	}
	w.entities = make(map[string]*Entity)
	if !headless {
		w.defaultBatch = NewBatch(Width(), Height(), batchVert, batchFrag)
		w.hudBatch = NewBatch(Width(), Height(), hudVert, hudFrag)
	}

	// Default WorldBounds values
	WorldBounds.Max = Point{Width(), Height()}

	// Initialize cameraSystem
	cam = &cameraSystem{}
	w.AddSystem(cam)

	// Short-circuit bypass if there's only 1 core
	if runtime.NumCPU() == 1 {
		w.serial = true
	} else {
		w.serial = false
	}
	w.isSetup = true
}

func (w *World) AddEntity(entity *Entity) {
	w.entities[entity.ID()] = entity

	for _, system := range w.systems {
		if entity.DoesRequire(system.Type()) {
			system.AddEntity(entity)
		}
	}
}

func (w *World) RemoveEntity(entity *Entity) {
	for _, system := range w.systems {
		if entity.DoesRequire(system.Type()) {
			system.RemoveEntity(entity)
		}
	}
	delete(w.entities, entity.ID())
}

func (w *World) AddSystem(system Systemer) {
	system.New()
	system.SetWorld(w)
	w.systems = append(w.systems, system)
}

func (w *World) Entities() []*Entity {
	entities := make([]*Entity, 0, len(w.entities))
	for _, v := range w.entities {
		entities = append(entities, v)
	}
	return entities
}

func (w *World) Systems() []Systemer {
	return w.systems
}

func (w *World) pre() {
	if !headless {
		Gl.Clear(Gl.COLOR_BUFFER_BIT)
	}
}

func (w *World) post() {}

func (w *World) update(dt float32) {
	w.pre()

	var unp *UnpauseComponent
	systemsList := w.Systems()

	for _, system := range systemsList {
		if headless && system.SkipOnHeadless() {
			continue // so skip it
		}

		system.Pre()
		
		entities := system.Entities()
		count := len(entities)
		
		// Concurrency performance maximized at 20+ entities
		// Performance tuning should be conducted for entity updates
		if w.serial || count < 20 {
			for _, entity := range entities {
				if w.paused && !entity.Component(&unp) {
					continue // so skip it
				}
				system.Update(entity, dt)
			}
		} else {
			complChan := make(chan struct{})
			for _, entity := range entities {
				if w.paused && !entity.Component(&unp) {
					count--
					continue // so skip it
				}
				go func(entity *Entity) {
					system.Update(entity, dt)
					complChan <- struct{}{}
				}(entity)
			}
			for count > 0 {
				<-complChan
				count--
			}
		}
		system.Post()
	}
	w.post()
}

func (w *World) batch(prio PriorityLevel) *Batch {
	if prio >= HUDGround {
		return w.hudBatch
	}
	return w.defaultBatch
}

{'fraction_non_alphanumeric': 0.14466239155035834, 'fraction_numerical': 0.0033949453036589967, 'mean_word_length': 3.1987331749802057, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, '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': '4053700', 'n_tokens_mistral': 2138, 'n_tokens_neox': 1891, 'n_words': 484}