/// <summary> /// Determines if number is/equals one of the specified comparison candidates. /// </summary> /// <returns><c>true</c> if is one of the specified candidates; otherwise, <c>false</c>.</returns> /// <param name="ns">Numbers to compar.e</param> /// <param name="candidates">Candidate numbers.</param> public static bool NumberIsOneOf(this IEnumerable <int> ns, IEnumerable <int> candidates) { var q = ns.AsQueryExpr().SelectMany(i => candidates, (i, candidate) => new { i, candidate }) .Where(@t => @t.candidate == @t.i) .Select(@t => @t.i).Run(); return(q.Any()); }
public static void innerQueryOriginalOthersTest(IEnumerable<Product> products) { innerQueryOriginalPLINQ(products); TestingEnvironment.BenchmarkQuery(() => products.AsQueryExpr().Where(p => products.Where(p2 => p2.productName == "Ikura").Select(p2 => p2.unitPrice).Contains(p.unitPrice)).Select(p => p.productName).Compile(), ref products, "Original Ikura with LinqOptimizer", "products.AsQueryExpr().Where(p => products.Where(p2 => p2.productName == \"Ikura\").Select(p2 => p2.unitPrice).Contains(p.unitPrice)).Select(p=>p.productName).Compile()" ); TestingEnvironment.BenchmarkQuery(() => products.AsParallelQueryExpr().Where(p => products.Where(p2 => p2.productName == "Ikura").Select(p2 => p2.unitPrice).Contains(p.unitPrice)).Select(p => p.productName).Compile(), ref products, "Original Ikura with Parallel LinqOptimizer", "products.AsParallelQueryExpr().Where(p => products.Where(p2 => p2.productName == \"Ikura\").Select(p2 => p2.unitPrice).Contains(p.unitPrice)).Select(p=>p.productName).Compile()" ); }
public void LINQOptimizer() { var query = numbers.AsQueryExpr() .Select(x => x.ToString()) .Select(x => x + "1") .Select(x => long.Parse(x)) .Select(x => Math.Ceiling(x / Math.PI)) .Select(x => x * 2) .Select(x => x + 1) .Select(x => (long)Math.Floor(Math.Sqrt(x))) .OrderByDescending(x => x) .Select(x => x.ToString()) .Select(x => int.Parse(x)) .Select(x => (x % 5) * Math.PI) .Select(x => Math.Truncate(x)) .Sum(); query.Run(); }
public static void suspendedSingleResultOthersTest(IEnumerable<Product> products) { TestingEnvironment.BenchmarkQuery(() => OptimizerExtensions.AsGroupSuspendedThreadSafe(() => products.AsParallel().Select(p2 => p2.unitPrice).Max()).SelectMany(uMaxThunk => products.AsParallel().Where(p => uMaxThunk.Value == p.unitPrice).Select(p => p.productName)), ref products, "Optimized Max With AsGroupSuspendedSelectMany operator with PLINQ", "OptimizerExtensions.AsGroupSuspendedThreadSafe(() => products.AsParallel().Select(p2 => p2.unitPrice).Max()).SelectMany(uMaxThunk => products.AsParallel().Where(p => uMaxThunk.Value == p.unitPrice).Select(p => p.productName))" ); TestingEnvironment.BenchmarkQuery(() => OptimizerExtensions.AsGroup(() => products.AsQueryExpr().Select(p2 => p2.unitPrice).Run().Max()).AsQueryExpr().SelectMany(uMaxThunk => products.Where(p => uMaxThunk.Value == p.unitPrice).Select(p => p.productName)).Compile(), ref products, "Optimized Max With AsGroupSuspendedSelectMany operator with LinqOptimizer", "OptimizerExtensions.AsGroup(() => products.AsQueryExpr().Select(p2 => p2.unitPrice).Run().Max()).AsQueryExpr().SelectMany(uMaxThunk => products.Where(p => uMaxThunk.Value == p.unitPrice).Select(p => p.productName)).Compile()" ); TestingEnvironment.BenchmarkQuery(() => OptimizerExtensions.AsGroupSuspendedThreadSafe(() => products.AsParallelQueryExpr().Select(p2 => p2.unitPrice).Run().Max()).SelectMany(uMaxThunk => products.AsParallelQueryExpr().Where(p => uMaxThunk.Value == p.unitPrice).Select(p => p.productName).Run()), ref products, "Optimized Max With AsGroupSuspendedSelectMany operator with Parallel LinqOptimizer", "OptimizerExtensions.AsGroupSuspendedThreadSafe(() => products.AsParallelQueryExpr().Select(p2 => p2.unitPrice).Run().Max()).SelectMany(uMaxThunk => products.AsParallelQueryExpr().Where(p => uMaxThunk.Value == p.unitPrice).Select(p => p.productName).Run())" ); }
public static void suspendedSingleExpressionOthersTest(IEnumerable<Product> products) { TestingEnvironment.BenchmarkQuery(() => products.AsParallel().Where(p => OptimizerExtensions.AsGroup(() => Math.Round(p.unitPrice / 1.2, 2)).Select(pup => products.Any(p2 => p2.unitPrice == pup.Value)).First()).Select(p => p.productName), ref products, "Optimized single With AsGroupSuspendedSelectFirst operator with PLINQ", "products.AsParallel().Where(p => OptimizerExtensions.AsGroup(() => Math.Round(p.unitPrice / 1.2, 2)).Select(pup => products.AsParallel().Any(p2 => p2.unitPrice == pup.Value)).First()).Select(p => p.productName)" ); TestingEnvironment.BenchmarkQuery(() => products.AsQueryExpr().Where(p => OptimizerExtensions.AsGroup(() => Math.Round(p.unitPrice / 1.2, 2)).Select(pup => products.Any(p2 => p2.unitPrice == pup.Value)).First()).Select(p => p.productName).Compile(), ref products, "Optimized single With AsGroupSuspendedSelectFirst operator with LinqOptimizer", "products.AsParallelQueryExpr().Where(p => OptimizerExtensions.AsGroup(() => Math.Round(p.unitPrice / 1.2, 2)).Select(pup => products.Any(p2 => p2.unitPrice == pup.Value)).First()).Select(p => p.productName).Compile()" ); TestingEnvironment.BenchmarkQuery(() => products.AsParallelQueryExpr().Where(p => OptimizerExtensions.AsGroup(() => Math.Round(p.unitPrice / 1.2, 2)).Select(pup => products.Any(p2 => p2.unitPrice == pup.Value)).First()).Select(p => p.productName).Compile(), ref products, "Optimized single With AsGroupSuspendedSelectFirst operator with Parallel LinqOptimizer", "products.AsParallelQueryExpr().Where(p => OptimizerExtensions.AsGroup(() => Math.Round(p.unitPrice / 1.2, 2)).Select(pup => products.Any(p2 => p2.unitPrice == pup.Value)).First()).Select(p => p.productName).Compile()" ); }
public static void suspendedInnerQueryOthersTest(IEnumerable<Product> products) { suspendedInnerQueryPLINQ(products); TestingEnvironment.BenchmarkQuery(() => OptimizerExtensions.AsGroup(products.AsQueryExpr().Where(p2 => p2.productName == "Ikura").Select(p2 => p2.unitPrice).Compile()).AsQueryExpr().SelectMany(uThunk => products.Where(p => uThunk.Value.Contains(p.unitPrice)).Select(p => p.productName)).Compile(), ref products, "Optimized Ikura With AsGroupSuspendedSelectMany operator with LinqOptimizer", "OptimizerExtensions.AsGroup(products.AsQueryExpr().Where(p2 => p2.productName == \"Ikura\").Select(p2 => p2.unitPrice).Compile())).AsQueryExpr().SelectMany(uThunk => products.Where(p => uThunk.Value.Contains(p.unitPrice)).Select(p => p.productName)).Compile()" ); TestingEnvironment.BenchmarkQuery(() => OptimizerExtensions.AsGroupSuspendedThreadSafe(products.AsParallelQueryExpr().Where(p2 => p2.productName == "Ikura").Select(p2 => p2.unitPrice).Compile()).SelectMany(uThunk => products.AsParallelQueryExpr().Where(p => uThunk.Value.Contains(p.unitPrice)).Select(p => p.productName).Run()), ref products, "Optimized Ikura With AsGroupSuspendedSelectMany operator with Parallel LinqOptimizer", "OptimizerExtensions.AsGroupSuspendedThreadSafe(products.AsParallelQueryExpr().Where(p2 => p2.productName == \"Ikura\").Select(p2 => p2.unitPrice).Compile()).SelectMany(uThunk => products.AsParallelQueryExpr().Where(p => uThunk.Value.Contains(p.unitPrice)).Select(p => p.productName).Run())" ); }
public static void singleResultOriginalOthersTest(IEnumerable<Product> products) { TestingEnvironment.BenchmarkQuery(() => from Product p in products.AsParallel() where (from Product p2 in products select p2.unitPrice).Max() == p.unitPrice select p.productName, ref products, "Original Max Query Expession with PLINQ", "from Product p in products.AsParallel()\n where (from Product p2 in products select p2.unitPrice).Max() == p.unitPrice\nselect p.productName" ); TestingEnvironment.BenchmarkQuery(() => products.AsQueryExpr().Where(p => products.Select(p2 => p2.unitPrice).Max() == p.unitPrice).Select(p => p.productName).Compile(), ref products, "Original Max Expession with LinqOptimizer", "products.AsQueryExpr().Where(p => products.Select(p2 => p2.unitPrice).Max() == p.unitPrice).Select(p => p.productName).Compile()" ); TestingEnvironment.BenchmarkQuery(() => products.AsParallelQueryExpr().Where(p => products.Select(p2 => p2.unitPrice).Max() == p.unitPrice).Select(p => p.productName).Compile(), ref products, "Original Max Expession with ParallelLinqOptimizer", "products.AsParallelQueryExpr().Where(p => products.Select(p2 => p2.unitPrice).Max() == p.unitPrice).Select(p => p.productName).Compile()" ); }
public static void singleExpressionOriginalOthersTest(IEnumerable<Product> products) { TestingEnvironment.BenchmarkQuery(() => products.AsParallel().Where(p => products.Any(p2 => p2.unitPrice == Math.Round(p.unitPrice / 1.2, 2))).Select(p => p.productName), ref products, "Original single Lambda Expession with PLINQ", "products.AsParallel().Where(p => products.AsParallel().Any(p2 => p2.unitPrice == Math.Round(p.unitPrice / 1.2, 2))).Select(p => p.productName)" ); TestingEnvironment.BenchmarkQuery(() => products.AsQueryExpr().Where(p => products.Any(p2 => p2.unitPrice == Math.Round(p.unitPrice / 1.2, 2))).Select(p => p.productName).Compile(), ref products, "Original single Lambda Expession with LinqOptimizer", "products.AsQueryExpr().Where(p => products.Any(p2 => p2.unitPrice == Math.Round(p.unitPrice / 1.2, 2))).Select(p => p.productName).Compile()" ); TestingEnvironment.BenchmarkQuery(() => products.AsParallelQueryExpr().Where(p => products.Any(p2 => p2.unitPrice == Math.Round(p.unitPrice / 1.2, 2))).Select(p => p.productName).Compile(), ref products, "Original single Lambda Expession with Parallel LinqOptimizer", "products.AsParallelQueryExpr().Where(p => products.Any(p2 => p2.unitPrice == Math.Round(p.unitPrice / 1.2, 2))).Select(p => p.productName).Compile()" ); }
/// <summary> /// Determines which (if any) key is valid from a set of potential keys. /// </summary> /// <remarks> /// Where appropriate, computes confirmations in parallel. /// </remarks> /// <param name="keyConfirmation">Key confirmation configuration.</param> /// <param name="verifiedOutput">Known/verified output of the function if correct key is input.</param> /// <param name="potentialSenderKeys">Set of potential public keys used by the sender.</param> /// <param name="ephemeralKey"></param> /// <param name="potentialRecipientKeys">Keys used by the recipient that the sender may have used.</param> /// <param name="senderKey">Output of the public key associated with the private key used by the sender.</param> /// <param name="recipientKeypair">Output of the keypair that contains the public key used by the sender.</param> /// <exception cref="ArgumentNullException">Some input argument is null.</exception> /// <exception cref="ConfigurationInvalidException"> /// Some aspect of configuration invalid - detailed inside exception message. /// </exception> /// <returns>Valid key, or null if none are validated as being correct.</returns> public static void ConfirmKeyFromCanary(AuthenticationConfiguration keyConfirmation, byte[] verifiedOutput, IEnumerable <ECKey> potentialSenderKeys, ECKey ephemeralKey, IEnumerable <ECKeypair> potentialRecipientKeys, out ECKey senderKey, out ECKeypair recipientKeypair) { if (keyConfirmation == null) { throw new ArgumentNullException("keyConfirmation", "No configuration supplied."); } if (ephemeralKey == null) { throw new ArgumentNullException("ephemeralKey", "No ephemeral key supplied."); } if (potentialSenderKeys == null) { throw new ArgumentNullException("potentialSenderKeys", "No potential sender keys supplied."); } if (ephemeralKey == null) { throw new ArgumentNullException("ephemeralKey", "No ephemeral key supplied."); } if (potentialRecipientKeys == null) { throw new ArgumentNullException("potentialRecipientKeys", "No potential recipient keys supplied."); } // We can determine which, if any, of the provided keys are capable of decrypting the manifest var viableSenderKeys = potentialSenderKeys.AsQueryExpr().Where(key => key.CurveProviderName.Equals(ephemeralKey.CurveProviderName) && key.CurveName.Equals(ephemeralKey.CurveName)).Run().ToArray(); if (viableSenderKeys.Length == 0) { throw new ArgumentException( "No viable sender keys found - curve provider and/or curve name do not match ephemeral key.", "potentialSenderKeys"); } var viableRecipientKeypairs = potentialRecipientKeys.AsQueryExpr().Where(key => key.CurveProviderName.Equals(ephemeralKey.CurveProviderName) && key.CurveName.Equals(ephemeralKey.CurveName)).Run().ToArray(); if (viableRecipientKeypairs.Length == 0) { throw new ArgumentException( "No viable recipient keys found - curve provider and/or curve name do not match ephemeral key.", "potentialRecipientKeys"); } Func <byte[], byte[]> validator = GetValidator(keyConfirmation, TagConstantBytes, keyConfirmation.SerialiseDto(), verifiedOutput.Length); // Temporary variables to store output in (can't access 'out' parameters inside anonymous method body) ECKey oSK = null; ECKeypair oRKP = null; // See which mode (by-sender / by-recipient) is better to run in parallel if (viableRecipientKeypairs.Length > viableSenderKeys.Length) { Parallel.ForEach(viableRecipientKeypairs, (rKeypair, state) => { foreach (ECKey sKey in viableSenderKeys) { byte[] canary = XorCanaryBytes(sKey.ConfirmationCanary, rKeypair.ConfirmationCanary); byte[] validationOut = validator(canary); if (validationOut.SequenceEqual_ConstantTime(verifiedOutput)) { oSK = sKey; oRKP = rKeypair; state.Stop(); } } }); } else { Parallel.ForEach(viableSenderKeys, (sKey, state) => { foreach (var rKeypair in viableRecipientKeypairs) { byte[] canary = XorCanaryBytes(sKey.ConfirmationCanary, rKeypair.ConfirmationCanary); byte[] validationOut = validator(canary); if (validationOut.SequenceEqual_ConstantTime(verifiedOutput)) { oSK = sKey; oRKP = rKeypair; state.Stop(); } } }); } // Assign the outputs to the 'out' parameters senderKey = oSK; recipientKeypair = oRKP; }