<commit_before>using System;
using System.Threading;
using System.Threading.Tasks;

namespace Trans
{
    /// <summary>
    /// Makes your data thread safe :)
    /// 
    /// This one works with structs, which means C# will be doing the cloning.
    /// </summary>
	public class Shielded<T> : IShielded where T : struct
	{
		private class ValueKeeper
		{
			public long Version;
			public T Value;
			public ValueKeeper Older;
		}
		
		private ValueKeeper _current;
        // once negotiated, kept until commit or rollback
        private long _writerStamp;
		private ThreadLocal<ValueKeeper> _locals = new ThreadLocal<ValueKeeper>(() => new ValueKeeper());

		public Shielded()
		{
			_current = new ValueKeeper();
		}

        public Shielded(T initial)
        {
            _current = new ValueKeeper();
            _current.Value = initial;
        }

		private ValueKeeper CurrentTransactionOldValue()
		{
            var stamp = Shield.CurrentTransactionStartStamp;
            SpinWait.SpinUntil(() =>
            {
                var w = Interlocked.Read(ref _writerStamp);
                return w == 0 || w > stamp;
            });
            Shield.Enlist(this);

			var point = _current;
			while (point != null && point.Version > stamp)
				point = point.Older;
			if (point == null)
				throw new ApplicationException("Critical error in Shielded<T> - lost data.");
            return point;
		}

        private bool IsLocalPrepared()
        {
            return _locals.IsValueCreated && _locals.Value != null &&
                _locals.Value.Version == Shield.CurrentTransactionStartStamp;
        }

		private void PrepareForWriting()
        {
            if (!IsLocalPrepared())
            {
                if (_locals.Value == null)
                    _locals.Value = new ValueKeeper();
                _locals.Value.Value = CurrentTransactionOldValue().Value;
                _locals.Value.Version = Shield.CurrentTransactionStartStamp;
            }
            else if (_current.Version > Shield.CurrentTransactionStartStamp)
                throw new TransException("Write collision.");
		}

        /// <summary>
        /// Since T is a value type, this returns a copy every time it's called!
        /// Works out of transaction also.
        /// </summary>
		public T Read
        {
            get
            {
                if (!Shield.IsInTransaction)
                    return _current.Value;

                if (!IsLocalPrepared())
                    return CurrentTransactionOldValue().Value;
                else if (_current.Version > Shield.CurrentTransactionStartStamp)
                    throw new TransException("Writable read collision.");
                return _locals.Value.Value;
            }
		}

        public delegate void ModificationDelegate(ref T value);

        public void Modify(ModificationDelegate d)
        {
            PrepareForWriting();
            d(ref _locals.Value.Value);
        }

        public void Assign(T value)
        {
            PrepareForWriting();
            _locals.Value.Value = value;
        }

        public static implicit operator T(Shielded<T> obj)
        {
            return obj.Read;
        }

		bool IShielded.HasChanges
		{
			get
			{
				return IsLocalPrepared();
			}
		}
		
		bool IShielded.CanCommit(bool strict, long writeStamp)
		{
			if (!strict && !((IShielded)this).HasChanges)
                return true;
            else if (Interlocked.Read(ref _writerStamp) != 0)
                return false;
            else if (_current.Version < Shield.CurrentTransactionStartStamp)
            {
                if (((IShielded)this).HasChanges)
                    Interlocked.Exchange(ref _writerStamp, writeStamp);
                return true;
            }
            return false;
		}
		
		bool IShielded.Commit(long? writeStamp)
        {
            if (((IShielded)this).HasChanges)
            {
                if (Interlocked.Read(ref _writerStamp) != writeStamp.Value)
                    throw new ApplicationException("Commit received from unexpected transaction.");
                var newCurrent = _locals.Value;
                newCurrent.Older = _current;
                newCurrent.Version = writeStamp.Value;
                _current = newCurrent;
                _locals.Value = null;
                Interlocked.Exchange(ref _writerStamp, 0);
                return true;
            }
            return false;
		}

        void IShielded.Rollback(long? writeStamp)
        {
            _locals.Value = null;
            if (writeStamp.HasValue)
                Interlocked.CompareExchange(ref _writerStamp, 0, writeStamp.Value);
        }
		
		void IShielded.TrimCopies(long smallestOpenTransactionId)
		{
			// NB the "smallest transaction" and others can freely read while
			// we're doing this.
			var point = _current;
			while (point != null && point.Version > smallestOpenTransactionId)
				point = point.Older;
			// point is the last accessible - his Older is not needed.
			if (point != null) point.Older = null;
			// if point were null above, data was lost, CurrentTransactionValue() might throw for some!
		}
	}
}

<commit_msg>Shielded - Small optimisation.<commit_after>using System;
using System.Threading;
using System.Threading.Tasks;

