Skip to content

Commit

Permalink
Merge pull request #4 from Clemapfel/performance
Browse files Browse the repository at this point in the history
Merge Performance to v0.7
  • Loading branch information
Clemapfel authored Feb 20, 2022
2 parents 2b96f96 + 73e862d commit 6db5d87
Show file tree
Hide file tree
Showing 29 changed files with 630 additions and 649 deletions.
13 changes: 9 additions & 4 deletions .benchmark/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Number_t generate_number(
return dist(engine);
}

size_t count = 10000;
size_t count = 1000;

void benchmark_lambda_call()
{
Expand Down Expand Up @@ -65,25 +65,30 @@ void benchmark_lambda_call()

int main()
{
std::cerr << "This executable is not intended for end-users and should not be used. To verify jluna works, instead execute \"./JLUNA_TEST\"" << std::endl;
return 1;

using namespace jluna;
State::initialize();

auto test = Main["abc"];

jl_eval_string("module M1; module M2; module M3; end end end");

Benchmark::run("Old Proxy Eval", count, [](){

auto name = generate_string(8);
jl_eval_string(("M1.M2.M3.eval(:(" + name + " = \"" + generate_string(16) + "\"))").c_str());

volatile auto value = Main["M1"]["M2"]["M3"][name].operator std::string();
//auto value = Main["M1"]["M2"]["M3"][name].operator std::string();
});

Benchmark::run("Old Proxy Assign", count, [](){

auto name = generate_string(8);
jl_eval_string(("M1.M2.M3.eval(:(" + name + " = undef))").c_str());

Main["M1"]["M2"]["M3"].as<Module>().assign(name, generate_string(16));
//Main["M1"]["M2"]["M3"].as<Module>().assign(name, generate_string(16));
});

Benchmark::conclude();
Expand Down Expand Up @@ -130,7 +135,7 @@ int main()

Benchmark::run("Proxy: CTOR DTOR Named", count, [](){

volatile auto* p = new Proxy(Main["test"]);
volatile auto* p = new Proxy(Main[std::string("test")]);
delete p;
});

Expand Down
33 changes: 18 additions & 15 deletions .src/array.inl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
// Created on 12.01.22 by clem ([email protected])
//

#include <include/gc_sentinel.hpp>

namespace jluna
{
namespace detail
Expand All @@ -17,17 +15,19 @@ namespace jluna
}

template<Boxable V, size_t R>
Array<V, R>::Array(jl_value_t* value, std::shared_ptr<typename Proxy::ProxyValue>& owner, jl_sym_t* symbol)
: Proxy(value, owner, symbol)
Array<V, R>::Array(Any* value, jl_sym_t* symbol)
: Proxy(value, symbol)
{
jl_assert_type(value, "Array");
static jl_datatype_t* array_t = (jl_datatype_t*) jl_eval_string("return Base.Array");
jl_assert_type(value, array_t);
}

template<Boxable V, size_t R>
Array<V, R>::Array(jl_value_t* value, jl_sym_t* symbol)
: Proxy(value, symbol)
Array<V, R>::Array(Proxy* proxy)
: Proxy(*proxy)
{
jl_assert_type(value, "Array");
static jl_datatype_t* array_t = (jl_datatype_t*) jl_eval_string("return Base.Array");
jl_assert_type(proxy->operator Any*(), array_t);
}

template<Boxable T, size_t Rank>
Expand Down Expand Up @@ -259,19 +259,19 @@ namespace jluna
template<Boxable V, size_t R>
size_t Array<V, R>::get_n_elements() const
{
return reinterpret_cast<jl_array_t*>(_content->value())->length;
return reinterpret_cast<const jl_array_t*>(this->operator const Any*())->length;
} //°

template<Boxable V, size_t R>
bool Array<V, R>::empty() const
{
return reinterpret_cast<jl_array_t*>(_content->value())->length == 0;
return reinterpret_cast<const jl_array_t*>(this->operator const Any*())->length == 0;
} //°

template<Boxable V, size_t R>
void* Array<V, R>::data()
{
return reinterpret_cast<jl_array_t*>(value())->data;
return reinterpret_cast<jl_array_t*>(this->operator Any*())->data;
} //°

// ###
Expand All @@ -287,17 +287,19 @@ namespace jluna
{}

template<Boxable V>
Vector<V>::Vector(jl_value_t* value, std::shared_ptr<typename Proxy::ProxyValue>& owner, jl_sym_t* symbol)
: Array<V, 1>(value, owner, symbol)
Vector<V>::Vector(Proxy* owner)
: Array<V, 1>(owner)
{
jl_assert_type(value, "Vector");
static jl_datatype_t* vector_t = (jl_datatype_t*) jl_eval_string("return Vector");
jl_assert_type(owner->operator Any*(), vector_t);
}

template<Boxable V>
Vector<V>::Vector(jl_value_t* value, jl_sym_t* symbol)
: Array<V, 1>(value, symbol)
{
jl_assert_type(value, "Vector");
static jl_datatype_t* vector_t = (jl_datatype_t*) jl_eval_string("return Vector");
jl_assert_type(value, vector_t);
}

template<Boxable V>
Expand All @@ -308,6 +310,7 @@ namespace jluna
vec.reserve(gen.get().size());
for (auto it : gen.get())
vec.push_back(unbox<V>(it));

return vec;
}())
{}
Expand Down
20 changes: 16 additions & 4 deletions .src/array_iterator.inl
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,16 @@ namespace jluna
template<Boxable V, size_t R>
Array<V, R>::ConstIterator::operator Proxy()
{
return Proxy(
jl_arrayref((jl_array_t*) _owner->_content->value(), _index),
_owner->_content,
jl_symbol(("[" + std::to_string(_index+1) + "]").c_str())
jl_gc_pause;

auto res = Proxy(
jl_arrayref((jl_array_t*) _owner->_content->value(), _index),
_owner->_content,
jl_box_uint64(_index+1)
);

jl_gc_unpause;
return res;
}

template<Boxable V, size_t R>
Expand Down Expand Up @@ -101,4 +106,11 @@ namespace jluna

return *this;
}

