From 02234f3d1efb7b66ec7ca05b7c032e6a7681e65e Mon Sep 17 00:00:00 2001 From: UpstreamData Date: Tue, 12 Dec 2023 09:25:43 -0700 Subject: [PATCH] feature: improve dict merging speed --- pyasic/config/__init__.py | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/pyasic/config/__init__.py b/pyasic/config/__init__.py index 57ed62d4..09ec96b5 100644 --- a/pyasic/config/__init__.py +++ b/pyasic/config/__init__.py @@ -13,8 +13,11 @@ # See the License for the specific language governing permissions and - # limitations under the License. - # ------------------------------------------------------------------------------ +from copy import deepcopy from dataclasses import asdict, dataclass +import toml + from pyasic.config.fans import FanModeConfig from pyasic.config.mining import MiningModeConfig from pyasic.config.pools import PoolConfig @@ -151,20 +154,11 @@ class MinerConfig: def merge(a: dict, b: dict) -> dict: - ret = {} - for k in a: - v = a[k] - if k in b.keys(): - if isinstance(v, dict): - ret[k] = merge(a[k], b[k]) - elif isinstance(v, list): - ret[k] = [*v, *b[k]] - else: - ret[k] = v + result = deepcopy(a) + for b_key, b_val in b.items(): + a_val = result.get(b_key) + if isinstance(a_val, dict) and isinstance(b_val, dict): + result[b_key] = merge(a_val, b_val) else: - ret[k] = v - for k in b: - v = b[k] - if k not in ret.keys(): - ret[k] = v - return ret + result[b_key] = deepcopy(b_val) + return result