Some optimizations in one of the most expensive bits of the server, namely the command handler.

This commit is contained in:
Griatch 2012-04-25 18:47:03 +02:00
parent 21eed74c8d
commit a8373c685f
4 changed files with 23 additions and 17 deletions

View file

@ -392,7 +392,7 @@ class CmdSet(object):
""" """
names = [] names = []
if caller: if caller:
[names.extend([cmd.key] + cmd.aliases) for cmd in self.commands if cmd.access(caller)] [names.extend(cmd._keyaliases) for cmd in self.commands if cmd.access(caller)]
else: else:
[names.extend([cmd.key] + cmd.aliases) for cmd in self.commands] [names.extend(cmd._keyaliases) for cmd in self.commands]
return names return names

View file

@ -7,7 +7,7 @@ All commands in Evennia inherit from the 'Command' class in this module.
import re import re
from src.locks.lockhandler import LockHandler from src.locks.lockhandler import LockHandler
from src.utils.utils import is_iter from src.utils.utils import is_iter, fill
class CommandMeta(type): class CommandMeta(type):
""" """
@ -26,7 +26,13 @@ class CommandMeta(type):
mcs.aliases = mcs.aliases.split(',') mcs.aliases = mcs.aliases.split(',')
except Exception: except Exception:
mcs.aliases = [] mcs.aliases = []
mcs.aliases = [str(alias).strip() for alias in mcs.aliases] mcs.aliases = [str(alias).strip().lower() for alias in mcs.aliases]
# optimization - a set is much faster to match against than a list
mcs._matchset = set([mcs.key] + mcs.aliases)
# optimization for retrieving aliases and key as one list
mcs._keyaliases = [mcs.key] + mcs.aliases
# by default we don't save the command between runs
if not hasattr(mcs, "save_for_next"): if not hasattr(mcs, "save_for_next"):
mcs.save_for_next = False mcs.save_for_next = False
@ -134,9 +140,9 @@ class Command(object):
input can be either a cmd object or the name of a command. input can be either a cmd object or the name of a command.
""" """
try: try:
return self.match(cmd.key) return cmd.key in self._matchset
except AttributeError: # got a string except AttributeError: # got a string
return self.match(cmd) return cmd in self._matchset
def __contains__(self, query): def __contains__(self, query):
""" """
@ -144,22 +150,21 @@ class Command(object):
used by the help system, returning True if query can be found used by the help system, returning True if query can be found
as a substring of the commands key or its aliases. as a substring of the commands key or its aliases.
input can be either a command object or a command name. query (str) - query to match against. Should be lower case.
""" """
try: return any(query in keyalias for keyalias in self._matchset)
query = query.key
except AttributeError: # we got a string
pass
return (query in self.key) or any(query in alias for alias in self.aliases)
def match(self, cmdname): def match(self, cmdname):
""" """
This is called by the system when searching the available commands, This is called by the system when searching the available commands,
in order to determine if this is the one we wanted. cmdname was in order to determine if this is the one we wanted. cmdname was
previously extracted from the raw string by the system. previously extracted from the raw string by the system.
cmdname is always lowercase when reaching this point.
cmdname (str) is always lowercase when reaching this point.
""" """
return cmdname and ((cmdname == self.key) or (cmdname in self.aliases)) return cmdname in self._matchset
def access(self, srcobj, access_type="cmd", default=False): def access(self, srcobj, access_type="cmd", default=False):
""" """
@ -204,7 +209,7 @@ class Command(object):
of this module for which object properties are available of this module for which object properties are available
(beyond those set in self.parse()) (beyond those set in self.parse())
""" """
# a simple test command to show the available properties # a simple test command to show the available properties
string = "-" * 50 string = "-" * 50
string += "\n{w%s{n - Command variables from evennia:\n" % self.key string += "\n{w%s{n - Command variables from evennia:\n" % self.key
string += "-" * 50 string += "-" * 50
@ -216,6 +221,6 @@ class Command(object):
string += "object storing cmdset (self.obj): {w%s{n\n" % self.obj string += "object storing cmdset (self.obj): {w%s{n\n" % self.obj
string += "command string given (self.cmdstring): {w%s{n\n" % self.cmdstring string += "command string given (self.cmdstring): {w%s{n\n" % self.cmdstring
# show cmdset.key instead of cmdset to shorten output # show cmdset.key instead of cmdset to shorten output
string += utils.fill("current cmdset (self.cmdset): {w%s{n\n" % self.cmdset) string += fill("current cmdset (self.cmdset): {w%s{n\n" % self.cmdset)
self.caller.msg(string) self.caller.msg(string)

View file

@ -340,7 +340,7 @@ class LockHandler(object):
""" """
if self.reset_flag: if self.reset_flag:
# rebuild cache # on-demand cache rebuild
self._cache_locks(self.obj.lock_storage) self._cache_locks(self.obj.lock_storage)
self.reset_flag = False self.reset_flag = False

View file

@ -8,6 +8,7 @@ be of use when designing your own game.
""" """
from inspect import ismodule from inspect import ismodule
import os, sys, imp, types, math import os, sys, imp, types, math
from collections import Counter
import textwrap import textwrap
import datetime import datetime
import random import random