<commit_before>using System;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
using Orleans.Runtime;

namespace Orleans
{
    /// <summary>
    /// Utility functions for dealing with Task's.
    /// </summary>
    public static class PublicOrleansTaskExtentions
    {
        /// <summary>
        /// Observes and ignores a potential exception on a given Task.
        /// If a Task fails and throws an exception which is never observed, it will be caught by the .NET finalizer thread.
        /// This function awaits the given task and if the exception is thrown, it observes this exception and simply ignores it.
        /// This will prevent the escalation of this exception to the .NET finalizer thread.
        /// </summary>
        /// <param name="task">The task to be ignored.</param>
        [SuppressMessage("Microsoft.Performance", "CA1804:RemoveUnusedLocals", MessageId = "ignored")]
        public static void Ignore(this Task task)
        {
            if (task.IsCompleted)
            {
                var ignored = task.Exception;
            }
            else
            {
                task.ContinueWith(
                    t => { var ignored = t.Exception; },
                    CancellationToken.None,
                    TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.ExecuteSynchronously,
                    TaskScheduler.Default);
            }
        }

        /// <summary>
        /// Returns a <see cref="Task{Object}"/> for the provided <see cref="Task"/>.
        /// </summary>
        /// <param name="task">
        /// The task.
        /// </param>
        /// <returns>
        /// The response.
        /// </returns>
        public static Task<object> Box(this Task task)
        {
            switch (task.Status)
            {
                case TaskStatus.RanToCompletion:
                    return Task.FromResult(default(object));

                case TaskStatus.Faulted:
                    {
                        return TaskFromFaulted(task);
                    }

                case TaskStatus.Canceled:
                    {
                        return CancelledTask;
                    }

                default:
                    return BoxAwait(task);
            }
        }

        /// <summary>
        /// Returns a <see cref="Task{Object}"/> for the provided <see cref="Task{T}"/>.
        /// </summary>
        /// <typeparam name="T">
        /// The underlying type of <paramref name="task"/>.
        /// </typeparam>
        /// <param name="task">
        /// The task.
        /// </param>
        /// <returns>
        /// The response.
        /// </returns>
        public static Task<object> Box<T>(this Task<T> task)
        {
            switch (task.Status)
            {
                case TaskStatus.RanToCompletion:
                    return Task.FromResult((object)task.GetResult());

                case TaskStatus.Faulted:
                    {
                        return TaskFromFaulted(task);
                    }

                case TaskStatus.Canceled:
                    {
                        return CancelledTask;
                    }

                default:
                    return BoxAwait(task);
            }
        }

        /// <summary>
        /// Returns a <see cref="Task{Object}"/> for the provided <see cref="Task{Object}"/>.
        /// </summary>
        /// <typeparam name="object">
        /// The underlying type of <paramref name="task"/>.
        /// </typeparam>
        /// <param name="task">
        /// The task.
        /// </param>
        /// <returns>
        /// The response.
        /// </returns>
        public static Task<object> Box(this Task<object> task)
        {
            return task;
        }

        private static async Task<object> BoxAwait(Task task)
        {
            await task;
            return default(object);
        }

        private static async Task<object> BoxAwait<T>(Task<T> task)
        {
            return await task;
        }

        private static Task<object> CancelledTask
        {
            get
            {
                var completion = new TaskCompletionSource<object>();
                completion.SetCanceled();
                return completion.Task;
            }
        }

        private static Task<object> TaskFromFaulted(Task task)
        {
            var completion = new TaskCompletionSource<object>();
            completion.SetException(task.Exception);
            return completion.Task;
        }
    }

    internal static class OrleansTaskExtentions
    {
        public static async Task LogException(this Task task, Logger logger, ErrorCode errorCode, string message)
        {
            try
            {
                await task;
            }
            catch (Exception exc)
            {
                var ignored = task.Exception; // Observe exception
                logger.Error((int)errorCode, message, exc);
                throw;
            }
        }

        // Executes an async function such as Exception is never thrown but rather always returned as a broken task.
        public static async Task SafeExecute(Func<Task> action)
        {
            await action();
        }
<commit_msg>Reused pre-created tasks for efficiency.<commit_after>using System;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
using Orleans.Runtime;

namespace Orleans
{
    /// <summary>
    /// Utility functions for dealing with Task's.
    /// </summary>
    public static class PublicOrleansTaskExtentions
    {
        private static readonly Task<object> CanceledTask;
        private static readonly Task<object> CompletedTask = Task.FromResult(default(object));

