Skip to content

Commit

Permalink
add array type cache disable/enable
Browse files Browse the repository at this point in the history
  • Loading branch information
gewang committed Oct 26, 2024
1 parent f489c56 commit 3bddedd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/core/chuck_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,9 @@ t_CKBOOL type_engine_init( Chuck_Carrier * carrier )
EM_log( CK_LOG_HERALD, "adding base classes..." );
EM_pushlog();

//-------------------------
// disable array type cache until ckt_array is initialized | 1.5.3.5 (ge) added
env->arrayTypeCache()->enable( FALSE );
// special: Object and Type, whose initializations mutually depend
init_class_object( env, env->ckt_object );
// special: @array and Type, whose initializations mutually depend
Expand All @@ -567,6 +570,9 @@ t_CKBOOL type_engine_init( Chuck_Carrier * carrier )
// initialize of Object's and @array's Type objects are deferred until after init_class_type()
type_engine_init_special( env, env->ckt_object );
type_engine_init_special( env, env->ckt_array );
// enable array type cache now that ckt_array is initialized | 1.5.3.5 (ge) added
env->arrayTypeCache()->enable( TRUE );
//-------------------------

// initialize the remaining internal classes
init_class_string( env, env->ckt_string );
Expand Down Expand Up @@ -7990,19 +7996,23 @@ Chuck_Type * Chuck_ArrayTypeCache::getOrCreate( Chuck_Env * env,
Chuck_Type * base_type /* ,
Chuck_Namespace * owner_nspc */ )
{
// if cache not enabled
if( !m_enabled )
{
// create and return
return create_new_array_type( env, array_parent, depth, base_type );
}

// return value
Chuck_Type * type = NULL;

// look for key
std::map<Chuck_ArrayTypeKey, Chuck_Type *, Chuck_ArrayTypeKeyCmp>::iterator it = cache.find(Chuck_ArrayTypeKey(array_parent,depth,base_type));

// if found
if( false ) // disabled for now, iuntil
// if( it != cache.end() )
// if( cache.count( Chuck_ArrayTypeKey(array_parent, depth, base_type) ) )
if( it != cache.end() )
{
// get the value from cache
type = it->second;
// type = cache[Chuck_ArrayTypeKey(array_parent, depth, base_type)];
}
else // not found
{
Expand Down
10 changes: 10 additions & 0 deletions src/core/chuck_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -685,10 +685,16 @@ struct Chuck_ArrayTypeKeyCmp
struct Chuck_ArrayTypeCache
{
public:
// constructor
Chuck_ArrayTypeCache() : m_enabled(FALSE) { }
// destructor
virtual ~Chuck_ArrayTypeCache() { clear(); }
// clear it
void clear();
// enable or disable
void enable( t_CKBOOL yesOrNo ) { m_enabled = yesOrNo; }
// get whether enabled
t_CKBOOL isEnabled() const { return m_enabled; }
// lookup an array type; if not already cached, create and insert
Chuck_Type * getOrCreate( Chuck_Env * env,
Chuck_Type * array_parent,
Expand All @@ -698,6 +704,8 @@ struct Chuck_ArrayTypeCache
protected:
// the cache
std::map<Chuck_ArrayTypeKey, Chuck_Type *, Chuck_ArrayTypeKeyCmp> cache;
// is the cache currently enabled?
t_CKBOOL m_enabled;
};


Expand Down Expand Up @@ -746,6 +754,8 @@ struct Chuck_Env : public Chuck_VM_Object
Chuck_Compiler * compiler() const { return m_carrier ? m_carrier->compiler : NULL; }

public:
// array type cache
Chuck_ArrayTypeCache * arrayTypeCache() { return &array_types; }
// retrieve array type based on parameters | 1.5.3.5 (ge, nick, andrew) added
Chuck_Type * get_array_type( Chuck_Type * array_parent,
t_CKUINT depth, Chuck_Type * base_type /*,
Expand Down

0 comments on commit 3bddedd

Please sign in to comment.