Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(453)

Side by Side Diff: core/src/fxcodec/jbig2/JBig2_Context.cpp

Issue 761313004: Add a small LRU cache for the JBIG2 symbol dictionary. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: style changes, fix flaw with iterator Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | core/src/fxcodec/jbig2/JBig2_SymbolDict.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 PDFium Authors. All rights reserved. 1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 6
7 #include <map>
8 #include <list>
7 #include "JBig2_Context.h" 9 #include "JBig2_Context.h"
10
11 // Implement a very small least recently used (LRU) cache. It is very
12 // common for a JBIG2 dictionary to span multiple pages in a PDF file,
13 // and we do not want to decode the same dictionary over and over
14 // again. We key off of the memory location of the dictionary. The
15 // list keeps track of the freshness of entries, with freshest ones
16 // at the front. Even a tiny cache size like 2 makes a dramatic
17 // difference for typical JBIG2 documents.
18 const int kSymbolDictCacheMaxSize = 2;
19 typedef std::pair<FX_BYTE*, CJBig2_SymbolDict*> CJBig2_CachePair;
20 static std::list<CJBig2_CachePair> SymbolDictCache;
21
8 void OutputBitmap(CJBig2_Image* pImage) 22 void OutputBitmap(CJBig2_Image* pImage)
9 { 23 {
10 if(!pImage) { 24 if(!pImage) {
11 return; 25 return;
12 } 26 }
13 } 27 }
14 CJBig2_Context *CJBig2_Context::CreateContext(CJBig2_Module *pModule, FX_BYTE *p GlobalData, FX_DWORD dwGlobalLength, 28 CJBig2_Context *CJBig2_Context::CreateContext(CJBig2_Module *pModule, FX_BYTE *p GlobalData, FX_DWORD dwGlobalLength,
15 FX_BYTE *pData, FX_DWORD dwLength, FX_INT32 nStreamType, IFX_Pause* pPau se) 29 FX_BYTE *pData, FX_DWORD dwLength, FX_INT32 nStreamType, IFX_Pause* pPau se)
16 { 30 {
17 return new(pModule) CJBig2_Context(pGlobalData, dwGlobalLength, pData, dwLen gth, nStreamType, pPause); 31 return new(pModule) CJBig2_Context(pGlobalData, dwGlobalLength, pData, dwLen gth, nStreamType, pPause);
(...skipping 589 matching lines...) Expand 10 before | Expand all | Expand 10 after
607 FX_BYTE cSDHUFFDH, cSDHUFFDW, cSDHUFFBMSIZE, cSDHUFFAGGINST; 621 FX_BYTE cSDHUFFDH, cSDHUFFDW, cSDHUFFBMSIZE, cSDHUFFAGGINST;
608 CJBig2_HuffmanTable *Table_B1 = NULL, *Table_B2 = NULL, *Table_B3 = NULL, *T able_B4 = NULL, *Table_B5 = NULL; 622 CJBig2_HuffmanTable *Table_B1 = NULL, *Table_B2 = NULL, *Table_B3 = NULL, *T able_B4 = NULL, *Table_B5 = NULL;
609 FX_INT32 i, nIndex, nRet; 623 FX_INT32 i, nIndex, nRet;
610 CJBig2_Segment *pSeg = NULL, *pLRSeg = NULL; 624 CJBig2_Segment *pSeg = NULL, *pLRSeg = NULL;
611 FX_BOOL bUsed; 625 FX_BOOL bUsed;
612 CJBig2_Image ** SDINSYMS = NULL; 626 CJBig2_Image ** SDINSYMS = NULL;
613 CJBig2_SDDProc *pSymbolDictDecoder; 627 CJBig2_SDDProc *pSymbolDictDecoder;
614 JBig2ArithCtx *gbContext = NULL, *grContext = NULL; 628 JBig2ArithCtx *gbContext = NULL, *grContext = NULL;
615 CJBig2_ArithDecoder *pArithDecoder; 629 CJBig2_ArithDecoder *pArithDecoder;
616 JBIG2_ALLOC(pSymbolDictDecoder, CJBig2_SDDProc()); 630 JBIG2_ALLOC(pSymbolDictDecoder, CJBig2_SDDProc());
631 FX_BYTE *key = pSegment->m_pData;
632 FX_BOOL cache_hit = false;
617 if(m_pStream->readShortInteger(&wFlags) != 0) { 633 if(m_pStream->readShortInteger(&wFlags) != 0) {
618 m_pModule->JBig2_Error("symbol dictionary segment : data header too shor t."); 634 m_pModule->JBig2_Error("symbol dictionary segment : data header too shor t.");
619 nRet = JBIG2_ERROR_TOO_SHORT; 635 nRet = JBIG2_ERROR_TOO_SHORT;
620 goto failed; 636 goto failed;
621 } 637 }
622 pSymbolDictDecoder->SDHUFF = wFlags & 0x0001; 638 pSymbolDictDecoder->SDHUFF = wFlags & 0x0001;
623 pSymbolDictDecoder->SDREFAGG = (wFlags >> 1) & 0x0001; 639 pSymbolDictDecoder->SDREFAGG = (wFlags >> 1) & 0x0001;
624 pSymbolDictDecoder->SDTEMPLATE = (wFlags >> 10) & 0x0003; 640 pSymbolDictDecoder->SDTEMPLATE = (wFlags >> 10) & 0x0003;
625 pSymbolDictDecoder->SDRTEMPLATE = (wFlags >> 12) & 0x0003; 641 pSymbolDictDecoder->SDRTEMPLATE = (wFlags >> 12) & 0x0003;
626 cSDHUFFDH = (wFlags >> 2) & 0x0003; 642 cSDHUFFDH = (wFlags >> 2) & 0x0003;
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
784 gbContext = (JBig2ArithCtx*)m_pModule->JBig2_Malloc2(sizeof(JBig2Ari thCtx), dwTemp); 800 gbContext = (JBig2ArithCtx*)m_pModule->JBig2_Malloc2(sizeof(JBig2Ari thCtx), dwTemp);
785 JBIG2_memset(gbContext, 0, sizeof(JBig2ArithCtx)*dwTemp); 801 JBIG2_memset(gbContext, 0, sizeof(JBig2ArithCtx)*dwTemp);
786 } 802 }
787 if (pSymbolDictDecoder->SDREFAGG == 1) { 803 if (pSymbolDictDecoder->SDREFAGG == 1) {
788 dwTemp = pSymbolDictDecoder->SDRTEMPLATE ? 1 << 10 : 1 << 13; 804 dwTemp = pSymbolDictDecoder->SDRTEMPLATE ? 1 << 10 : 1 << 13;
789 grContext = (JBig2ArithCtx*)m_pModule->JBig2_Malloc2(sizeof(JBig2Ari thCtx), dwTemp); 805 grContext = (JBig2ArithCtx*)m_pModule->JBig2_Malloc2(sizeof(JBig2Ari thCtx), dwTemp);
790 JBIG2_memset(grContext, 0, sizeof(JBig2ArithCtx)*dwTemp); 806 JBIG2_memset(grContext, 0, sizeof(JBig2ArithCtx)*dwTemp);
791 } 807 }
792 } 808 }
793 pSegment->m_nResultType = JBIG2_SYMBOL_DICT_POINTER; 809 pSegment->m_nResultType = JBIG2_SYMBOL_DICT_POINTER;
794 if(pSymbolDictDecoder->SDHUFF == 0) { 810 for(std::list<CJBig2_CachePair>::iterator it =
795 JBIG2_ALLOC(pArithDecoder, CJBig2_ArithDecoder(m_pStream)); 811 SymbolDictCache.begin(); it != SymbolDictCache.end(); ++it) {
796 pSegment->m_Result.sd = pSymbolDictDecoder->decode_Arith(pArithDecoder, gbContext, grContext); 812 if (it->first == key) {
797 delete pArithDecoder; 813 pSegment->m_Result.sd = it->second->DeepCopy();
798 if(pSegment->m_Result.sd == NULL) { 814 SymbolDictCache.push_front(*it);
799 nRet = JBIG2_ERROR_FETAL; 815 SymbolDictCache.erase(it);
800 goto failed; 816 cache_hit = true;
817 break;
801 } 818 }
802 m_pStream->alignByte(); 819 }
803 m_pStream->offset(2); 820 if (!cache_hit) {
804 } else { 821 if(pSymbolDictDecoder->SDHUFF == 0) {
805 pSegment->m_Result.sd = pSymbolDictDecoder->decode_Huffman(m_pStream, gb Context, grContext, pPause); 822 JBIG2_ALLOC(pArithDecoder, CJBig2_ArithDecoder(m_pStream));
806 if(pSegment->m_Result.sd == NULL) { 823 pSegment->m_Result.sd = pSymbolDictDecoder->decode_Arith(pArithDecod er, gbContext, grContext);
807 nRet = JBIG2_ERROR_FETAL; 824 delete pArithDecoder;
808 goto failed; 825 if(pSegment->m_Result.sd == NULL) {
826 nRet = JBIG2_ERROR_FETAL;
827 goto failed;
828 }
829 m_pStream->alignByte();
830 m_pStream->offset(2);
831 } else {
832 pSegment->m_Result.sd = pSymbolDictDecoder->decode_Huffman(m_pStream , gbContext, grContext, pPause);
833 if(pSegment->m_Result.sd == NULL) {
834 nRet = JBIG2_ERROR_FETAL;
835 goto failed;
836 }
837 m_pStream->alignByte();
809 } 838 }
810 m_pStream->alignByte(); 839 CJBig2_SymbolDict *value = pSegment->m_Result.sd->DeepCopy();
840 if (value) {
841 while (SymbolDictCache.size() >= kSymbolDictCacheMaxSize) {
842 delete SymbolDictCache.back().second;
843 SymbolDictCache.pop_back();
844 }
845 SymbolDictCache.push_front(CJBig2_CachePair(key, value));
846 }
811 } 847 }
812 if(wFlags & 0x0200) { 848 if(wFlags & 0x0200) {
813 pSegment->m_Result.sd->m_bContextRetained = TRUE; 849 pSegment->m_Result.sd->m_bContextRetained = TRUE;
814 if(pSymbolDictDecoder->SDHUFF == 0) { 850 if(pSymbolDictDecoder->SDHUFF == 0) {
815 pSegment->m_Result.sd->m_gbContext = gbContext; 851 pSegment->m_Result.sd->m_gbContext = gbContext;
816 } 852 }
817 if(pSymbolDictDecoder->SDREFAGG == 1) { 853 if(pSymbolDictDecoder->SDREFAGG == 1) {
818 pSegment->m_Result.sd->m_grContext = grContext; 854 pSegment->m_Result.sd->m_grContext = grContext;
819 } 855 }
820 bUsed = TRUE; 856 bUsed = TRUE;
(...skipping 982 matching lines...) Expand 10 before | Expand all | Expand 10 after
1803 SBSYMCODES[CURTEMP].code = CURCODE; 1839 SBSYMCODES[CURTEMP].code = CURCODE;
1804 CURCODE = CURCODE + 1; 1840 CURCODE = CURCODE + 1;
1805 } 1841 }
1806 CURTEMP = CURTEMP + 1; 1842 CURTEMP = CURTEMP + 1;
1807 } 1843 }
1808 CURLEN = CURLEN + 1; 1844 CURLEN = CURLEN + 1;
1809 } 1845 }
1810 m_pModule->JBig2_Free(LENCOUNT); 1846 m_pModule->JBig2_Free(LENCOUNT);
1811 m_pModule->JBig2_Free(FIRSTCODE); 1847 m_pModule->JBig2_Free(FIRSTCODE);
1812 } 1848 }
OLDNEW
« no previous file with comments | « no previous file | core/src/fxcodec/jbig2/JBig2_SymbolDict.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698