Skip to content

Commit

Permalink
Merge pull request #985 from mgreter/bugfix/loop-performance
Browse files Browse the repository at this point in the history
Performance and Memory improvement for loops
  • Loading branch information
mgreter committed Mar 27, 2015
2 parents f0475ee + 934c890 commit 50fbda1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
3 changes: 3 additions & 0 deletions environment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
#include <string>
#include <iostream>

#include "ast_fwd_decl.hpp"
#include "ast_def_macros.hpp"
#include "memory_manager.hpp"

namespace Sass {
using std::string;
Expand All @@ -20,6 +22,7 @@ namespace Sass {
ADD_PROPERTY(Environment*, parent);

public:
Memory_Manager<AST_Node> mem;
Environment() : current_frame_(map<string, T>()), parent_(0) { }

map<string, T>& current_frame() { return current_frame_; }
Expand Down
18 changes: 10 additions & 8 deletions eval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ namespace Sass {
double start = static_cast<Number*>(low)->value();
double end = static_cast<Number*>(high)->value();
Env new_env;
new_env[variable] = new (ctx.mem) Number(low->pstate(), start);
// only create iterator once in this environment
Number* it = new (new_env.mem) Number(low->pstate(), start);
new_env[variable] = it;
new_env.link(env);
env = &new_env;
Block* body = f->block();
Expand All @@ -109,15 +111,15 @@ namespace Sass {
if (f->is_inclusive()) ++end;
for (double i = start;
i < end;
(*env)[variable] = new (ctx.mem) Number(low->pstate(), ++i)) {
it->value(++i)) {
val = body->perform(this);
if (val) break;
}
} else {
if (f->is_inclusive()) --end;
for (double i = start;
i > end;
(*env)[variable] = new (ctx.mem) Number(low->pstate(), --i)) {
it->value(--i)) {
val = body->perform(this);
if (val) break;
}
Expand All @@ -132,17 +134,17 @@ namespace Sass {
Expression* expr = e->list()->perform(this);
List* list = 0;
Map* map = 0;
Env new_env;
if (expr->concrete_type() == Expression::MAP) {
map = static_cast<Map*>(expr);
}
else if (expr->concrete_type() != Expression::LIST) {
list = new (ctx.mem) List(expr->pstate(), 1, List::COMMA);
list = new (new_env.mem) List(expr->pstate(), 1, List::COMMA);
*list << expr;
}
else {
list = static_cast<List*>(expr);
}
Env new_env;
for (size_t i = 0, L = variables.size(); i < L; ++i) new_env[variables[i]] = 0;
new_env.link(env);
env = &new_env;
Expand All @@ -154,7 +156,7 @@ namespace Sass {
Expression* value = map->at(key);

if (variables.size() == 1) {
List* variable = new (ctx.mem) List(map->pstate(), 2, List::SPACE);
List* variable = new (new_env.mem) List(map->pstate(), 2, List::SPACE);
*variable << key;
*variable << value;
(*env)[variables[0]] = variable;
Expand All @@ -171,7 +173,7 @@ namespace Sass {
for (size_t i = 0, L = list->length(); i < L; ++i) {
List* variable = 0;
if ((*list)[i]->concrete_type() != Expression::LIST || variables.size() == 1) {
variable = new (ctx.mem) List((*list)[i]->pstate(), 1, List::COMMA);
variable = new (new_env.mem) List((*list)[i]->pstate(), 1, List::COMMA);
*variable << (*list)[i];
}
else {
Expand All @@ -182,7 +184,7 @@ namespace Sass {
(*env)[variables[j]] = (*variable)[j];
}
else {
(*env)[variables[j]] = new (ctx.mem) Null(expr->pstate());
(*env)[variables[j]] = new (new_env.mem) Null(expr->pstate());
}
val = body->perform(this);
if (val) break;
Expand Down
18 changes: 10 additions & 8 deletions expand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,22 +297,24 @@ namespace Sass {
double start = static_cast<Number*>(low)->value();
double end = static_cast<Number*>(high)->value();
Env new_env;
new_env[variable] = new (ctx.mem) Number(low->pstate(), start);
// only create iterator once in this environment
Number* it = new (new_env.mem) Number(low->pstate(), start);
new_env[variable] = it;
new_env.link(env);
env = &new_env;
Block* body = f->block();
if (start < end) {
if (f->is_inclusive()) ++end;
for (double i = start;
i < end;
(*env)[variable] = new (ctx.mem) Number(low->pstate(), ++i)) {
it->value(++i)) {
append_block(body);
}
} else {
if (f->is_inclusive()) --end;
for (double i = start;
i > end;
(*env)[variable] = new (ctx.mem) Number(low->pstate(), --i)) {
it->value(--i)) {
append_block(body);
}
}
Expand All @@ -326,17 +328,17 @@ namespace Sass {
Expression* expr = e->list()->perform(eval->with(env, backtrace));
List* list = 0;
Map* map = 0;
Env new_env;
if (expr->concrete_type() == Expression::MAP) {
map = static_cast<Map*>(expr);
}
else if (expr->concrete_type() != Expression::LIST) {
list = new (ctx.mem) List(expr->pstate(), 1, List::COMMA);
list = new (new_env.mem) List(expr->pstate(), 1, List::COMMA);
*list << expr;
}
else {
list = static_cast<List*>(expr);
}
Env new_env;
for (size_t i = 0, L = variables.size(); i < L; ++i) new_env[variables[i]] = 0;
new_env.link(env);
env = &new_env;
Expand All @@ -348,7 +350,7 @@ namespace Sass {
Expression* v = map->at(key)->perform(eval->with(env, backtrace));

if (variables.size() == 1) {
List* variable = new (ctx.mem) List(map->pstate(), 2, List::SPACE);
List* variable = new (new_env.mem) List(map->pstate(), 2, List::SPACE);
*variable << k;
*variable << v;
(*env)[variables[0]] = variable;
Expand All @@ -363,7 +365,7 @@ namespace Sass {
for (size_t i = 0, L = list->length(); i < L; ++i) {
List* variable = 0;
if ((*list)[i]->concrete_type() != Expression::LIST || variables.size() == 1) {
variable = new (ctx.mem) List((*list)[i]->pstate(), 1, List::COMMA);
variable = new (new_env.mem) List((*list)[i]->pstate(), 1, List::COMMA);
*variable << (*list)[i];
}
else {
Expand All @@ -374,7 +376,7 @@ namespace Sass {
(*env)[variables[j]] = (*variable)[j]->perform(eval->with(env, backtrace));
}
else {
(*env)[variables[j]] = new (ctx.mem) Null(expr->pstate());
(*env)[variables[j]] = new (new_env.mem) Null(expr->pstate());
}
}
append_block(body);
Expand Down

0 comments on commit 50fbda1

Please sign in to comment.