Added cmdset caching to cmdhandler.

This commit is contained in:
Griatch 2013-10-20 13:42:56 +02:00
parent a00179b742
commit efa6d85574
2 changed files with 34 additions and 27 deletions

View file

@ -47,8 +47,8 @@ from src.utils.utils import string_suggestions, make_iter
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
__all__ = ("cmdhandler",) __all__ = ("cmdhandler",)
_GA = object.__getattribute__ _GA = object.__getattribute__
_CMDSET_MERGE_CACHE = {}
# This decides which command parser is to be used. # This decides which command parser is to be used.
# You have to restart the server for changes to take effect. # You have to restart the server for changes to take effect.
@ -193,6 +193,11 @@ def get_and_merge_cmdsets(caller, session, player, obj, callertype, sessid=None)
yield [report_to.msg(cmdset.errmessage) for cmdset in cmdsets if cmdset.key == "_CMDSET_ERROR"] yield [report_to.msg(cmdset.errmessage) for cmdset in cmdsets if cmdset.key == "_CMDSET_ERROR"]
if cmdsets: if cmdsets:
mergehash = tuple([id(cmdset) for cmdset in cmdsets]) # faster to do tuple on list than to build tuple directly
if mergehash in _CMDSET_MERGE_CACHE:
# cached merge exist; use that
cmdset = _CMDSET_MERGE_CACHE[mergehash]
else:
# we group and merge all same-prio cmdsets separately (this avoids order-dependent # we group and merge all same-prio cmdsets separately (this avoids order-dependent
# clashes in certain cases, such as when duplicates=True) # clashes in certain cases, such as when duplicates=True)
tempmergers = {} tempmergers = {}
@ -213,6 +218,7 @@ def get_and_merge_cmdsets(caller, session, player, obj, callertype, sessid=None)
#print "<%s(%s,%s)> onto <%s(%s,%s)>" % (merging_cmdset.key, merging_cmdset.priority, merging_cmdset.mergetype, #print "<%s(%s,%s)> onto <%s(%s,%s)>" % (merging_cmdset.key, merging_cmdset.priority, merging_cmdset.mergetype,
# cmdset.key, cmdset.priority, cmdset.mergetype) # cmdset.key, cmdset.priority, cmdset.mergetype)
cmdset = yield merging_cmdset + cmdset cmdset = yield merging_cmdset + cmdset
_CMDSET_MERGE_CACHE[mergehash] = cmdset
else: else:
cmdset = None cmdset = None

View file

@ -41,14 +41,15 @@ from twisted.internet.task import LoopingCall
# Tack on the root evennia directory to the python path and initialize django settings # Tack on the root evennia directory to the python path and initialize django settings
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))))
from game import settings os.environ["DJANGO_SETTINGS_MODULE"] = "game.settings"
try: #from game import settings
from django.conf import settings as settings2 #try:
settings2.configure() # from django.conf import settings as settings2
except RuntimeError # settings2.configure()
pass #except RuntimeError:
finally: # pass
del settings2 #finally:
# del settings2
from django.conf import settings from django.conf import settings
from src.utils import utils from src.utils import utils