using 2-level caching as suggested by @wsfulton

This commit is contained in:
Frank Schlimbach 2017-01-09 09:46:33 -06:00
commit be92482e27
2 changed files with 40 additions and 20 deletions

View file

@ -2,17 +2,22 @@
%import "amodel.i" %import "amodel.i"
%{ // C++ code to make this compilable %inline %{
namespace d { namespace d {
template< typename T > class d {}; template< typename T > class d {};
} }
namespace ns__a { %}
namespace iface1 {
class Model {}; %ignore ns__a::iface1::Model;
typedef d::d<Model> ModelPtr;
} %inline %{
using iface1::ModelPtr; namespace ns__a {
namespace iface1 {
class Model {};
typedef d::d<Model> ModelPtr;
} }
using iface1::ModelPtr;
}
%} %}
%inline %{ %inline %{
@ -23,6 +28,10 @@ namespace ns__b {
ns__a::ModelPtr foo() { return ns__a::ModelPtr(); }; ns__a::ModelPtr foo() { return ns__a::ModelPtr(); };
}; };
typedef d::d<Model> ModelPtr; typedef d::d<Model> ModelPtr;
ns__a::ModelPtr get_mp_a() { return ns__a::ModelPtr(); }
ModelPtr get_mp_b() { return ModelPtr(); }
} }
} }
%} %}
%template(AModelPtr) d::d<ns__a::iface1::Model>;
%template(BModelPtr) d::d<ns__b::iface1::Model>;

View file

@ -1906,26 +1906,37 @@ SwigType *Swig_symbol_template_deftype(const SwigType *type, Symtab *tscope) {
int len = Len(elements); int len = Len(elements);
int i; int i;
#ifdef SWIG_TEMPLATE_DEFTYPE_CACHE #ifdef SWIG_TEMPLATE_DEFTYPE_CACHE
static Hash *deftype_cache = 0; static Hash *s_cache = 0;
Hash *scope_cache;
/* The lookup depends on the current scope and potential namespace qualification. /* The lookup depends on the current scope and potential namespace qualification.
Looking up x in namespace y is not the same as looking up x::y in outer scope. Looking up x in namespace y is not the same as looking up x::y in outer scope.
-> we contruct an "artificial" key. */ -> we use a 2-level hash: first scope and then symbol. */
String *scopetype = tscope String *scope_name = tscope
? NewStringf("%s..%s::%s", Swig_symbol_qualifiedscopename(tscope), Getattr(tscope, "name"), type) ? Swig_symbol_qualifiedscopename(tscope)
: NewStringf("%s..%s::%s", Swig_symbol_qualifiedscopename(current_symtab), Swig_symbol_getscopename(), type); : Swig_symbol_qualifiedscopename(current_symtab);
if (!deftype_cache) { String *type_name = tscope
deftype_cache = NewHash(); ? NewStringf("%s::%s", Getattr(tscope, "name"), type)
: NewStringf("%s::%s", Swig_symbol_getscopename(), type);
if (!scope_name) scope_name = NewString("::");
if (!s_cache) {
s_cache = NewHash();
} }
if (scopetype) { scope_cache = Getattr(s_cache, scope_name);
String *cres = Getattr(deftype_cache, scopetype); if (scope_cache) {
String *cres = Getattr(scope_cache, type_name);
if (cres) { if (cres) {
Append(result, cres); Append(result, cres);
#ifdef SWIG_DEBUG #ifdef SWIG_DEBUG
Printf(stderr, "cached deftype %s(%s) -> %s\n", type, scopetype, result); Printf(stderr, "cached deftype %s(%s) -> %s\n", type, scope_name, result);
#endif #endif
Delete(scopetype); Delete(type_name);
Delete(scope_name);
return result; return result;
} }
} else {
scope_cache = NewHash();
Setattr(s_cache, scope_name, scope_cache);
Delete(scope_name);
} }
#endif #endif
@ -2025,8 +2036,8 @@ SwigType *Swig_symbol_template_deftype(const SwigType *type, Symtab *tscope) {
} }
Delete(elements); Delete(elements);
#ifdef SWIG_TEMPLATE_DEFTYPE_CACHE #ifdef SWIG_TEMPLATE_DEFTYPE_CACHE
Setattr(deftype_cache, scopetype, result); Setattr(scope_cache, type_name, result);
Delete(scopetype); Delete(type_name);
#endif #endif
return result; return result;