Skip to content

Commit b219744

Browse files
[gnc-engine-guile] reduce overhead of gnc_foo_to_scm
by caching the result of SWIG_TypeQuery(typestr)
1 parent 811ebf5 commit b219744

File tree

1 file changed

+23
-40
lines changed

1 file changed

+23
-40
lines changed

bindings/guile/gnc-engine-guile.cpp

+23-40
Original file line numberDiff line numberDiff line change
@@ -1733,37 +1733,28 @@ gnc_numeric_to_scm(gnc_numeric arg)
17331733
scm_divide (scm_from_int64 (arg.num), scm_from_int64 (arg.denom));
17341734
}
17351735

1736+
static swig_type_info*
1737+
get_swig_type (const gchar *type_str)
1738+
{
1739+
auto type = SWIG_TypeQuery (type_str);
1740+
if (!type)
1741+
PERR("Unknown SWIG Type: %s ", type_str);
1742+
return type;
1743+
}
1744+
17361745
static SCM
1737-
gnc_generic_to_scm(const void *cx, const gchar *type_str)
1746+
gnc_generic_to_scm (const void *cx, swig_type_info* stype)
17381747
{
1739-
swig_type_info * stype = nullptr;
17401748
void *x = (void*) cx;
1741-
1742-
if (!x) return SCM_BOOL_F;
1743-
stype = SWIG_TypeQuery(type_str);
1744-
1745-
if (!stype)
1746-
{
1747-
PERR("Unknown SWIG Type: %s ", type_str);
1748-
return SCM_BOOL_F;
1749-
}
1749+
if (!x || !stype) return SCM_BOOL_F;
17501750

17511751
return SWIG_NewPointerObj(x, stype, 0);
17521752
}
17531753

17541754
static void *
1755-
gnc_scm_to_generic(SCM scm, const gchar *type_str)
1755+
gnc_scm_to_generic (SCM scm, swig_type_info* stype)
17561756
{
1757-
swig_type_info * stype = nullptr;
1758-
1759-
stype = SWIG_TypeQuery(type_str);
1760-
if (!stype)
1761-
{
1762-
PERR("Unknown SWIG Type: %s ", type_str);
1763-
return nullptr;
1764-
}
1765-
1766-
if (!SWIG_IsPointerOfType(scm, stype))
1757+
if (!stype || !SWIG_IsPointerOfType(scm, stype))
17671758
return nullptr;
17681759

17691760
return SWIG_MustGetPtr(scm, stype, 1, 0);
@@ -1772,38 +1763,30 @@ gnc_scm_to_generic(SCM scm, const gchar *type_str)
17721763
gnc_commodity *
17731764
gnc_scm_to_commodity(SCM scm)
17741765
{
1775-
return static_cast<gnc_commodity*>(gnc_scm_to_generic(scm, "_p_gnc_commodity"));
1766+
static auto stype = get_swig_type ("_p_gnc_commodity");
1767+
return GNC_COMMODITY (gnc_scm_to_generic (scm, stype));
17761768
}
17771769

17781770
SCM
17791771
gnc_commodity_to_scm (const gnc_commodity *commodity)
17801772
{
1781-
return gnc_generic_to_scm(commodity, "_p_gnc_commodity");
1773+
static auto stype = get_swig_type ("_p_gnc_commodity");
1774+
return gnc_generic_to_scm (commodity, stype);
17821775
}
17831776

17841777
SCM
17851778
gnc_book_to_scm (const QofBook *book)
17861779
{
1787-
return gnc_generic_to_scm(book, "_p_QofBook");
1788-
}
1789-
1790-
static swig_type_info *
1791-
get_acct_type ()
1792-
{
1793-
static swig_type_info * account_type = nullptr;
1794-
1795-
if (!account_type)
1796-
account_type = SWIG_TypeQuery("_p_Account");
1797-
1798-
return account_type;
1780+
static auto stype = get_swig_type ("_p_QofBook");
1781+
return gnc_generic_to_scm (book, stype);
17991782
}
18001783

18011784
GncAccountValue * gnc_scm_to_account_value_ptr (SCM valuearg)
18021785
{
18031786
GncAccountValue *res;
18041787
Account *acc = nullptr;
18051788
gnc_numeric value;
1806-
swig_type_info * account_type = get_acct_type();
1789+
static auto account_type = get_swig_type ("_p_Account");
18071790
SCM val;
18081791

18091792
/* Get the account */
@@ -1826,7 +1809,7 @@ GncAccountValue * gnc_scm_to_account_value_ptr (SCM valuearg)
18261809

18271810
SCM gnc_account_value_ptr_to_scm (GncAccountValue *av)
18281811
{
1829-
swig_type_info * account_type = get_acct_type();
1812+
static auto account_type = get_swig_type ("_p_Account");
18301813
gnc_commodity * com;
18311814
gnc_numeric val;
18321815

@@ -1864,9 +1847,9 @@ scm_hook_cb (gpointer data, GncScmDangler *scm)
18641847
scm_call_0 (scm->proc);
18651848
else
18661849
{
1850+
static auto stype = get_swig_type ("_p_QofSession");
18671851
// XXX: FIXME: We really should make sure this is a session!!! */
1868-
scm_call_1 (scm->proc,
1869-
SWIG_NewPointerObj(data, SWIG_TypeQuery("_p_QofSession"), 0));
1852+
scm_call_1 (scm->proc, SWIG_NewPointerObj(data, stype, 0));
18701853
}
18711854

18721855
LEAVE("");

0 commit comments

Comments
 (0)