Chromium Code Reviews| Index: core/src/fxcodec/jbig2/JBig2_Context.cpp |
| diff --git a/core/src/fxcodec/jbig2/JBig2_Context.cpp b/core/src/fxcodec/jbig2/JBig2_Context.cpp |
| index 83ded6fbe18f15ac81bde0b7d0de95d9b25ad2be..ee2165c809a9c4f8d7c882660d369ef0c311fae5 100644 |
| --- a/core/src/fxcodec/jbig2/JBig2_Context.cpp |
| +++ b/core/src/fxcodec/jbig2/JBig2_Context.cpp |
| @@ -4,7 +4,20 @@ |
| // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| +#include <map> |
| +#include <list> |
| #include "JBig2_Context.h" |
| + |
| +// Implement a very small least recently used (LRU) cache. It is very |
| +// common for a JBIG2 dictionary to span multiple page in a PDF file, |
| +// and we do not want to decode the same dictionary over an over |
| +// again. We key off of the memory location of the dictionary. The |
| +// list keeps track of the freshness of enties, with freshest ones are |
| +// at the front. Even a tiny cache size like 2 makes a dramatic |
| +// difference for typical JBIG2 documents. |
| +const int kSymbolDictCacheMaxSize = 2; |
| +static list<pair<FX_BYTE*, CJBig2_SymbolDict*> > symbol_dict_cache; |
|
Lei Zhang
2014/12/12 00:01:20
You need std::
Lei Zhang
2014/12/12 00:01:20
I'd add a typedef for "pair<FX_BYTE*, CJBig2_Symbo
jab
2014/12/13 01:46:40
Done.
jab
2014/12/13 01:46:41
Done.
|
| + |
| void OutputBitmap(CJBig2_Image* pImage) |
| { |
| if(!pImage) { |
| @@ -614,6 +627,8 @@ FX_INT32 CJBig2_Context::parseSymbolDict(CJBig2_Segment *pSegment, IFX_Pause* pP |
| JBig2ArithCtx *gbContext = NULL, *grContext = NULL; |
| CJBig2_ArithDecoder *pArithDecoder; |
| JBIG2_ALLOC(pSymbolDictDecoder, CJBig2_SDDProc()); |
| + FX_BYTE *key = pSegment->m_pData; |
| + FX_BOOL cache_hit = false; |
| if(m_pStream->readShortInteger(&wFlags) != 0) { |
| m_pModule->JBig2_Error("symbol dictionary segment : data header too short."); |
| nRet = JBIG2_ERROR_TOO_SHORT; |
| @@ -791,23 +806,44 @@ FX_INT32 CJBig2_Context::parseSymbolDict(CJBig2_Segment *pSegment, IFX_Pause* pP |
| } |
| } |
| pSegment->m_nResultType = JBIG2_SYMBOL_DICT_POINTER; |
| - if(pSymbolDictDecoder->SDHUFF == 0) { |
| - JBIG2_ALLOC(pArithDecoder, CJBig2_ArithDecoder(m_pStream)); |
| - pSegment->m_Result.sd = pSymbolDictDecoder->decode_Arith(pArithDecoder, gbContext, grContext); |
| - delete pArithDecoder; |
| - if(pSegment->m_Result.sd == NULL) { |
| - nRet = JBIG2_ERROR_FETAL; |
| - goto failed; |
| + for(std::list<pair<FX_BYTE*, CJBig2_SymbolDict*> >::iterator it = |
| + symbol_dict_cache.begin(); it != symbol_dict_cache.end(); ++it) { |
| + if (it->first == key) { |
| + pSegment->m_Result.sd = it->second->DeepCopy(); |
| + symbol_dict_cache.erase(it); |
| + symbol_dict_cache.push_front(*it); |
| + cache_hit = true; |
| + break; |
| } |
| - m_pStream->alignByte(); |
| - m_pStream->offset(2); |
| - } else { |
| - pSegment->m_Result.sd = pSymbolDictDecoder->decode_Huffman(m_pStream, gbContext, grContext, pPause); |
| - if(pSegment->m_Result.sd == NULL) { |
| - nRet = JBIG2_ERROR_FETAL; |
| - goto failed; |
| + } |
| + if (!cache_hit) { |
| + if(pSymbolDictDecoder->SDHUFF == 0) { |
| + JBIG2_ALLOC(pArithDecoder, CJBig2_ArithDecoder(m_pStream)); |
| + pSegment->m_Result.sd = pSymbolDictDecoder->decode_Arith(pArithDecoder, gbContext, grContext); |
| + delete pArithDecoder; |
| + if(pSegment->m_Result.sd == NULL) { |
| + nRet = JBIG2_ERROR_FETAL; |
| + goto failed; |
| + } |
| + m_pStream->alignByte(); |
| + m_pStream->offset(2); |
| + } else { |
| + pSegment->m_Result.sd = pSymbolDictDecoder->decode_Huffman(m_pStream, gbContext, grContext, pPause); |
| + if(pSegment->m_Result.sd == NULL) { |
| + nRet = JBIG2_ERROR_FETAL; |
| + goto failed; |
| + } |
| + m_pStream->alignByte(); |
| + } |
| + CJBig2_SymbolDict *value = pSegment->m_Result.sd->DeepCopy(); |
| + if (value) { |
| + while (symbol_dict_cache.size() >= kSymbolDictCacheMaxSize) { |
| + delete symbol_dict_cache.back().second; |
| + symbol_dict_cache.pop_back(); |
| + } |
| + symbol_dict_cache.push_front( |
| + std::pair<FX_BYTE*, CJBig2_SymbolDict*>(key, value)); |
| } |
| - m_pStream->alignByte(); |
| } |
| if(wFlags & 0x0200) { |
| pSegment->m_Result.sd->m_bContextRetained = TRUE; |