From 6f629601be418c8bd9fc7672a2a130526d92a8e0 Mon Sep 17 00:00:00 2001 From: Sergei Trofimov Date: Thu, 20 Jul 2017 09:32:48 +0100 Subject: [PATCH] utils/trace_cmd: lazy evaluation of event fields Parsing if a trace events body text into fields is potentially expensive, so only do it if the fields are being accessed. --- wlauto/utils/trace_cmd.py | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/wlauto/utils/trace_cmd.py b/wlauto/utils/trace_cmd.py index 7a7b36d1..d5b7556c 100644 --- a/wlauto/utils/trace_cmd.py +++ b/wlauto/utils/trace_cmd.py @@ -41,7 +41,22 @@ class TraceCmdEvent(object): """ - __slots__ = ['thread', 'reporting_cpu_id', 'timestamp', 'name', 'text', 'fields'] + __slots__ = ['thread', 'reporting_cpu_id', 'timestamp', 'name', 'text', '_fields', '_parser'] + + @property + def fields(self): + if self._fields is not None: + return self._fields + self._fields = {} + + if self._parser: + try: + self._parser(self, self.text) + except Exception: # pylint: disable=broad-except + # unknown format assume user does not care or know how to + # parse self.text + pass + return self._fields def __init__(self, thread, cpu_id, ts, name, body, parser=None): """ @@ -70,15 +85,8 @@ class TraceCmdEvent(object): self.timestamp = numeric(ts) self.name = name self.text = body - self.fields = {} - - if parser: - try: - parser(self, self.text) - except Exception: # pylint: disable=broad-except - # unknown format assume user does not care or know how to - # parse self.text - pass + self._fields = None + self._parser = parser def __getattr__(self, name): try: @@ -143,7 +151,7 @@ def default_body_parser(event, text): v = int(v) except ValueError: pass - event.fields[k] = v + event._fields[k] = v def regex_body_parser(regex, flags=0): @@ -166,9 +174,9 @@ def regex_body_parser(regex, flags=0): if match: for k, v in match.groupdict().iteritems(): try: - event.fields[k] = int(v) + event._fields[k] = int(v) except ValueError: - event.fields[k] = v + event._fields[k] = v return regex_parser_func