From 29205baccd771ab584b1f0182963aca201352b64 Mon Sep 17 00:00:00 2001 From: Charles Cooper Date: Wed, 14 Feb 2024 10:15:46 -0800 Subject: [PATCH] feat: frontend optimizations (#3781) a couple compilation-time optimizations. bring total time down by 5% (`vyper -f bytecode`), and time in frontend code down by 20% (`vyper -f annotated_ast`). - cache `VyperNode.get_fields()`, it's a hotspot - optimize `get_common_types(), this line is a hotspot; `rejected = [i for i in common_types if i not in common]` --- vyper/ast/nodes.py | 2 ++ vyper/semantics/analysis/utils.py | 14 ++++++++------ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/vyper/ast/nodes.py b/vyper/ast/nodes.py index c4bce814a4..fb5fb73592 100644 --- a/vyper/ast/nodes.py +++ b/vyper/ast/nodes.py @@ -2,6 +2,7 @@ import contextlib import copy import decimal +import functools import operator import sys import warnings @@ -341,6 +342,7 @@ def from_node(cls, node: "VyperNode", **kwargs) -> "VyperNode": return cls(**ast_struct) @classmethod + @functools.lru_cache(maxsize=None) def get_fields(cls) -> set: """ Return a set of field names for this node. diff --git a/vyper/semantics/analysis/utils.py b/vyper/semantics/analysis/utils.py index fa4dfcc1d1..abea600d88 100644 --- a/vyper/semantics/analysis/utils.py +++ b/vyper/semantics/analysis/utils.py @@ -500,12 +500,14 @@ def get_common_types(*nodes: vy_ast.VyperNode, filter_fn: Callable = None) -> Li for item in nodes[1:]: new_types = _ExprAnalyser().get_possible_types_from_node(item) - common = [i for i in common_types if _is_type_in_list(i, new_types)] - - rejected = [i for i in common_types if i not in common] - common += [i for i in new_types if _is_type_in_list(i, rejected)] - - common_types = common + tmp = [] + for c in common_types: + for t in new_types: + if t.compare_type(c) or c.compare_type(t): + tmp.append(c) + break + + common_types = tmp if filter_fn is not None: common_types = [i for i in common_types if filter_fn(i)]