<commit_before>﻿using System;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
using System.Reflection;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AustinHarris.JsonRpc
{
    public static class JsonRpcProcessor
    {
        public static void Process(JsonRpcStateAsync async, object context = null)
        {
            Task.Factory.StartNew((_async) =>
            {
                var tuple = (Tuple<JsonRpcStateAsync, object>)_async;
                ProcessJsonRpcState(tuple.Item1, tuple.Item2);
            }, new Tuple<JsonRpcStateAsync,object>(async,context));
            
        }

        public static void Process(string sessionId, JsonRpcStateAsync async, object context = null)
        {
            var t = Task.Factory.StartNew((_async) =>
            {
                var i = (Tuple<string, JsonRpcStateAsync, object>)_async;
                ProcessJsonRpcState(i.Item1, i.Item2, i.Item3);
            }, new Tuple<string, JsonRpcStateAsync, object>(sessionId, async, context));

        }
        internal static void ProcessJsonRpcState(JsonRpcStateAsync async, object jsonRpcContext = null)
        {
            ProcessJsonRpcState(Handler.DefaultSessionId(), async, jsonRpcContext);
        }
        internal static void ProcessJsonRpcState(string sessionId, JsonRpcStateAsync async, object jsonRpcContext = null)
        {
            async.Result = ProcessInternal(sessionId, async.JsonRpc, jsonRpcContext);
            async.SetCompleted();
        }

        public static Task<string> Process(string jsonRpc, object context = null)
        {
            return Process(Handler.DefaultSessionId(), jsonRpc, context);
        }
        public static Task<string> Process(string sessionId, string jsonRpc, object context = null)
        {
            return Task<string>.Factory.StartNew((_) =>
            {
                var tuple = (Tuple<string, string, object>) _;
                return ProcessInternal(tuple.Item1, tuple.Item2, tuple.Item3);
            }, new Tuple<string, string, object>(sessionId, jsonRpc, context));
        }

        private static string ProcessInternal(string sessionId, string jsonRpc, object jsonRpcContext)
        {
            var handler = Handler.GetSessionHandler(sessionId);

            try
            {
                if (isSingleRpc(jsonRpc))
                {
                    jsonRpc = string.Format("[{0}]", jsonRpc);
                }
<commit_msg>reduce cost of adding [] around a single rpc by 2.2%<commit_after>﻿using System;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
using System.Reflection;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AustinHarris.JsonRpc
{
    public static class JsonRpcProcessor
    {
        public static void Process(JsonRpcStateAsync async, object context = null)
        {
            Task.Factory.StartNew((_async) =>
            {
                var tuple = (Tuple<JsonRpcStateAsync, object>)_async;
                ProcessJsonRpcState(tuple.Item1, tuple.Item2);
            }, new Tuple<JsonRpcStateAsync,object>(async,context));
            
        }

        public static void Process(string sessionId, JsonRpcStateAsync async, object context = null)
        {
            var t = Task.Factory.StartNew((_async) =>
            {
                var i = (Tuple<string, JsonRpcStateAsync, object>)_async;
                ProcessJsonRpcState(i.Item1, i.Item2, i.Item3);
            }, new Tuple<string, JsonRpcStateAsync, object>(sessionId, async, context));

        }
        internal static void ProcessJsonRpcState(JsonRpcStateAsync async, object jsonRpcContext = null)
        {
            ProcessJsonRpcState(Handler.DefaultSessionId(), async, jsonRpcContext);
        }
        internal static void ProcessJsonRpcState(string sessionId, JsonRpcStateAsync async, object jsonRpcContext = null)
        {
            async.Result = ProcessInternal(sessionId, async.JsonRpc, jsonRpcContext);
            async.SetCompleted();
        }

        public static Task<string> Process(string jsonRpc, object context = null)
        {
            return Process(Handler.DefaultSessionId(), jsonRpc, context);
        }
        public static Task<string> Process(string sessionId, string jsonRpc, object context = null)
        {
            return Task<string>.Factory.StartNew((_) =>
            {
                var tuple = (Tuple<string, string, object>) _;
                return ProcessInternal(tuple.Item1, tuple.Item2, tuple.Item3);
            }, new Tuple<string, string, object>(sessionId, jsonRpc, context));
        }

        private static string ProcessInternal(string sessionId, string jsonRpc, object jsonRpcContext)
        {
            var handler = Handler.GetSessionHandler(sessionId);

            try
            {
                if (isSingleRpc(jsonRpc))
                {
                    var sbAddBrackets = new StringBuilder("[", jsonRpc.Length + 2);
                    sbAddBrackets.Append(jsonRpc);
                    sbAddBrackets.Append("]");
                    jsonRpc = sbAddBrackets.ToString();
                }

{'fraction_non_alphanumeric': 0.09163498098859316, 'fraction_numerical': 0.0038022813688212928, 'mean_word_length': 2.2515451174289245, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 27, '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': '6246338', 'n_tokens_mistral': 1409, 'n_tokens_neox': 1370, 'n_words': 318}