namespace Trans
{
    /// <summary>
    /// Makes your data thread safe :)
    /// 
    /// This one works with structs, which means C# will be doing the cloning.
    /// </summary>
	public class Shielded<T> : IShielded where T : struct
	{
		private class ValueKeeper
		{
			public long Version;
			public T Value;
			public ValueKeeper Older;
		}
		
		private ValueKeeper _current;
        // once negotiated, kept until commit or rollback
        private long _writerStamp;
		private ThreadLocal<ValueKeeper> _locals = new ThreadLocal<ValueKeeper>(() => new ValueKeeper());

		public Shielded()
		{
			_current = new ValueKeeper();
		}

        public Shielded(T initial)
        {
            _current = new ValueKeeper();
            _current.Value = initial;
        }

        private void CheckLockAndEnlist()
        {
            var stamp = Shield.CurrentTransactionStartStamp;
            SpinWait.SpinUntil(() =>
            {
                var w = Interlocked.Read(ref _writerStamp);
                return w == 0 || w > stamp;
            });
            Shield.Enlist(this);
        }

		private ValueKeeper CurrentTransactionOldValue()
		{
            CheckLockAndEnlist();

			var point = _current;
			while (point != null && point.Version > Shield.CurrentTransactionStartStamp)
				point = point.Older;
			if (point == null)
				throw new ApplicationException("Critical error in Shielded<T> - lost data.");
            return point;
		}

        private bool IsLocalPrepared()
        {
            return _locals.IsValueCreated && _locals.Value != null &&
                _locals.Value.Version == Shield.CurrentTransactionStartStamp;
        }

		private void PrepareForWriting(bool prepareOld)
        {
            if (!IsLocalPrepared())
            {
                if (_locals.Value == null)
                    _locals.Value = new ValueKeeper();

                if (!prepareOld)
                    CheckLockAndEnlist();
                else
                    _locals.Value.Value = CurrentTransactionOldValue().Value;

                _locals.Value.Version = Shield.CurrentTransactionStartStamp;
            }
            else if (_current.Version > Shield.CurrentTransactionStartStamp)
                throw new TransException("Write collision.");
		}

        /// <summary>
        /// Since T is a value type, this returns a copy every time it's called!
        /// Works out of transaction also.
        /// </summary>
		public T Read
        {
            get
            {
                if (!Shield.IsInTransaction)
                    return _current.Value;

                if (!IsLocalPrepared())
                    return CurrentTransactionOldValue().Value;
                else if (_current.Version > Shield.CurrentTransactionStartStamp)
                    throw new TransException("Writable read collision.");
                return _locals.Value.Value;
            }
		}

        public delegate void ModificationDelegate(ref T value);

        public void Modify(ModificationDelegate d)
        {
            PrepareForWriting(true);
            d(ref _locals.Value.Value);
        }

        public void Assign(T value)
        {
            PrepareForWriting(false);
            _locals.Value.Value = value;
        }

        public static implicit operator T(Shielded<T> obj)
        {
            return obj.Read;
        }

		bool IShielded.HasChanges
		{
			get
			{
				return IsLocalPrepared();
			}
		}
		
		bool IShielded.CanCommit(bool strict, long writeStamp)
		{
			if (!strict && !((IShielded)this).HasChanges)
                return true;
            else if (Interlocked.Read(ref _writerStamp) != 0)
                return false;
            else if (_current.Version < Shield.CurrentTransactionStartStamp)
            {
                if (((IShielded)this).HasChanges)
                    Interlocked.Exchange(ref _writerStamp, writeStamp);
                return true;
            }
            return false;
		}
		
		bool IShielded.Commit(long? writeStamp)
        {
            if (((IShielded)this).HasChanges)
            {
                if (Interlocked.Read(ref _writerStamp) != writeStamp.Value)
                    throw new ApplicationException("Commit received from unexpected transaction.");
                var newCurrent = _locals.Value;
                newCurrent.Older = _current;
                newCurrent.Version = writeStamp.Value;
                _current = newCurrent;
                _locals.Value = null;
                Interlocked.Exchange(ref _writerStamp, 0);
                return true;
            }
            return false;
		}

        void IShielded.Rollback(long? writeStamp)
        {
            _locals.Value = null;
            if (writeStamp.HasValue)
                Interlocked.CompareExchange(ref _writerStamp, 0, writeStamp.Value);
        }
		
		void IShielded.TrimCopies(long smallestOpenTransactionId)
		{
			// NB the "smallest transaction" and others can freely read while
			// we're doing this.
			var point = _current;
			while (point != null && point.Version > smallestOpenTransactionId)
				point = point.Older;
			// point is the last accessible - his Older is not needed.
			if (point != null) point.Older = null;
			// if point were null above, data was lost, CurrentTransactionValue() might throw for some!
		}
	}
}


{'fraction_non_alphanumeric': 0.08848393762915649, 'fraction_numerical': 0.0007514559458951719, 'mean_word_length': 1.7676111255523785, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 35, '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': '3552209', 'n_tokens_mistral': 3104, 'n_tokens_neox': 2761, 'n_words': 727}