From: Richard Purdie <richard.purdie@linuxfoundation.org>
To: bitbake-devel <bitbake-devel@lists.openembedded.org>
Subject: [PATCH] event/utils/methodpool: Add a cache of compiled code objects
Date: Sun, 20 Dec 2015 13:22:19 +0000 [thread overview]
Message-ID: <1450617739.8461.93.camel@linuxfoundation.org> (raw)
With the addition of function line number handling, the overhead of
the compile functions is no longer negligible. We tend to compile
the same pieces of code over and over again so wrapping a cache around
this is beneficial and removes the overhead of line numbered functions.
Life cycle of a cache using a global like this is in theory problematic
although in reality unlikely to be an issue. It can be dealt with
if/as/when we deal with the other global caches.
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
diff --git a/bitbake/lib/bb/event.py b/bitbake/lib/bb/event.py
index bd2b0a4..5ffe89e 100644
--- a/bitbake/lib/bb/event.py
+++ b/bitbake/lib/bb/event.py
@@ -31,6 +31,7 @@ except ImportError:
import logging
import atexit
import traceback
+import ast
import bb.utils
import bb.compat
import bb.exceptions
@@ -189,13 +190,15 @@ def register(name, handler, mask=None, filename=None, lineno=None):
if isinstance(handler, basestring):
tmp = "def %s(e):\n%s" % (name, handler)
try:
- import ast
- if filename is None:
- filename = "%s(e)" % name
- code = compile(tmp, filename, "exec", ast.PyCF_ONLY_AST)
- if lineno is not None:
- ast.increment_lineno(code, lineno-1)
- code = compile(code, filename, "exec")
+ code = bb.methodpool.compile_cache(tmp)
+ if not code:
+ if filename is None:
+ filename = "%s(e)" % name
+ code = compile(tmp, filename, "exec", ast.PyCF_ONLY_AST)
+ if lineno is not None:
+ ast.increment_lineno(code, lineno-1)
+ code = compile(code, filename, "exec")
+ bb.methodpool.compile_cache_add(tmp, code)
except SyntaxError:
logger.error("Unable to register event handler '%s':\n%s", name,
''.join(traceback.format_exc(limit=0)))
diff --git a/bitbake/lib/bb/methodpool.py b/bitbake/lib/bb/methodpool.py
index b2ea1a1..492c3a5 100644
--- a/bitbake/lib/bb/methodpool.py
+++ b/bitbake/lib/bb/methodpool.py
@@ -27,3 +27,14 @@ def insert_method(modulename, code, fn, lineno):
comp = better_compile(code, modulename, fn, lineno=lineno)
better_exec(comp, None, code, fn)
+compilecache = {}
+
+def compile_cache(code):
+ h = hash(code)
+ if h in compilecache:
+ return compilecache[h]
+ return None
+
+def compile_cache_add(code, compileobj):
+ h = hash(code)
+ compilecache[h] = compileobj
diff --git a/bitbake/lib/bb/utils.py b/bitbake/lib/bb/utils.py
index e564bb6..fa2853e 100644
--- a/bitbake/lib/bb/utils.py
+++ b/bitbake/lib/bb/utils.py
@@ -298,10 +298,15 @@ def better_compile(text, file, realfile, mode = "exec", lineno = None):
will print the offending lines.
"""
try:
+ cache = bb.methodpool.compile_cache(text)
+ if cache:
+ return cache
code = compile(text, realfile, mode, ast.PyCF_ONLY_AST)
if lineno is not None:
ast.increment_lineno(code, lineno)
- return compile(code, realfile, mode)
+ code = compile(code, realfile, mode)
+ bb.methodpool.compile_cache_add(text, code)
+ return code
except Exception as e:
error = []
# split the text into lines again
reply other threads:[~2015-12-20 13:22 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1450617739.8461.93.camel@linuxfoundation.org \
--to=richard.purdie@linuxfoundation.org \
--cc=bitbake-devel@lists.openembedded.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.