Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7229


Ignore:
Timestamp:
Aug 27, 2010, 2:53:06 PM (14 years ago)
Author:
landauf
Message:

implemented command cache in CommandExecutor

Location:
code/branches/consolecommands3/src/libraries/core/command
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • code/branches/consolecommands3/src/libraries/core/command/CommandExecutor.cc

    r7228 r7229  
    6161            return TclBind::eval(command, error);
    6262        else
    63             return CommandExecutor::evaluate(command).query(error);
     63        {
     64            CommandEvaluation evaluation;
     65            if (!CommandExecutor::getInstance().getCached(command, evaluation))
     66            {
     67COUT(0) << "evaluate" << std::endl;
     68                evaluation = CommandExecutor::evaluate(command);
     69                CommandExecutor::getInstance().cache(command, evaluation);
     70            }
     71            else
     72            {
     73COUT(0) << "cached" << std::endl;
     74            }
     75
     76            return evaluation.query(error);
     77        }
    6478    }
    6579
     
    97111        return evaluation;
    98112    }
     113
     114    bool CommandExecutor::getCached(const std::string& command, CommandEvaluation& evaluation)
     115    {
     116        if (Shell::getCacheSize() == 0)
     117            return false;
     118
     119        std::map<std::string, CacheEntry>::iterator it = this->cache_.find(command);
     120        if (it != this->cache_.end())
     121        {
     122            // update ordered list of cached commands (move it to the front)
     123            this->cachelist_.erase(it->second.iterator_);
     124            this->cachelist_.push_front(command);
     125            it->second.iterator_ = this->cachelist_.begin();
     126
     127            // assign the cached evaluation
     128            evaluation = it->second.evaluation_;
     129            return true;
     130        }
     131        return false;
     132    }
     133
     134    void CommandExecutor::cache(const std::string& command, const CommandEvaluation& evaluation)
     135    {
     136        if (Shell::getCacheSize() == 0)
     137            return;
     138
     139        // push command to the front of the ordered list
     140        this->cachelist_.push_front(command);
     141
     142        // create a cache entry and store it in the cache
     143        CacheEntry entry;
     144        entry.evaluation_ = evaluation;
     145        entry.iterator_ = this->cachelist_.begin();
     146        this->cache_[command] = entry;
     147
     148        // remove the last command in the ordered list from the cache if it exceeds the maximum size of the cache
     149        if (this->cachelist_.size() > Shell::getCacheSize())
     150        {
     151            this->cache_.erase(this->cachelist_.back());
     152            this->cachelist_.pop_back();
     153        }
     154    }
    99155}
  • code/branches/consolecommands3/src/libraries/core/command/CommandExecutor.h

    r7228 r7229  
    3232#include "core/CorePrereqs.h"
    3333
     34#include <map>
     35#include <list>
    3436#include <string>
    3537
     
    6567
    6668            static CommandExecutor& getInstance();
     69
     70            bool getCached(const std::string& command, CommandEvaluation& evaluation);
     71            void cache(const std::string& command, const CommandEvaluation& evaluation);
     72
     73            struct CacheEntry
     74            {
     75                CommandEvaluation evaluation_;
     76                std::list<std::string>::iterator iterator_;
     77            };
     78
     79            std::map<std::string, CacheEntry> cache_;
     80            std::list<std::string> cachelist_;
    6781    }; // tolua_export
    6882} // tolua_export
  • code/branches/consolecommands3/src/libraries/core/command/Shell.cc

    r7228 r7229  
    4646    _SetConsoleCommand("debug",   OutputHandler::debug  );
    4747
     48    unsigned int Shell::cacheSize_s;
     49
    4850    Shell::Shell(const std::string& consoleName, bool bScrollable)
    4951        : OutputListener(consoleName)
     
    99101            .callback(this, &Shell::commandHistoryOffsetChanged);
    100102        setConfigValueGeneric(this, &commandHistory_, ConfigFileType::CommandHistory, "Shell", "commandHistory_", std::vector<std::string>());
     103        SetConfigValue(cacheSize_s, 32);
    101104
    102105#ifdef ORXONOX_RELEASE
  • code/branches/consolecommands3/src/libraries/core/command/Shell.h

    r7203 r7229  
    113113            void setPromptPrefix(const std::string& str);
    114114
     115            static inline unsigned int getCacheSize()
     116                { return Shell::cacheSize_s; }
     117
    115118        private:
    116119            Shell(const Shell& other);
     
    167170            std::vector<std::string>  commandHistory_;
    168171            int                       softDebugLevel_;
     172            static unsigned int       cacheSize_s;
    169173    };
    170174}
Note: See TracChangeset for help on using the changeset viewer.