template<Boxable V, size_t R>
template<Unboxable T, std::enable_if_t<not std::is_same_v<T, Proxy>, bool>>
Array<V, R>::Iterator::operator T() const
{
return ConstIterator::operator T();
}
}
25 changes: 14 additions & 11 deletions .src/box.inl
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,14 @@ namespace jluna
template<typename T, typename Value_t, std::enable_if_t<std::is_same_v<T, std::vector<Value_t>>, bool>>
Any* box(const T& value)
{
static jl_function_t* vector = jl_get_function(jl_base_module, "Vector");
static jl_function_t* new_vector = jl_find_function("jluna", "new_vector");

jl_gc_pause;
auto* res = (jl_array_t*) jl_call2(vector, jl_undef_initializer(), jl_box_uint64(value.size()));
auto* res = (jl_array_t*) jl_call2(
new_vector,
jl_box_uint64(value.size()),
value.empty() ? jl_eval_string(to_julia_type<Value_t>::type_name.c_str()) : box<Value_t>(value.front())
);

for (size_t i = 0; i < value.size(); ++i)
jl_arrayset(res, box(value.at(i)), i);
Expand Down Expand Up @@ -179,8 +183,6 @@ namespace jluna
for (auto& pair : value)
pairs.push_back(jl_call2(make_pair, box<Key_t>(pair.first), box<Value_t>(pair.second)));

jl_gc_enable(before);

auto* res = jl_call(dict, pairs.data(), pairs.size());
jl_gc_unpause;
return res;
Expand All @@ -189,18 +191,19 @@ namespace jluna
template<typename T, typename Value_t, std::enable_if_t<std::is_same_v<T, std::set<Value_t>>, bool>>
Any* box(const T& value)
{
static jl_function_t* vector = jl_get_function(jl_base_module, "Vector");
static jl_function_t* new_vector = jl_find_function("jluna", "new_vector");
static jl_function_t* set = jl_get_function(jl_base_module, "Set");

jl_gc_pause;
auto* res = (jl_array_t*) jl_call2(vector, jl_undef_initializer(), jl_box_uint64(value.size()));
auto* res = (jl_array_t*) jl_call2(
new_vector,
jl_box_uint64(value.size()),
value.empty() ? jl_eval_string(to_julia_type<Value_t>::type_name.c_str()) : box<Value_t>(*value.begin())
);

size_t i = 0;
for (const auto& s : value)
{
jl_arrayset(res, box<Value_t>(s), i);
i += 1;
}
for (auto s : value)
jl_arrayset(res, box(s), i++);

auto* out = jl_call1(set, (Any*) res);
jl_gc_unpause;
Expand Down
8 changes: 4 additions & 4 deletions .src/cppcall.inl
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ namespace jluna
{
namespace detail
{
template<typename Lambda_t, typename Return_t, typename... Args_t, std::enable_if_t<std::is_same_v<Return_t, void>, Bool> = true>
jl_value_t* detail::invoke_lambda(const Lambda_t* func, Args_t... args)
template<typename Lambda_t, typename Return_t, typename... Args_t, std::enable_if_t<std::is_same_v<Return_t, void>, Bool>>
jl_value_t* invoke_lambda(const Lambda_t* func, Args_t... args)
{
(*func)(args...);
return jl_nothing;
}

template<typename Lambda_t, typename Return_t, typename... Args_t, std::enable_if_t<std::is_same_v<Return_t, jl_value_t*>, Bool> = true>
jl_value_t* detail::invoke_lambda(const Lambda_t* func, Args_t... args)
template<typename Lambda_t, typename Return_t, typename... Args_t, std::enable_if_t<std::is_same_v<Return_t, jl_value_t*>, Bool>>
jl_value_t* invoke_lambda(const Lambda_t* func, Args_t... args)
{
jl_value_t* res = (*func)(args...);
return res;
Expand Down
35 changes: 0 additions & 35 deletions .src/gc_sentinel.cpp

This file was deleted.

1 change: 0 additions & 1 deletion .src/generator_expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ namespace jluna

jl_gc_pause;
_length = jl_unbox_int64(jl_call1(length, get()));
std::cout << _length << std::endl;
forward_last_exception();
jl_gc_unpause;
}
Expand Down
8 changes: 4 additions & 4 deletions .src/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ namespace jluna
Module::Module(jl_module_t* value)
: Proxy((jl_value_t*) value, value->name)
{
jl_assert_type((Any*) value, "Module");
jl_assert_type((Any*) value, jl_module_type);
}

Module::Module(jl_value_t* value, std::shared_ptr<ProxyValue>& owner, jl_sym_t* name)
: Proxy(value, owner, name)
Module::Module(Proxy* owner)
: Proxy(*owner)
{
jl_assert_type(value, "Module");
jl_assert_type(owner->operator Any*(), jl_module_type);
}

jl_module_t * Module::get() const
Expand Down
Loading

0 comments on commit 6db5d87

Please sign in to comment.