Make up some of the performance losses #132

This commit is contained in:
Jason Turner 2014-08-30 14:49:31 -06:00
parent a71903f185
commit 4ee9ba9c96
2 changed files with 37 additions and 38 deletions

View File

@ -249,12 +249,13 @@ namespace chaiscript
return (*m_data->m_attrs)[t_name]; return (*m_data->m_attrs)[t_name];
} }
void copy_attrs(const Boxed_Value &t_obj) Boxed_Value &copy_attrs(const Boxed_Value &t_obj)
{ {
if (t_obj.m_data->m_attrs) if (t_obj.m_data->m_attrs)
{ {
m_data->m_attrs = std::unique_ptr<std::map<std::string, Boxed_Value>>(new std::map<std::string, Boxed_Value>(*t_obj.m_data->m_attrs)); m_data->m_attrs = std::unique_ptr<std::map<std::string, Boxed_Value>>(new std::map<std::string, Boxed_Value>(*t_obj.m_data->m_attrs));
} }
return *this;
} }

View File

@ -42,9 +42,7 @@ def new(x) {
def clone(x) : function_exists(type_name(x)) && call_exists(eval(type_name(x)), x) def clone(x) : function_exists(type_name(x)) && call_exists(eval(type_name(x)), x)
{ {
var c := eval(type_name(x))(x); eval(type_name(x))(x).copy_var_attrs(x);
c.copy_var_attrs(x);
return c;
} }
@ -138,8 +136,8 @@ def insert_at(container, pos, x)
# Returns the reverse of the given container # Returns the reverse of the given container
def reverse(container) { def reverse(container) {
auto retval = new(container); auto retval := new(container);
auto r = range(container); auto r := range(container);
while (!r.empty()) { while (!r.empty()) {
retval.push_back(r.back()); retval.push_back(r.back());
r.pop_back(); r.pop_back();
@ -157,7 +155,7 @@ def range(r) : call_exists(range_internal, r)
{ {
var ri := range_internal(r); var ri := range_internal(r);
ri.get_var_attr("internal_obj") := r; ri.get_var_attr("internal_obj") := r;
return ri; ri;
} }
# The retro attribute that contains the underlying range # The retro attribute that contains the underlying range
@ -208,7 +206,7 @@ def retro::empty()
# Performs the second value function over the container first value # Performs the second value function over the container first value
def for_each(container, func) : call_exists(range, container) { def for_each(container, func) : call_exists(range, container) {
var t_range = range(container); var t_range := range(container);
while (!t_range.empty()) { while (!t_range.empty()) {
func(t_range.front()); func(t_range.front());
t_range.pop_front(); t_range.pop_front();
@ -220,7 +218,7 @@ def back_inserter(container) {
} }
def contains(container, item, compare_func) : call_exists(range, container) { def contains(container, item, compare_func) : call_exists(range, container) {
auto t_range = range(container); auto t_range := range(container);
while (!t_range.empty()) { while (!t_range.empty()) {
if ( compare_func(t_range.front(), item) ) { if ( compare_func(t_range.front(), item) ) {
return true; return true;
@ -228,15 +226,15 @@ def contains(container, item, compare_func) : call_exists(range, container) {
t_range.pop_front(); t_range.pop_front();
} }
return false; false;
} }
def contains(container, item) { def contains(container, item) {
return contains(container, item, eq) contains(container, item, eq)
} }
def map(container, func, inserter) : call_exists(range, container) { def map(container, func, inserter) : call_exists(range, container) {
auto range = range(container); auto range := range(container);
while (!range.empty()) { while (!range.empty()) {
inserter(func(range.front())); inserter(func(range.front()));
range.pop_front(); range.pop_front();
@ -245,7 +243,7 @@ def map(container, func, inserter) : call_exists(range, container) {
# Performs the second value function over the container first value. Creates a new container with the results # Performs the second value function over the container first value. Creates a new container with the results
def map(container, func) { def map(container, func) {
auto retval = new(container); auto retval := new(container);
map(container, func, back_inserter(retval)); map(container, func, back_inserter(retval));
retval; retval;
} }
@ -253,7 +251,7 @@ def map(container, func) {
# Performs the second value function over the container first value. Starts with initial and continues with each element. # Performs the second value function over the container first value. Starts with initial and continues with each element.
def foldl(container, func, initial) : call_exists(range, container){ def foldl(container, func, initial) : call_exists(range, container){
auto retval = initial; auto retval = initial;
auto range = range(container); auto range := range(container);
while (!range.empty()) { while (!range.empty()) {
retval = (func(range.front(), retval)); retval = (func(range.front(), retval));
range.pop_front(); range.pop_front();
@ -274,8 +272,8 @@ def product(container) {
# Returns a new container with the elements of the first value concatenated with the elements of the second value # Returns a new container with the elements of the first value concatenated with the elements of the second value
def concat(x, y) : call_exists(clone, x) { def concat(x, y) : call_exists(clone, x) {
auto retval = x; auto retval = x;
auto inserter = back_inserter(retval); auto inserter := back_inserter(retval);
auto range = range(y); auto range := range(y);
while (!range.empty()) { while (!range.empty()) {
inserter(range.front()); inserter(range.front());
range.pop_front(); range.pop_front();
@ -285,7 +283,7 @@ def concat(x, y) : call_exists(clone, x) {
def take(container, num, inserter) : call_exists(range, container) { def take(container, num, inserter) : call_exists(range, container) {
auto r = range(container); auto r := range(container);
auto i = num; auto i = num;
while ((i > 0) && (!r.empty())) { while ((i > 0) && (!r.empty())) {
inserter(r.front()); inserter(r.front());
@ -297,14 +295,14 @@ def take(container, num, inserter) : call_exists(range, container) {
# Returns a new container with the given number of elements taken from the container # Returns a new container with the given number of elements taken from the container
def take(container, num) { def take(container, num) {
auto retval = new(container); auto retval := new(container);
take(container, num, back_inserter(retval)); take(container, num, back_inserter(retval));
retval; retval;
} }
def take_while(container, f, inserter) : call_exists(range, container) { def take_while(container, f, inserter) : call_exists(range, container) {
auto r = range(container); auto r := range(container);
while ((!r.empty()) && f(r.front())) { while ((!r.empty()) && f(r.front())) {
inserter(r.front()); inserter(r.front());
r.pop_front(); r.pop_front();
@ -314,14 +312,14 @@ def take_while(container, f, inserter) : call_exists(range, container) {
# Returns a new container with the given elements match the second value function # Returns a new container with the given elements match the second value function
def take_while(container, f) { def take_while(container, f) {
auto retval = new(container); auto retval := new(container);
take_while(container, f, back_inserter(retval)); take_while(container, f, back_inserter(retval));
retval; retval;
} }
def drop(container, num, inserter) : call_exists(range, container) { def drop(container, num, inserter) : call_exists(range, container) {
auto r = range(container); auto r := range(container);
auto i = num; auto i = num;
while ((i > 0) && (!r.empty())) { while ((i > 0) && (!r.empty())) {
r.pop_front(); r.pop_front();
@ -336,14 +334,14 @@ def drop(container, num, inserter) : call_exists(range, container) {
# Returns a new container with the given number of elements dropped from the given container # Returns a new container with the given number of elements dropped from the given container
def drop(container, num) { def drop(container, num) {
auto retval = new(container); auto retval := new(container);
drop(container, num, back_inserter(retval)); drop(container, num, back_inserter(retval));
retval; retval;
} }
def drop_while(container, f, inserter) : call_exists(range, container) { def drop_while(container, f, inserter) : call_exists(range, container) {
auto r = range(container); auto r := range(container);
while ((!r.empty())&& f(r.front())) { while ((!r.empty())&& f(r.front())) {
r.pop_front(); r.pop_front();
} }
@ -356,7 +354,7 @@ def drop_while(container, f, inserter) : call_exists(range, container) {
# Returns a new container with the given elements dropped that match the second value function # Returns a new container with the given elements dropped that match the second value function
def drop_while(container, f) { def drop_while(container, f) {
auto retval = new(container); auto retval := new(container);
drop_while(container, f, back_inserter(retval)); drop_while(container, f, back_inserter(retval));
retval; retval;
} }
@ -364,7 +362,7 @@ def drop_while(container, f) {
# Applies the second value function to the container. Starts with the first two elements. Expects at least 2 elements. # Applies the second value function to the container. Starts with the first two elements. Expects at least 2 elements.
def reduce(container, func) : container.size() >= 2 && call_exists(range, container) { def reduce(container, func) : container.size() >= 2 && call_exists(range, container) {
auto r = range(container); auto r := range(container);
auto retval = r.front(); auto retval = r.front();
r.pop_front(); r.pop_front();
retval = func(retval, r.front()); retval = func(retval, r.front());
@ -380,7 +378,7 @@ def reduce(container, func) : container.size() >= 2 && call_exists(range, contai
# Returns a string of the elements in container delimited by the second value string # Returns a string of the elements in container delimited by the second value string
def join(container, delim) { def join(container, delim) {
auto retval = ""; auto retval = "";
auto range = range(container); auto range := range(container);
if (!range.empty()) { if (!range.empty()) {
retval += to_string(range.front()); retval += to_string(range.front());
range.pop_front(); range.pop_front();
@ -395,7 +393,7 @@ def join(container, delim) {
def filter(container, f, inserter) : call_exists(range, container) { def filter(container, f, inserter) : call_exists(range, container) {
auto r = range(container); auto r := range(container);
while (!r.empty()) { while (!r.empty()) {
if (f(r.front())) { if (f(r.front())) {
inserter(r.front()); inserter(r.front());
@ -407,7 +405,7 @@ def filter(container, f, inserter) : call_exists(range, container) {
# Returns a new Vector which match the second value function # Returns a new Vector which match the second value function
def filter(container, f) { def filter(container, f) {
auto retval = new(container); auto retval := new(container);
filter(container, f, back_inserter(retval)); filter(container, f, back_inserter(retval));
retval; retval;
} }
@ -424,7 +422,7 @@ def generate_range(x, y, inserter) {
# Returns a new Vector which represents the range from the first value to the second value # Returns a new Vector which represents the range from the first value to the second value
def generate_range(x, y) { def generate_range(x, y) {
auto retval = Vector(); auto retval := Vector();
generate_range(x,y,back_inserter(retval)); generate_range(x,y,back_inserter(retval));
retval; retval;
} }
@ -437,8 +435,8 @@ def collate(x, y) {
def zip_with(f, x, y, inserter) : call_exists(range, x) && call_exists(range, y) { def zip_with(f, x, y, inserter) : call_exists(range, x) && call_exists(range, y) {
auto r_x = range(x); auto r_x := range(x);
auto r_y = range(y); auto r_y := range(y);
while (!r_x.empty() && !r_y.empty()) { while (!r_x.empty() && !r_y.empty()) {
inserter(f(r_x.front(), r_y.front())); inserter(f(r_x.front(), r_y.front()));
r_x.pop_front(); r_x.pop_front();
@ -449,7 +447,7 @@ def zip_with(f, x, y, inserter) : call_exists(range, x) && call_exists(range, y)
# Returns a new Vector which joins matching elements of the second and third value with the first value function # Returns a new Vector which joins matching elements of the second and third value with the first value function
def zip_with(f, x, y) { def zip_with(f, x, y) {
auto retval = Vector(); auto retval := Vector();
zip_with(f,x,y,back_inserter(retval)); zip_with(f,x,y,back_inserter(retval));
retval; retval;
} }
@ -513,7 +511,7 @@ def string::trim() {
def find(container, value, compare_func) : call_exists(range, container) && is_type(compare_func, "Function") { def find(container, value, compare_func) : call_exists(range, container) && is_type(compare_func, "Function") {
auto range = range(container); auto range := range(container);
while (!range.empty()) { while (!range.empty()) {
if (compare_func(range.front(), value)) { if (compare_func(range.front(), value)) {
return range; return range;
@ -521,12 +519,12 @@ def find(container, value, compare_func) : call_exists(range, container) && is_t
range.pop_front(); range.pop_front();
} }
} }
return range; range;
} }
def find(container, value) { def find(container, value) {
return find(container, value, eq) find(container, value, eq)
} }