diff options
| author | Ajax <commial@gmail.com> | 2015-03-23 17:31:31 +0100 |
|---|---|---|
| committer | Ajax <commial@gmail.com> | 2015-03-24 19:08:38 +0100 |
| commit | e2c1900a5b38082b6569138a36459ecf5778ad67 (patch) | |
| tree | 9e8c7447fc723b5e4e5b6d753b23202fe5cef5f4 | |
| parent | 40dcf88bfda7c8fc8492f408b08d58292af6bafa (diff) | |
| download | miasm-e2c1900a5b38082b6569138a36459ecf5778ad67.tar.gz miasm-e2c1900a5b38082b6569138a36459ecf5778ad67.zip | |
Translator: add a bounded cache on `from_expr`
Diffstat (limited to '')
| -rw-r--r-- | miasm2/ir/translators/translator.py | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/miasm2/ir/translators/translator.py b/miasm2/ir/translators/translator.py index 90e5890c..e3641843 100644 --- a/miasm2/ir/translators/translator.py +++ b/miasm2/ir/translators/translator.py @@ -1,4 +1,5 @@ import miasm2.expression.expression as m2_expr +from miasm2.core.utils import BoundedDict class Translator(object): @@ -34,6 +35,12 @@ class Translator(object): "Return the list of registered languages" return [translator.__LANG__ for translator in cls.available_translators] + def __init__(self, cache_size=1000): + """Instance a translator + @cache_size: (optional) Expr cache size + """ + self._cache = BoundedDict(cache_size) + def from_ExprInt(self, expr): """Translate an ExprInt @expr: ExprInt to translate @@ -86,6 +93,11 @@ class Translator(object): """Translate an expression according to its type @expr: expression to translate """ + # Use cache + if expr in self._cache: + return self._cache[expr] + + # Handle Expr type handlers = {m2_expr.ExprInt: self.from_ExprInt, m2_expr.ExprId: self.from_ExprId, m2_expr.ExprCompose: self.from_ExprCompose, @@ -97,6 +109,9 @@ class Translator(object): } for target, handler in handlers.iteritems(): if isinstance(expr, target): - return handler(expr) + ## Compute value and update the internal cache + ret = handler(expr) + self._cache[expr] = ret + return ret raise ValueError("Unhandled type for %s" % expr) |