mirror of
https://github.com/JakubMelka/PDF4QT.git
synced 2024-12-27 08:42:48 +01:00
Performance bugfixes
This commit is contained in:
parent
26a2a8deb5
commit
bc8617751e
@ -147,6 +147,11 @@ void PDFString::setString(const QByteArray& string)
|
||||
m_string = string;
|
||||
}
|
||||
|
||||
void PDFString::optimize()
|
||||
{
|
||||
m_string.shrink_to_fit();
|
||||
}
|
||||
|
||||
bool PDFArray::equals(const PDFObjectContent* other) const
|
||||
{
|
||||
Q_ASSERT(dynamic_cast<const PDFArray*>(other));
|
||||
@ -159,6 +164,11 @@ void PDFArray::appendItem(PDFObject object)
|
||||
m_objects.push_back(std::move(object));
|
||||
}
|
||||
|
||||
void PDFArray::optimize()
|
||||
{
|
||||
m_objects.shrink_to_fit();
|
||||
}
|
||||
|
||||
bool PDFDictionary::equals(const PDFObjectContent* other) const
|
||||
{
|
||||
Q_ASSERT(dynamic_cast<const PDFDictionary*>(other));
|
||||
@ -194,6 +204,16 @@ const PDFObject& PDFDictionary::get(const char* key) const
|
||||
}
|
||||
}
|
||||
|
||||
void PDFDictionary::optimize()
|
||||
{
|
||||
m_dictionary.shrink_to_fit();
|
||||
|
||||
for (DictionaryEntry& entry : m_dictionary)
|
||||
{
|
||||
entry.first.shrink_to_fit();
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<PDFDictionary::DictionaryEntry>::const_iterator PDFDictionary::find(const QByteArray& key) const
|
||||
{
|
||||
return std::find_if(m_dictionary.cbegin(), m_dictionary.cend(), [&key](const DictionaryEntry& entry) { return entry.first == key; });
|
||||
|
@ -47,6 +47,9 @@ public:
|
||||
/// Equals operator. Returns true, if content of this object is
|
||||
/// equal to the content of the other object.
|
||||
virtual bool equals(const PDFObjectContent* other) const = 0;
|
||||
|
||||
/// Optimizes memory consumption of this object
|
||||
virtual void optimize() = 0;
|
||||
};
|
||||
|
||||
class PDFFORQTLIBSHARED_EXPORT PDFObject
|
||||
@ -133,22 +136,22 @@ public:
|
||||
static inline PDFObject createReal(PDFReal value) { return PDFObject(Type::Real, value); }
|
||||
|
||||
/// Creates a name object
|
||||
static inline PDFObject createName(PDFObjectContentPointer&& value) { return PDFObject(Type::Name, std::move(value)); }
|
||||
static inline PDFObject createName(PDFObjectContentPointer&& value) { value->optimize(); return PDFObject(Type::Name, std::move(value)); }
|
||||
|
||||
/// Creates a reference object
|
||||
static inline PDFObject createReference(const PDFObjectReference& reference) { return PDFObject(Type::Reference, reference); }
|
||||
|
||||
/// Creates a string object
|
||||
static inline PDFObject createString(PDFObjectContentPointer&& value) { return PDFObject(Type::String, std::move(value)); }
|
||||
static inline PDFObject createString(PDFObjectContentPointer&& value) { value->optimize(); return PDFObject(Type::String, std::move(value)); }
|
||||
|
||||
/// Creates an array object
|
||||
static inline PDFObject createArray(PDFObjectContentPointer&& value) { return PDFObject(Type::Array, std::move(value)); }
|
||||
static inline PDFObject createArray(PDFObjectContentPointer&& value) { value->optimize(); return PDFObject(Type::Array, std::move(value)); }
|
||||
|
||||
/// Creates a dictionary object
|
||||
static inline PDFObject createDictionary(PDFObjectContentPointer&& value) { return PDFObject(Type::Dictionary, std::move(value)); }
|
||||
static inline PDFObject createDictionary(PDFObjectContentPointer&& value) { value->optimize(); return PDFObject(Type::Dictionary, std::move(value)); }
|
||||
|
||||
/// Creates a stream object
|
||||
static inline PDFObject createStream(PDFObjectContentPointer&& value) { return PDFObject(Type::Stream, std::move(value)); }
|
||||
static inline PDFObject createStream(PDFObjectContentPointer&& value) { value->optimize(); return PDFObject(Type::Stream, std::move(value)); }
|
||||
|
||||
private:
|
||||
template<typename T>
|
||||
@ -183,6 +186,9 @@ public:
|
||||
const QByteArray& getString() const { return m_string; }
|
||||
void setString(const QByteArray &getString);
|
||||
|
||||
/// Optimizes the string for memory consumption
|
||||
virtual void optimize() override;
|
||||
|
||||
private:
|
||||
QByteArray m_string;
|
||||
};
|
||||
@ -209,6 +215,9 @@ public:
|
||||
/// Appends object to the end of object list
|
||||
void appendItem(PDFObject object);
|
||||
|
||||
/// Optimizes the array for memory consumption
|
||||
virtual void optimize() override;
|
||||
|
||||
private:
|
||||
std::vector<PDFObject> m_objects;
|
||||
};
|
||||
@ -265,6 +274,9 @@ public:
|
||||
/// \param index Zero-based index of value in the dictionary
|
||||
const PDFObject& getValue(size_t index) const { return m_dictionary[index].second; }
|
||||
|
||||
/// Optimizes the dictionary for memory consumption
|
||||
virtual void optimize() override;
|
||||
|
||||
private:
|
||||
/// Finds an item in the dictionary array, if the item is not in the dictionary,
|
||||
/// then end iterator is returned.
|
||||
@ -299,6 +311,12 @@ public:
|
||||
/// Returns dictionary for this content stream
|
||||
const PDFDictionary* getDictionary() const { return &m_dictionary; }
|
||||
|
||||
/// Optimizes the stream for memory consumption
|
||||
virtual void optimize() override { m_dictionary.optimize(); m_content.shrink_to_fit(); }
|
||||
|
||||
/// Returns content of the stream
|
||||
const QByteArray* getContent() const { return &m_content; }
|
||||
|
||||
private:
|
||||
PDFDictionary m_dictionary;
|
||||
QByteArray m_content;
|
||||
|
@ -51,7 +51,6 @@ void PDFAbstractVisitor::acceptStream(const PDFStream* stream)
|
||||
PDFStatisticsCollector::PDFStatisticsCollector()
|
||||
{
|
||||
// We must avoid to allocate map item
|
||||
//std::vector<PDFObject::Type> types = PDFObject::getTypes();
|
||||
for (PDFObject::Type type : PDFObject::getTypes())
|
||||
{
|
||||
m_statistics.emplace(std::make_pair(type, Statistics()));
|
||||
@ -120,6 +119,13 @@ void PDFStatisticsCollector::visitStream(const PDFStream* stream)
|
||||
Statistics& statistics = m_statistics[PDFObject::Type::Stream];
|
||||
collectStatisticsOfDictionary(statistics, stream->getDictionary());
|
||||
|
||||
const QByteArray& byteArray = *stream->getContent();
|
||||
const uint64_t memoryConsumption = byteArray.size() * sizeof(char);
|
||||
const uint64_t memoryOverhead = (byteArray.capacity() - byteArray.size()) * sizeof(char);
|
||||
|
||||
statistics.memoryConsumptionEstimate += memoryConsumption;
|
||||
statistics.memoryOverheadEstimate += memoryOverhead;
|
||||
|
||||
acceptStream(stream);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user