1 |
commit: 4e2bf0c6eb6f291a5d151d82c486c7fb73d7f33f |
2 |
Author: Mu Qiao <qiaomuf <AT> gentoo <DOT> org> |
3 |
AuthorDate: Thu May 5 07:18:27 2011 +0000 |
4 |
Commit: Petteri Räty <betelgeuse <AT> gentoo <DOT> org> |
5 |
CommitDate: Sun May 8 12:55:52 2011 +0000 |
6 |
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=4e2bf0c6 |
7 |
|
8 |
Builtin: cache AST inside source builtin |
9 |
|
10 |
AST can be reused in order to improve the performance. Now we use |
11 |
a static variable inside the source builtin to cache all the ASTs |
12 |
created by the builtin. In future we may define rules to change the |
13 |
internal behavior. |
14 |
|
15 |
--- |
16 |
src/builtins/source_builtin.cpp | 18 +++++++++++++----- |
17 |
1 files changed, 13 insertions(+), 5 deletions(-) |
18 |
|
19 |
diff --git a/src/builtins/source_builtin.cpp b/src/builtins/source_builtin.cpp |
20 |
index d2b4bcb..6ba6b11 100644 |
21 |
--- a/src/builtins/source_builtin.cpp |
22 |
+++ b/src/builtins/source_builtin.cpp |
23 |
@@ -26,6 +26,7 @@ |
24 |
|
25 |
#include <fstream> |
26 |
#include <string> |
27 |
+#include <unordered_map> |
28 |
|
29 |
#include "cppbash_builtin.h" |
30 |
#include "core/interpreter.h" |
31 |
@@ -34,17 +35,24 @@ |
32 |
|
33 |
int source_builtin::exec(const std::vector<std::string>& bash_args) |
34 |
{ |
35 |
+ static std::unordered_map<std::string, std::shared_ptr<bash_ast>> ast_cache; |
36 |
+ |
37 |
if(bash_args.size() == 0) |
38 |
throw interpreter_exception("should provide one argument for source builtin"); |
39 |
|
40 |
// we need fix this to pass extra arguments as positional parameters |
41 |
const std::string& path = bash_args[0]; |
42 |
- std::ifstream input(path); |
43 |
- if(!input) |
44 |
- throw interpreter_exception(path + " can't be read"); |
45 |
|
46 |
- bash_ast ast(input); |
47 |
- ast.interpret_with(_walker); |
48 |
+ auto& stored_ast = ast_cache[path]; |
49 |
+ if(!stored_ast) |
50 |
+ { |
51 |
+ std::ifstream input(path); |
52 |
+ if(!input) |
53 |
+ throw interpreter_exception(path + " can't be read"); |
54 |
+ |
55 |
+ stored_ast.reset(new bash_ast(input)); |
56 |
+ } |
57 |
+ stored_ast->interpret_with(_walker); |
58 |
|
59 |
return _walker.get_status(); |
60 |
} |