diff --git a/moses/TranslationModel/PhraseDictionary.cpp b/moses/TranslationModel/PhraseDictionary.cpp index f36d29f19..ffe929a64 100644 --- a/moses/TranslationModel/PhraseDictionary.cpp +++ b/moses/TranslationModel/PhraseDictionary.cpp @@ -35,10 +35,48 @@ namespace Moses PhraseDictionary::PhraseDictionary(const std::string &description, const std::string &line) :DecodeFeature(description, line) ,m_tableLimit(20) // default + ,m_useCache(666) { } const TargetPhraseCollection *PhraseDictionary::GetTargetPhraseCollection(const Phrase& src) const +{ + const TargetPhraseCollection *ret; + if (m_useCache) { + size_t hash = hash_value(src); + + std::map::const_iterator iter; + + { // scope of read lock + #ifdef WITH_THREADS + boost::shared_lock read_lock(m_accessLock); + #endif + iter = m_cache.find(hash); + } + + if (iter == m_cache.end()) { + ret = GetTargetPhraseCollectionNonCache(src); + if (ret) { + ret = new TargetPhraseCollection(*ret); + } + + #ifdef WITH_THREADS + boost::unique_lock lock(m_accessLock); + #endif + m_cache[hash] = ret; + } + else { + ret = iter->second; + } + } + else { + ret = GetTargetPhraseCollectionNonCache(src); + } + + return ret; +} + +const TargetPhraseCollection *PhraseDictionary::GetTargetPhraseCollectionNonCache(const Phrase& src) const { UTIL_THROW(util::Exception, "Legacy method not implemented"); } @@ -54,7 +92,9 @@ GetTargetPhraseCollectionLegacy(InputType const& src,WordsRange const& range) co void PhraseDictionary::SetParameter(const std::string& key, const std::string& value) { - if (key == "path") { + if (key == "use-cache") { + m_useCache = Scan(value); + } else if (key == "path") { m_filePath = value; } else if (key == "table-limit") { m_tableLimit = Scan(value); diff --git a/moses/TranslationModel/PhraseDictionary.h b/moses/TranslationModel/PhraseDictionary.h index 0acfaff87..9e2c05eed 100644 --- a/moses/TranslationModel/PhraseDictionary.h +++ b/moses/TranslationModel/PhraseDictionary.h @@ -32,6 +32,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA #ifdef WITH_THREADS #include +#include #endif #include "moses/Phrase.h" @@ -112,6 +113,17 @@ protected: // MUST be called at the start of Load() void SetFeaturesToApply(); + + // cache + int m_useCache; // 666=not yet set, otherwise act like a bool + mutable std::map m_cache; +#ifdef WITH_THREADS + //reader-writer lock + mutable boost::shared_mutex m_accessLock; +#endif + + virtual const TargetPhraseCollection *GetTargetPhraseCollectionNonCache(const Phrase& src) const; + }; } diff --git a/moses/TranslationModel/PhraseDictionaryTreeAdaptor.cpp b/moses/TranslationModel/PhraseDictionaryTreeAdaptor.cpp index f6fcfe9d4..9dc7c1adc 100644 --- a/moses/TranslationModel/PhraseDictionaryTreeAdaptor.cpp +++ b/moses/TranslationModel/PhraseDictionaryTreeAdaptor.cpp @@ -28,7 +28,6 @@ namespace Moses PhraseDictionaryTreeAdaptor:: PhraseDictionaryTreeAdaptor(const std::string &line) : PhraseDictionary("PhraseDictionaryBinary", line) - , m_useCache(true) { ReadParameters(); } @@ -47,15 +46,6 @@ void PhraseDictionaryTreeAdaptor::Load() SetFeaturesToApply(); } -void PhraseDictionaryTreeAdaptor::SetParameter(const std::string& key, const std::string& value) -{ - if (key == "use-cache") { - m_useCache = Scan(value); - } else { - PhraseDictionary::SetParameter(key, value); - } -} - void PhraseDictionaryTreeAdaptor::InitializeForInput(InputType const& source) { const StaticData &staticData = StaticData::Instance(); @@ -88,40 +78,9 @@ void PhraseDictionaryTreeAdaptor::CleanUpAfterSentenceProcessing(InputType const } TargetPhraseCollection const* -PhraseDictionaryTreeAdaptor::GetTargetPhraseCollection(Phrase const &src) const +PhraseDictionaryTreeAdaptor::GetTargetPhraseCollectionNonCache(Phrase const &src) const { - const TargetPhraseCollection *ret; - if (m_useCache) { - size_t hash = hash_value(src); - - std::map::const_iterator iter; - - { // scope of read lock - #ifdef WITH_THREADS - boost::shared_lock read_lock(m_accessLock); - #endif - iter = m_cache.find(hash); - } - - if (iter == m_cache.end()) { - ret = GetImplementation().GetTargetPhraseCollection(src); - if (ret) { - ret = new TargetPhraseCollection(*ret); - } - - #ifdef WITH_THREADS - boost::unique_lock lock(m_accessLock); - #endif - m_cache[hash] = ret; - } - else { - ret = iter->second; - } - } - else { - ret = GetImplementation().GetTargetPhraseCollection(src); - } - + const TargetPhraseCollection *ret = GetImplementation().GetTargetPhraseCollection(src); return ret; } diff --git a/moses/TranslationModel/PhraseDictionaryTreeAdaptor.h b/moses/TranslationModel/PhraseDictionaryTreeAdaptor.h index 4b4bcd19e..f20e388eb 100644 --- a/moses/TranslationModel/PhraseDictionaryTreeAdaptor.h +++ b/moses/TranslationModel/PhraseDictionaryTreeAdaptor.h @@ -11,7 +11,6 @@ #ifdef WITH_THREADS #include -#include #else #include #endif @@ -45,21 +44,11 @@ class PhraseDictionaryTreeAdaptor : public PhraseDictionary PDTAimp& GetImplementation(); const PDTAimp& GetImplementation() const; - // cache - bool m_useCache; - mutable std::map m_cache; -#ifdef WITH_THREADS - //reader-writer lock - mutable boost::shared_mutex m_accessLock; -#endif - public: PhraseDictionaryTreeAdaptor(const std::string &line); virtual ~PhraseDictionaryTreeAdaptor(); void Load(); - void SetParameter(const std::string& key, const std::string& value); - // enable/disable caching // you enable caching if you request the target candidates for a source phrase multiple times // if you do caching somewhere else, disable it @@ -71,7 +60,7 @@ public: // get translation candidates for a given source phrase // returns null pointer if nothing found - TargetPhraseCollection const* GetTargetPhraseCollection(Phrase const &src) const; + TargetPhraseCollection const* GetTargetPhraseCollectionNonCache(Phrase const &src) const; void InitializeForInput(InputType const& source); void CleanUpAfterSentenceProcessing(InputType const& source);