From 20f56fef63c3d25ae597a4162f17013c22288450 Mon Sep 17 00:00:00 2001 From: Yang Chun Ung Date: Wed, 11 Sep 2024 17:16:08 +0900 Subject: [PATCH] Revert parallel loops --- ArenaService/ArenaService/RpcClient.cs | 131 ++++++++++--------------- 1 file changed, 54 insertions(+), 77 deletions(-) diff --git a/ArenaService/ArenaService/RpcClient.cs b/ArenaService/ArenaService/RpcClient.cs index fe148a2..41fc1ff 100644 --- a/ArenaService/ArenaService/RpcClient.cs +++ b/ArenaService/ArenaService/RpcClient.cs @@ -24,8 +24,6 @@ namespace ArenaService; public class RpcClient: IDisposable, IActionEvaluationHubReceiver { - private int _maxDegreeOfParallelism; - public Address Address => _privateKey.Address; public Block PreviousTip; public Block Tip; @@ -42,7 +40,6 @@ public RpcClient(PrivateKey privateKey, IConfiguration configuration) _privateKey = privateKey; _codec = new Codec(); _rpcHost = configuration["Rpc:Host"]!; - _maxDegreeOfParallelism = int.Parse(configuration["Rpc:Degree"]!); } public async Task StartAsync(CancellationToken cancellationToken) @@ -468,15 +465,11 @@ public async Task> GetStates(Block block, Address ac .GroupBy(x => x.Index / chunkSize) .Select(x => x.Select(v => v.Value.ToByteArray()).ToList()) .ToList(); - ParallelOptions options = new() - { - MaxDegreeOfParallelism = _maxDegreeOfParallelism - }; - await Parallel.ForEachAsync(chunks, options, async (chunk, _) => + foreach (var chunk in chunks) { var queryResult = await _service.GetBulkStateByStateRootHash(block.StateRootHash.ToByteArray(), accountAddress.ToByteArray(), chunk); foreach (var kv in queryResult) result[new Address(kv.Key)] = _codec.Decode(kv.Value); - }); + } return result.ToDictionary(kv => kv.Key, kv => kv.Value); } @@ -572,21 +565,18 @@ public async Task GetAvatarState(Block block, Address avatarAddress /// for the given addresses. public async Task> GetItemSlotStates(Block block, IReadOnlyList
avatarAddresses) { - var result = new ConcurrentDictionary(); + var result = new Dictionary(); var slotAddresses = avatarAddresses.Select(a => ItemSlotState.DeriveAddress(a, BattleType.Arena)).ToList(); var values = await GetStates(block, ReservedAddresses.LegacyAccount, slotAddresses); - avatarAddresses - .AsParallel() - .WithDegreeOfParallelism(_maxDegreeOfParallelism) - .ForAll(address => - { - var slotAddress = ItemSlotState.DeriveAddress(address, BattleType.Arena); - var serialized = values[slotAddress]; - var itemSlotState = serialized is List bencoded - ? new ItemSlotState(bencoded) - : new ItemSlotState(BattleType.Arena); - result.TryAdd(address, itemSlotState); - }); + foreach (var address in avatarAddresses) + { + var slotAddress = ItemSlotState.DeriveAddress(address, BattleType.Arena); + var serialized = values[slotAddress]; + var itemSlotState = serialized is List bencoded + ? new ItemSlotState(bencoded) + : new ItemSlotState(BattleType.Arena); + result.TryAdd(address, itemSlotState); + } return result; } @@ -599,22 +589,18 @@ public async Task> GetItemSlotStates(Block b /// for the given addresses. public async Task> GetRuneSlotStates(Block block, IReadOnlyList
avatarAddresses) { - var result = new ConcurrentDictionary(); + var result = new Dictionary(); var slotAddresses = avatarAddresses.Select(a => RuneSlotState.DeriveAddress(a, BattleType.Arena)).ToList(); var values = await GetStates(block, ReservedAddresses.LegacyAccount, slotAddresses); - avatarAddresses - .AsParallel() - .WithDegreeOfParallelism(_maxDegreeOfParallelism) - .ForAll(address => - { - var slotAddress = RuneSlotState.DeriveAddress(address, BattleType.Arena); - var serialized = values[slotAddress]; - var runeSlotState = serialized is List bencoded - ? new RuneSlotState(bencoded) - : new RuneSlotState(BattleType.Arena); - result.TryAdd(address, runeSlotState); - - }); + foreach (var address in avatarAddresses) + { + var slotAddress = RuneSlotState.DeriveAddress(address, BattleType.Arena); + var serialized = values[slotAddress]; + var runeSlotState = serialized is List bencoded + ? new RuneSlotState(bencoded) + : new RuneSlotState(BattleType.Arena); + result.TryAdd(address, runeSlotState); + } return result; } @@ -623,42 +609,36 @@ public async Task> GetAvatarStates(Block block { var avatarResults = await GetStates(block, Addresses.Avatar, avatarAddresses); var inventoryResults = await GetStates(block, Addresses.Inventory, avatarAddresses); - var result = new ConcurrentDictionary(); - avatarResults - .AsParallel() - .WithDegreeOfParallelism(_maxDegreeOfParallelism) - .ForAll(kv => + var result = new Dictionary(); + foreach (var kv in avatarResults) + { + var address = kv.Key; + var avatarResult = kv.Value; + if (avatarResult is List l) { - var address = kv.Key; - var avatarResult = kv.Value; - if (avatarResult is List l) - { - var avatarState = new AvatarState(l); - var inventory = inventoryResults[address] is List l2 - ? new Inventory(l2) - : new Inventory(); - avatarState.inventory = inventory; - result.TryAdd(address, avatarState); - } - }); + var avatarState = new AvatarState(l); + var inventory = inventoryResults[address] is List l2 + ? new Inventory(l2) + : new Inventory(); + avatarState.inventory = inventory; + result.TryAdd(address, avatarState); + } + } return result; } - public async Task> GetRuneStates(Block block, IReadOnlyList
runeAddresses) + public async Task> GetRuneStates(Block block, IReadOnlyList
runeAddresses) { - var result = new ConcurrentBag(); + var result = new List(); var runeResults = await GetStates(block, ReservedAddresses.LegacyAccount, runeAddresses); - runeResults - .AsParallel() - .WithDegreeOfParallelism(_maxDegreeOfParallelism) - .ForAll(pair => + foreach (var pair in runeResults) + { + if (pair.Value is List rawState) { - if (pair.Value is List rawState) - { - var runeState = new RuneState(rawState); - result.Add(runeState); - } - }); + var runeState = new RuneState(rawState); + result.Add(runeState); + } + } return result; } @@ -666,19 +646,16 @@ public async Task> GetAllRuneStates(Block blo IReadOnlyList
avatarAddresses) { var serializedResults = await GetStates(block, Addresses.RuneState, avatarAddresses); - var result = new ConcurrentDictionary(); - avatarAddresses - .AsParallel() - .WithDegreeOfParallelism(_maxDegreeOfParallelism) - .ForAll(address => + var result = new Dictionary(); + foreach (var address in avatarAddresses) + { + var serialized = serializedResults[address]; + if (serialized is List l) { - var serialized = serializedResults[address]; - if (serialized is List l) - { - var allRuneState = new AllRuneState(l); - result.TryAdd(address, allRuneState); - } - }); + var allRuneState = new AllRuneState(l); + result.TryAdd(address, allRuneState); + } + } return result; }