        static PublicOrleansTaskExtentions()
        {
            var completion = new TaskCompletionSource<object>();
            completion.SetCanceled();
            CanceledTask = completion.Task;
        }

        /// <summary>
        /// Observes and ignores a potential exception on a given Task.
        /// If a Task fails and throws an exception which is never observed, it will be caught by the .NET finalizer thread.
        /// This function awaits the given task and if the exception is thrown, it observes this exception and simply ignores it.
        /// This will prevent the escalation of this exception to the .NET finalizer thread.
        /// </summary>
        /// <param name="task">The task to be ignored.</param>
        [SuppressMessage("Microsoft.Performance", "CA1804:RemoveUnusedLocals", MessageId = "ignored")]
        public static void Ignore(this Task task)
        {
            if (task.IsCompleted)
            {
                var ignored = task.Exception;
            }
            else
            {
                task.ContinueWith(
                    t => { var ignored = t.Exception; },
                    CancellationToken.None,
                    TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.ExecuteSynchronously,
                    TaskScheduler.Default);
            }
        }

        /// <summary>
        /// Returns a <see cref="Task{Object}"/> for the provided <see cref="Task"/>.
        /// </summary>
        /// <param name="task">
        /// The task.
        /// </param>
        /// <returns>
        /// The response.
        /// </returns>
        public static Task<object> Box(this Task task)
        {
            switch (task.Status)
            {
                case TaskStatus.RanToCompletion:
                    return CompletedTask;

                case TaskStatus.Faulted:
                    {
                        return TaskFromFaulted(task);
                    }

                case TaskStatus.Canceled:
                    {
                        return CanceledTask;
                    }

                default:
                    return BoxAwait(task);
            }
        }

        /// <summary>
        /// Returns a <see cref="Task{Object}"/> for the provided <see cref="Task{T}"/>.
        /// </summary>
        /// <typeparam name="T">
        /// The underlying type of <paramref name="task"/>.
        /// </typeparam>
        /// <param name="task">
        /// The task.
        /// </param>
        /// <returns>
        /// The response.
        /// </returns>
        public static Task<object> Box<T>(this Task<T> task)
        {
            switch (task.Status)
            {
                case TaskStatus.RanToCompletion:
                    return Task.FromResult((object)task.GetResult());

                case TaskStatus.Faulted:
                    {
                        return TaskFromFaulted(task);
                    }

                case TaskStatus.Canceled:
                    {
                        return CanceledTask;
                    }

                default:
                    return BoxAwait(task);
            }
        }

        /// <summary>
        /// Returns a <see cref="Task{Object}"/> for the provided <see cref="Task{Object}"/>.
        /// </summary>
        /// <typeparam name="object">
        /// The underlying type of <paramref name="task"/>.
        /// </typeparam>
        /// <param name="task">
        /// The task.
        /// </param>
        /// <returns>
        /// The response.
        /// </returns>
        public static Task<object> Box(this Task<object> task)
        {
            return task;
        }

        private static async Task<object> BoxAwait(Task task)
        {
            await task;
            return default(object);
        }

        private static async Task<object> BoxAwait<T>(Task<T> task)
        {
            return await task;
        }

        private static Task<object> TaskFromFaulted(Task task)
        {
            var completion = new TaskCompletionSource<object>();
            completion.SetException(task.Exception);
            return completion.Task;
        }
    }

    internal static class OrleansTaskExtentions
    {
        public static async Task LogException(this Task task, Logger logger, ErrorCode errorCode, string message)
        {
            try
            {
                await task;
            }
            catch (Exception exc)
            {
                var ignored = task.Exception; // Observe exception
                logger.Error((int)errorCode, message, exc);
                throw;
            }
        }

        // Executes an async function such as Exception is never thrown but rather always returned as a broken task.
        public static async Task SafeExecute(Func<Task> action)
        {
            await action();
        }

{'fraction_non_alphanumeric': 0.10243539691392452, 'fraction_numerical': 0.0007436326454731362, 'mean_word_length': 1.323758099352052, 'pattern_counts': {'":': 0, '<': 106, '<?xml version=': 0, '>': 108, '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': '4873998', 'n_tokens_mistral': 2631, 'n_tokens_neox': 2532, 'n_words': 720}