From 363acb1e454a350137e91665a4317732b356766b Mon Sep 17 00:00:00 2001 From: Iustin Pop <iustin@google.com> Date: Fri, 17 Jul 2009 14:54:30 +0200 Subject: [PATCH] Optimizie OpCode loading This patch converts the opcode loading to a pre-built map (at import time) instead of iteration over the globals dict at each call. Microbenchmarks show that this should be around three times faster, and burnin still passes. Signed-off-by: Iustin Pop <iustin@google.com> Reviewed-by: Michael Hanselmann <hansmi@google.com> --- lib/opcodes.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/opcodes.py b/lib/opcodes.py index 535db910d..6ba30f614 100644 --- a/lib/opcodes.py +++ b/lib/opcodes.py @@ -142,14 +142,9 @@ class OpCode(BaseOpCode): raise ValueError("Invalid data to LoadOpcode, missing OP_ID") op_id = data["OP_ID"] op_class = None - for item in globals().values(): - if (isinstance(item, type) and - issubclass(item, cls) and - hasattr(item, "OP_ID") and - getattr(item, "OP_ID") == op_id): - op_class = item - break - if op_class is None: + if op_id in OP_MAPPING: + op_class = OP_MAPPING[op_id] + else: raise ValueError("Invalid data to LoadOpCode: OP_ID %s unsupported" % op_id) op = op_class() @@ -578,3 +573,7 @@ class OpTestAllocator(OpCode): "mem_size", "disks", "disk_template", "os", "tags", "nics", "vcpus", "hypervisor", ] + +OP_MAPPING = dict([(v.OP_ID, v) for v in globals().values() + if (isinstance(v, type) and issubclass(v, OpCode) and + hasattr(v, "OP_ID"))]) -- GitLab