Drastically reduce the number of exceptions thrown at runtime (cannot completely eliminate them all, it's the nature of doing what we are doing with making a runtime interface to a compiled system like we are).

profile.chai should see something like a reduction from 35,000 exceptions to about 100.
This commit is contained in:
Jason Turner
2010-08-07 19:27:15 +00:00
parent d838f7a6d4
commit 3a904d9f74
2 changed files with 73 additions and 51 deletions

View File

@@ -71,11 +71,11 @@ namespace chaiscript
virtual bool operator==(const Proxy_Function_Base &f) const
{
try
const Dynamic_Object_Function *df = dynamic_cast<const Dynamic_Object_Function *>(&f);
if (df)
{
const Dynamic_Object_Function &df = dynamic_cast<const Dynamic_Object_Function &>(f);
return df.m_type_name == m_type_name && (*df.m_func) == (*m_func);
} catch (const std::bad_cast &) {
return df->m_type_name == m_type_name && (*df->m_func) == (*m_func);
} else {
return false;
}
}
@@ -102,7 +102,7 @@ namespace chaiscript
virtual int get_arity() const
{
return m_func->get_arity();
return m_func->get_arity();
}
virtual std::string annotation() const
@@ -110,23 +110,42 @@ namespace chaiscript
return m_func->annotation();
}
protected:
virtual bool compare_first_type(const Boxed_Value &bv) const
{
return dynamic_object_typename_match(bv, m_type_name, m_ti);
}
private:
static bool dynamic_object_typename_match(const Boxed_Value &bv, const std::string &name,
const boost::optional<Type_Info> &ti)
{
static Type_Info doti = user_type<Dynamic_Object>();
if (bv.get_type_info().bare_equal(doti))
{
try {
const Dynamic_Object &d = boxed_cast<const Dynamic_Object &>(bv);
return name == "Dynamic_Object" || d.get_type_name() == name;
} catch (const std::bad_cast &) {
return false;
}
} else {
if (ti)
{
return bv.get_type_info().bare_equal(*ti);
} else {
return false;
}
}
}
static bool dynamic_object_typename_match(const std::vector<Boxed_Value> &bvs, const std::string &name,
const boost::optional<Type_Info> &ti)
{
if (bvs.size() > 0)
{
try {
const Dynamic_Object &d = boxed_cast<const Dynamic_Object &>(bvs[0]);
return name == "Dynamic_Object" || d.get_type_name() == name;
} catch (const std::bad_cast &) {
if (ti)
{
return bvs[0].get_type_info().bare_equal(*ti);
} else {
return false;
}
}
return dynamic_object_typename_match(bvs[0], name, ti);
} else {
return false;
}
@@ -175,11 +194,11 @@ namespace chaiscript
virtual bool operator==(const Proxy_Function_Base &f) const
{
try
const Dynamic_Object_Constructor *dc = dynamic_cast<const Dynamic_Object_Constructor*>(&f);
if (dc)
{
const Dynamic_Object_Constructor &dc = dynamic_cast<const Dynamic_Object_Constructor&>(f);
return dc.m_type_name == m_type_name && (*dc.m_func) == (*m_func);
} catch (const std::bad_cast &) {
return dc->m_type_name == m_type_name && (*dc->m_func) == (*m_func);
} else {
return false;
}
}

View File

@@ -72,6 +72,7 @@ namespace chaiscript
virtual bool operator==(const Proxy_Function_Base &) const = 0;
virtual bool call_match(const std::vector<Boxed_Value> &vals) const = 0;
//! Return true if the function is a possible match
//! to the passed in values
bool filter(const std::vector<Boxed_Value> &vals) const
@@ -86,26 +87,7 @@ namespace chaiscript
{
return true;
} else {
const std::vector<Type_Info> &types = get_param_types();
if (types.size() < 2)
{
return true;
}
const Type_Info &ti = types[1];
if (ti.is_undef() || vals[0].get_type_info().is_undef()
|| ti.bare_equal(user_type<Boxed_Value>())
|| ti.bare_equal(user_type<Boxed_POD_Value>())
|| ti.bare_equal(vals[0].get_type_info())
|| dynamic_cast_converts(ti, vals[0].get_type_info()) )
{
return true;
} else {
return false;
}
return compare_first_type(vals[0]);
}
} else {
return false;
@@ -122,6 +104,31 @@ namespace chaiscript
{
}
virtual bool compare_first_type(const Boxed_Value &bv) const
{
const std::vector<Type_Info> &types = get_param_types();
if (types.size() < 2)
{
return true;
}
const Type_Info &ti = types[1];
if (ti.is_undef()
|| ti.bare_equal(user_type<Boxed_Value>())
|| (!bv.get_type_info().is_undef()
&& (ti.bare_equal(user_type<Boxed_POD_Value>())
|| ti.bare_equal(bv.get_type_info())
|| dynamic_cast_converts(ti, bv.get_type_info())
)
)
)
{
return true;
} else {
return false;
}
}
bool compare_types(const std::vector<Type_Info> &tis, const std::vector<Boxed_Value> &bvs) const
{
if (tis.size() - 1 != bvs.size())
@@ -368,12 +375,8 @@ namespace chaiscript
virtual bool operator==(const Proxy_Function_Base &t_func) const
{
try {
dynamic_cast<const Proxy_Function_Impl<Func> &>(t_func);
return true;
} catch (const std::bad_cast &) {
return false;
}
const Proxy_Function_Impl *pimpl = dynamic_cast<const Proxy_Function_Impl<Func> *>(&t_func);
return pimpl != 0;
}
virtual Boxed_Value operator()(const std::vector<Boxed_Value> &params) const
@@ -424,11 +427,11 @@ namespace chaiscript
virtual bool operator==(const Proxy_Function_Base &t_func) const
{
try {
const Attribute_Access<T, Class> &aa
= dynamic_cast<const Attribute_Access<T, Class> &>(t_func);
return m_attr == aa.m_attr;
} catch (const std::bad_cast &) {
const Attribute_Access<T, Class> * aa
= dynamic_cast<const Attribute_Access<T, Class> *>(&t_func);
if (aa) {
return m_attr == aa->m_attr;
} else {
return false;
}
}