<commit_before>/* Copyright (c) 2013-2016 <NAME>
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "timing.h"

DEFINE_VECTOR(mTimingEventList, struct mTimingEvent*);

void mTimingInit(struct mTiming* timing, int32_t* relativeCycles, int32_t* nextEvent) {
	mTimingEventListInit(&timing->events, 0);
	timing->masterCycles = 0;
	timing->relativeCycles = relativeCycles;
	timing->nextEvent = nextEvent;
}

void mTimingDeinit(struct mTiming* timing) {
	mTimingEventListDeinit(&timing->events);
}

void mTimingClear(struct mTiming* timing) {
	mTimingEventListClear(&timing->events);
	timing->masterCycles = 0;
}

void mTimingSchedule(struct mTiming* timing, struct mTimingEvent* event, int32_t when) {
	int32_t nextEvent = when + *timing->relativeCycles;
	event->when = nextEvent + timing->masterCycles;
	if (nextEvent < *timing->nextEvent) {
		*timing->nextEvent = nextEvent;
	}
	size_t e;
	for (e = 0; e < mTimingEventListSize(&timing->events); ++e) {
		struct mTimingEvent* next = *mTimingEventListGetPointer(&timing->events, e);
		int32_t nextWhen = next->when - timing->masterCycles;
		if (nextWhen > when) {
			mTimingEventListUnshift(&timing->events, e, 1);
			*mTimingEventListGetPointer(&timing->events, e) = event;
			return;
		}
	}
	*mTimingEventListAppend(&timing->events) = event;
}

void mTimingDeschedule(struct mTiming* timing, struct mTimingEvent* event) {
	size_t e;
	for (e = 0; e < mTimingEventListSize(&timing->events); ++e) {
		struct mTimingEvent* next = *mTimingEventListGetPointer(&timing->events, e);
		if (next == event) {
			mTimingEventListShift(&timing->events, e, 1);
			return;
		}
	}
}

void mTimingTick(struct mTiming* timing, int32_t cycles) {
	timing->masterCycles += cycles;
	while (mTimingEventListSize(&timing->events)) {
		struct mTimingEvent* next = *mTimingEventListGetPointer(&timing->events, 0);
		int32_t nextWhen = next->when - timing->masterCycles;
		if (nextWhen > 0) {
			*timing->nextEvent = nextWhen;
			return;
		}
		mTimingEventListShift(&timing->events, 0, 1);
		next->callback(timing, next->context, -nextWhen);
	}
}

int32_t mTimingNextEvent(struct mTiming* timing) {
	if (!mTimingEventListSize(&timing->events)) {
		return INT_MAX;
	}
	struct mTimingEvent* next = *mTimingEventListGetPointer(&timing->events, 0);
	return next->when - timing->masterCycles;
}<commit_msg>Core: Remove unnecessary pointer operation in timing<commit_after>/* Copyright (c) 2013-2016 <NAME>
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "timing.h"

DEFINE_VECTOR(mTimingEventList, struct mTimingEvent*);

void mTimingInit(struct mTiming* timing, int32_t* relativeCycles, int32_t* nextEvent) {
	mTimingEventListInit(&timing->events, 0);
	timing->masterCycles = 0;
	timing->relativeCycles = relativeCycles;
	timing->nextEvent = nextEvent;
}

void mTimingDeinit(struct mTiming* timing) {
	mTimingEventListDeinit(&timing->events);
}

void mTimingClear(struct mTiming* timing) {
	mTimingEventListClear(&timing->events);
	timing->masterCycles = 0;
}

void mTimingSchedule(struct mTiming* timing, struct mTimingEvent* event, int32_t when) {
	int32_t nextEvent = when + *timing->relativeCycles;
	event->when = nextEvent + timing->masterCycles;
	if (nextEvent < *timing->nextEvent) {
		*timing->nextEvent = nextEvent;
	}
	size_t e;
	for (e = 0; e < mTimingEventListSize(&timing->events); ++e) {
		struct mTimingEvent* next = *mTimingEventListGetPointer(&timing->events, e);
		int32_t nextWhen = next->when - timing->masterCycles;
		if (nextWhen > when) {
			mTimingEventListUnshift(&timing->events, e, 1);
			*mTimingEventListGetPointer(&timing->events, e) = event;
			return;
		}
	}
	*mTimingEventListAppend(&timing->events) = event;
}

void mTimingDeschedule(struct mTiming* timing, struct mTimingEvent* event) {
	size_t e;
	for (e = 0; e < mTimingEventListSize(&timing->events); ++e) {
		struct mTimingEvent* next = *mTimingEventListGetPointer(&timing->events, e);
		if (next == event) {
			mTimingEventListShift(&timing->events, e, 1);
			return;
		}
	}
}

void mTimingTick(struct mTiming* timing, int32_t cycles) {
	timing->masterCycles += cycles;
	uint32_t masterCycles = timing->masterCycles;
	while (mTimingEventListSize(&timing->events)) {
		struct mTimingEvent* next = *mTimingEventListGetPointer(&timing->events, 0);
		int32_t nextWhen = next->when - masterCycles;
		if (nextWhen > 0) {
			*timing->nextEvent = nextWhen;
			return;
		}
		mTimingEventListShift(&timing->events, 0, 1);
		next->callback(timing, next->context, -nextWhen);
	}
}

int32_t mTimingNextEvent(struct mTiming* timing) {
	if (!mTimingEventListSize(&timing->events)) {
		return INT_MAX;
	}
	struct mTimingEvent* next = *mTimingEventListGetPointer(&timing->events, 0);
	return next->when - timing->masterCycles;
}
{'fraction_non_alphanumeric': 0.13400633412509896, 'fraction_numerical': 0.01623119556611243, 'mean_word_length': 5.192401960784314, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 79, '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': '6228294', 'n_tokens_mistral': 1969, 'n_tokens_neox': 1822, 'n_words': 401}