[fbreader] Enabled thread-local plain file stream cache

This commit is contained in:
Slava Monich 2015-08-21 15:18:07 +03:00
parent c9a7225138
commit de7028bf5a
4 changed files with 35 additions and 14 deletions

View file

@ -153,6 +153,7 @@ bool ZLibrary::init(int& aArgc, char** &aArgv)
ZLQtImageManager::createInstance();
ZLEncodingCollection::Instance().registerProvider(new IConvEncodingConverterProvider());
ZLApplication::Instance();
ZLFile::initCache();
return true;
}

View file

@ -11,7 +11,7 @@ LINEBREAK_DIR = $$_PRO_FILE_PWD_/../linebreak
FBREADER_DIR = fbreader
DEFINES += \
FBREADER_DISABLE_ZLFILE_PLAIN_STREAM_CACHE=1 \
FBREADER_THREAD_LOCAL_ZLFILE_PLAIN_STREAM_CACHE=1 \
FBREADER_DISABLE_BOOKS_DB=1
# Core
@ -335,12 +335,13 @@ SOURCES += \
$$FBREADER_SRC/formats/pdb/PluckerPlugin.cpp \
$$FBREADER_SRC/formats/pdb/EReaderPlugin.cpp \
$$FBREADER_SRC/formats/xhtml/XHTMLReader.cpp \
$$FBREADER_SRC/formats/oeb/NCXReader.cpp \
$$FBREADER_SRC/formats/oeb/OCFContainerReader.cpp \
$$FBREADER_SRC/formats/oeb/OEBPlugin.cpp \
$$FBREADER_SRC/formats/oeb/OEBMetaInfoReader.cpp \
$$FBREADER_SRC/formats/oeb/OEBCoverReader.cpp \
$$FBREADER_SRC/formats/oeb/OEBTextStream.cpp \
$$FBREADER_SRC/formats/oeb/OEBBookReader.cpp \
$$FBREADER_SRC/formats/oeb/NCXReader.cpp \
$$FBREADER_SRC/formats/PluginCollection.cpp \
$$FBREADER_SRC/formats/html/HtmlPlugin.cpp \
$$FBREADER_SRC/formats/html/HtmlReaderStream.cpp \
@ -427,10 +428,11 @@ HEADERS += \
$$FBREADER_SRC/formats/pdb/EReaderStream.h \
$$FBREADER_SRC/formats/pdb/PluckerTextStream.h \
$$FBREADER_SRC/formats/xhtml/XHTMLReader.h \
$$FBREADER_SRC/formats/oeb/NCXReader.h \
$$FBREADER_SRC/formats/oeb/OCFContainerReader.h \
$$FBREADER_SRC/formats/oeb/OEBBookReader.h \
$$FBREADER_SRC/formats/oeb/OEBTextStream.h \
$$FBREADER_SRC/formats/oeb/OEBPlugin.h \
$$FBREADER_SRC/formats/oeb/NCXReader.h \
$$FBREADER_SRC/formats/oeb/OEBMetaInfoReader.h \
$$FBREADER_SRC/formats/oeb/OEBCoverReader.h \
$$FBREADER_SRC/formats/html/HtmlPlugin.h \

View file

@ -1,5 +1,6 @@
/*
* Copyright (C) 2004-2010 Geometer Plus <contact@geometerplus.com>
* Copyright (C) 2015 Slava Monich <slava.monich@jolla.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -32,14 +33,28 @@
const ZLFile ZLFile::NO_FILE;
#ifndef FBREADER_DISABLE_ZLFILE_PLAIN_STREAM_CACHE
# define FBREADER_DISABLE_ZLFILE_PLAIN_STREAM_CACHE 0
typedef std::map<std::string,weak_ptr<ZLInputStream> > ZLFilePlainStreamCache;
#ifndef FBREADER_THREAD_LOCAL_ZLFILE_PLAIN_STREAM_CACHE
# define FBREADER_THREAD_LOCAL_ZLFILE_PLAIN_STREAM_CACHE 0
#endif
#if !FBREADER_DISABLE_ZLFILE_PLAIN_STREAM_CACHE
std::map<std::string,weak_ptr<ZLInputStream> > ZLFile::ourPlainStreamCache;
#if FBREADER_THREAD_LOCAL_ZLFILE_PLAIN_STREAM_CACHE
static pthread_key_t plainStreamCacheKey = (pthread_key_t)0;
static void ZLFilePlainStreamCacheDestructor(void *value) {
delete ((ZLFilePlainStreamCache*)value);
pthread_setspecific(plainStreamCacheKey, NULL);
}
#else
static ZLFilePlainStreamCache ourPlainStreamCache;
#endif
void ZLFile::initCache() {
#if FBREADER_THREAD_LOCAL_ZLFILE_PLAIN_STREAM_CACHE
pthread_key_create(&plainStreamCacheKey, ZLFilePlainStreamCacheDestructor);
#endif
}
ZLFile::ZLFile() : myMimeTypeIsUpToDate(true), myInfoIsFilled(true) {
}
@ -108,19 +123,23 @@ shared_ptr<ZLInputStream> ZLFile::inputStream() const {
int index = ZLFSManager::Instance().findArchiveFileNameDelimiter(myPath);
if (index == -1) {
#if !FBREADER_DISABLE_ZLFILE_PLAIN_STREAM_CACHE
#if FBREADER_THREAD_LOCAL_ZLFILE_PLAIN_STREAM_CACHE
ZLFilePlainStreamCache *cache = (ZLFilePlainStreamCache*)pthread_getspecific(plainStreamCacheKey);
if (!cache) {
cache = new ZLFilePlainStreamCache;
pthread_setspecific(plainStreamCacheKey, cache);
}
ZLFilePlainStreamCache &ourPlainStreamCache = *cache;
#endif
stream = ourPlainStreamCache[myPath];
if (stream.isNull()) {
#endif
if (isDirectory()) {
return 0;
}
stream = ZLFSManager::Instance().createPlainInputStream(myPath);
stream = envelopeCompressedStream(stream);
#if !FBREADER_DISABLE_ZLFILE_PLAIN_STREAM_CACHE
ourPlainStreamCache[myPath] = stream;
}
#endif
} else {
ZLFile baseFile(myPath.substr(0, index));
shared_ptr<ZLInputStream> base = baseFile.inputStream();

View file

@ -1,5 +1,6 @@
/*
* Copyright (C) 2004-2010 Geometer Plus <contact@geometerplus.com>
* Copyright (C) 2015 Slava Monich <slava.monich@jolla.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -35,10 +36,8 @@ class ZLFile {
public:
static const ZLFile NO_FILE;
private:
static std::map<std::string,weak_ptr<ZLInputStream> > ourPlainStreamCache;
public:
static void initCache();
static std::string fileNameToUtf8(const std::string &fileName);
static std::string replaceIllegalCharacters(const std::string &fileName, char replaceWith);