diff options
| author | Paolo Bonzini <pbonzini@redhat.com> | 2025-01-22 14:18:18 +0100 |
|---|---|---|
| committer | Jussi Pakkanen <jussi.pakkanen@mailbox.org> | 2025-06-17 12:29:56 +0300 |
| commit | f3366a7e543f69844bd3658db65c28295912db79 (patch) | |
| tree | c845b67716730a01ff7b7d3f3f7c30a50f679daa /mesonbuild/interpreterbase | |
| parent | 992a93bcef47972c75464d1878c4c4b0469b1fbf (diff) | |
| download | meson-f3366a7e543f69844bd3658db65c28295912db79.tar.gz | |
interpreter: make trivial_operators per-class
Do not call update() and Enum.__hash__ a gazillion times; trivial
operators are the same for every instance of the class.
Introduce the infrastructure to build the MRO-resolved operators (so
the outcome same as if one called super().__init__) for each subclass
of InterpreterObject.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Diffstat (limited to 'mesonbuild/interpreterbase')
| -rw-r--r-- | mesonbuild/interpreterbase/baseobjects.py | 33 |
1 files changed, 24 insertions, 9 deletions
diff --git a/mesonbuild/interpreterbase/baseobjects.py b/mesonbuild/interpreterbase/baseobjects.py index 32c36f538..928d03861 100644 --- a/mesonbuild/interpreterbase/baseobjects.py +++ b/mesonbuild/interpreterbase/baseobjects.py @@ -35,19 +35,34 @@ TYPE_op_func = T.Callable[[TYPE_op_arg, TYPE_op_arg], TYPE_var] SubProject = T.NewType('SubProject', str) class InterpreterObject: + TRIVIAL_OPERATORS: T.Dict[ + MesonOperator, + T.Tuple[ + T.Union[T.Type, T.Tuple[T.Type, ...]], + TYPE_op_func + ] + ] = {} + + def __init_subclass__(cls: T.Type[InterpreterObject], **kwargs: T.Any) -> None: + super().__init_subclass__(**kwargs) + saved_trivial_operators = cls.TRIVIAL_OPERATORS + cls.TRIVIAL_OPERATORS = {} + + # Compute inherited operators according to the Python resolution order + # Reverse the result of mro() because update() will overwrite operators + # that are set by the superclass with those that are set by the subclass + for superclass in reversed(cls.mro()[1:]): + if issubclass(superclass, InterpreterObject): + cls.TRIVIAL_OPERATORS.update(superclass.TRIVIAL_OPERATORS) + + cls.TRIVIAL_OPERATORS.update(saved_trivial_operators) + def __init__(self, *, subproject: T.Optional['SubProject'] = None) -> None: self.methods: T.Dict[ str, T.Callable[[T.List[TYPE_var], TYPE_kwargs], TYPE_var] ] = {} self.operators: T.Dict[MesonOperator, TYPE_op_func] = {} - self.trivial_operators: T.Dict[ - MesonOperator, - T.Tuple[ - T.Union[T.Type, T.Tuple[T.Type, ...]], - TYPE_op_func - ] - ] = {} # Current node set during a method call. This can be used as location # when printing a warning message during a method call. self.current_node: mparser.BaseNode = None @@ -79,8 +94,8 @@ class InterpreterObject: raise InvalidCode(f'Unknown method "{method_name}" in object {self} of type {type(self).__name__}.') def operator_call(self, operator: MesonOperator, other: TYPE_var) -> TYPE_var: - if operator in self.trivial_operators: - op = self.trivial_operators[operator] + if operator in self.TRIVIAL_OPERATORS: + op = self.TRIVIAL_OPERATORS[operator] if op[0] is None and other is not None: raise MesonBugException(f'The unary operator `{operator.value}` of {self.display_name()} was passed the object {other} of type {type(other).__name__}') if op[0] is not None and not isinstance(other, op[0]): |
