summaryrefslogtreecommitdiff
path: root/modules/gdscript/gd_parser.cpp
diff options
context:
space:
mode:
authorest31 <MTest31@outlook.com>2016-02-24 02:46:36 +0100
committerest31 <MTest31@outlook.com>2016-02-28 22:47:48 +0100
commit5f66692395744712244f19e66eaa89790590a019 (patch)
treed22d36d9bf5dc497acc0265aabab96d70d413825 /modules/gdscript/gd_parser.cpp
parentf81153eb6997c16318a5a7df5e982f013ebad37b (diff)
Use xrange for common "for i in range(...)" use case
Make the parser eliminate a wasteful allocation and initialisation of a possibly large array.
Diffstat (limited to 'modules/gdscript/gd_parser.cpp')
-rw-r--r--modules/gdscript/gd_parser.cpp14
1 files changed, 14 insertions, 0 deletions
diff --git a/modules/gdscript/gd_parser.cpp b/modules/gdscript/gd_parser.cpp
index 4f572b7b6e..4b0164d8a2 100644
--- a/modules/gdscript/gd_parser.cpp
+++ b/modules/gdscript/gd_parser.cpp
@@ -1779,6 +1779,20 @@ void GDParser::_parse_block(BlockNode *p_block,bool p_static) {
return;
}
+ // Little optimisation for common usage "for i in range(...):":
+ // don't create and initialize a possibly huge array as range()
+ // would do, but instead create an iterator using xrange()
+ if (container->type == Node::TYPE_OPERATOR) {
+ OperatorNode *op = static_cast<OperatorNode *>(container);
+ if (op->arguments.size() > 0 &&
+ op->arguments[0]->type == Node::TYPE_BUILT_IN_FUNCTION) {
+ BuiltInFunctionNode *c = static_cast<BuiltInFunctionNode *>(op->arguments[0]);
+ if (c->function == GDFunctions::GEN_RANGE) {
+ c->function = GDFunctions::GEN_XRANGE;
+ }
+ }
+ }
+
ControlFlowNode *cf_for = alloc_node<ControlFlowNode>();
cf_for->cf_type=ControlFlowNode::CF_FOR;