renderer: use iterator rather than creating vectors

Creating vectors from the FrameHashCache was one of the slowest functions in the program, this should be significantly faster.
This commit is contained in:
itsmattkc
2021-07-11 16:47:12 -07:00
parent 9b0b3962fe
commit 8a58ee2bdb
9 changed files with 126 additions and 95 deletions
+62
View File
@@ -296,6 +296,68 @@ uint qHash(const TimeRange &r, uint seed)
return qHash(r.in(), seed) ^ qHash(r.out(), seed); return qHash(r.in(), seed) ^ qHash(r.out(), seed);
} }
TimeRangeListFrameIterator::TimeRangeListFrameIterator(const TimeRangeList &list, const rational &timebase) :
list_(list),
timebase_(timebase),
index_(-1),
size_(-1)
{
UpdateIndexIfNecessary();
}
bool TimeRangeListFrameIterator::GetNext(rational *out)
{
if (index_ == list_.size()) {
return false;
}
// Output current value
*out = current_;
// Determine next value by adding timebase
current_ += timebase_;
// If this time is outside the current range, jump to the next one
UpdateIndexIfNecessary();
return true;
}
int TimeRangeListFrameIterator::size()
{
if (size_ == -1) {
// Size isn't calculated automatically for optimization, so we'll calculate it now
size_ = 0;
foreach (const TimeRange &range, list_) {
rational start = Timecode::snap_time_to_timebase(range.in(), timebase_, Timecode::kCeil);
rational end = Timecode::snap_time_to_timebase(range.out(), timebase_, Timecode::kFloor);
if (end == range.out()) {
end -= timebase_;
}
int64_t start_ts = Timecode::time_to_timestamp(start, timebase_);
int64_t end_ts = Timecode::time_to_timestamp(end, timebase_);
size_ += 1 + (end_ts - start_ts);
}
}
return size_;
}
void TimeRangeListFrameIterator::UpdateIndexIfNecessary()
{
while (index_ < list_.size() && (index_ == -1 || current_ >= list_.at(index_).out())) {
index_++;
if (index_ < list_.size()) {
current_ = Timecode::snap_time_to_timebase(list_.at(index_).in(), timebase_, Timecode::kCeil);
}
}
}
} }
QDebug operator<<(QDebug debug, const olive::TimeRange &r) QDebug operator<<(QDebug debug, const olive::TimeRange &r)
+46
View File
@@ -22,6 +22,7 @@
#define TIMERANGE_H #define TIMERANGE_H
#include "rational.h" #include "rational.h"
#include "timecodefunctions.h"
namespace olive { namespace olive {
@@ -127,16 +128,61 @@ public:
return array_.last(); return array_.last();
} }
const TimeRange& at(int index) const
{
return array_.at(index);
}
const QVector<TimeRange>& internal_array() const const QVector<TimeRange>& internal_array() const
{ {
return array_; return array_;
} }
bool operator==(const TimeRangeList &rhs) const
{
return array_ == rhs.array_;
}
private: private:
QVector<TimeRange> array_; QVector<TimeRange> array_;
}; };
class TimeRangeListFrameIterator
{
public:
TimeRangeListFrameIterator(const TimeRangeList &list, const rational &timebase);
bool GetNext(rational *out);
QVector<rational> ToVector() const
{
TimeRangeListFrameIterator copy(list_, timebase_);
QVector<rational> times;
rational r;
while (copy.GetNext(&r)) {
times.append(r);
}
return times;
}
int size();
private:
void UpdateIndexIfNecessary();
TimeRangeList list_;
rational timebase_;
rational current_;
int index_;
int size_;
};
uint qHash(const TimeRange& r, uint seed = 0); uint qHash(const TimeRange& r, uint seed = 0);
} }
-71
View File
@@ -114,77 +114,6 @@ void FrameHashCache::ValidateFramesWithHash(const QByteArray &hash)
} }
} }
QList<rational> FrameHashCache::GetFramesWithHash(const QByteArray &hash)
{
QList<rational> times;
for (int64_t i=0; i<GetMapSize(); i++) {
if (time_hash_map_.at(i) == hash) {
times.append(ToTime(i));
}
}
return times;
}
QList<rational> FrameHashCache::TakeFramesWithHash(const QByteArray &hash)
{
TimeRangeList range_to_invalidate;
QList<rational> times;
for (int64_t i=0; i<GetMapSize(); i++) {
if (time_hash_map_.at(i) == hash) {
time_hash_map_[i].clear();
rational time = ToTime(i);
times.append(time);
range_to_invalidate.insert(TimeRange(time, time + timebase_));
}
}
foreach (const TimeRange& r, range_to_invalidate) {
// We apply a 0 job time because the graph hasn't changed to get here, so any renderer should
// be up to date already
Invalidate(r, 0);
}
return times;
}
QVector<rational> FrameHashCache::GetFrameListFromTimeRange(TimeRangeList range_list, const rational &timebase)
{
// If timebase is null, this will be an infinite loop
Q_ASSERT(!timebase.isNull());
QVector<rational> times;
foreach (const TimeRange &range, range_list) {
rational frame = Timecode::snap_time_to_timebase(range.in(), timebase, Timecode::kCeil);
while (frame < range.out()) {
times.append(frame);
frame += timebase;
}
}
return times;
}
QVector<rational> FrameHashCache::GetFrameListFromTimeRange(const TimeRangeList &range)
{
return GetFrameListFromTimeRange(range, timebase_);
}
QVector<rational> FrameHashCache::GetInvalidatedFrames()
{
return GetFrameListFromTimeRange(GetInvalidatedRanges());
}
QVector<rational> FrameHashCache::GetInvalidatedFrames(const TimeRange &intersecting)
{
return GetFrameListFromTimeRange(GetInvalidatedRanges().Intersects(intersecting));
}
bool FrameHashCache::SaveCacheFrame(const QByteArray& hash, bool FrameHashCache::SaveCacheFrame(const QByteArray& hash,
char* data, char* data,
const VideoParams& vparam, const VideoParams& vparam,
-15
View File
@@ -50,16 +50,6 @@ public:
void ValidateFramesWithHash(const QByteArray& hash); void ValidateFramesWithHash(const QByteArray& hash);
/**
* @brief Returns a list of frames that use a particular hash
*/
QList<rational> GetFramesWithHash(const QByteArray& hash);
/**
* @brief Same as FramesWithHash() but also removes these frames from the map
*/
QList<rational> TakeFramesWithHash(const QByteArray& hash);
QMap<rational, QByteArray> time_hash_map(); QMap<rational, QByteArray> time_hash_map();
/** /**
@@ -77,11 +67,6 @@ public:
FramePtr LoadCacheFrame(const QByteArray& hash) const; FramePtr LoadCacheFrame(const QByteArray& hash) const;
static FramePtr LoadCacheFrame(const QString& fn); static FramePtr LoadCacheFrame(const QString& fn);
static QVector<rational> GetFrameListFromTimeRange(TimeRangeList range_list, const rational& timebase);
QVector<rational> GetFrameListFromTimeRange(const TimeRangeList &range);
QVector<rational> GetInvalidatedFrames();
QVector<rational> GetInvalidatedFrames(const TimeRange& intersecting);
public slots: public slots:
void SetHash(const olive::rational &time, const QByteArray& hash, const qint64 &job_time, bool frame_exists); void SetHash(const olive::rational &time, const QByteArray& hash, const qint64 &job_time, bool frame_exists);
+8 -4
View File
@@ -91,8 +91,10 @@ void GenerateHashesInternal(ViewerOutput *viewer, FrameHashCache* cache, const Q
} }
} }
void PreviewAutoCacher::GenerateHashes(ViewerOutput *viewer, FrameHashCache* cache, const QVector<rational> &times, qint64 job_time) void PreviewAutoCacher::GenerateHashes(ViewerOutput *viewer, FrameHashCache* cache, TimeRangeListFrameIterator iterator, qint64 job_time)
{ {
QVector<rational> times = iterator.ToVector();
// Ensure number of threads doesn't exceed idealThreadCount for maximum concurrency // Ensure number of threads doesn't exceed idealThreadCount for maximum concurrency
int hashes_per_thread = times.size() / qMax(1, QThread::idealThreadCount()-1); int hashes_per_thread = times.size() / qMax(1, QThread::idealThreadCount()-1);
@@ -493,7 +495,7 @@ void PreviewAutoCacher::TryRender()
// If we're here, we must be able to render // If we're here, we must be able to render
if (!invalidated_video_.isEmpty()) { if (!invalidated_video_.isEmpty()) {
QVector<rational> frames = viewer_node_->video_frame_cache()->GetFrameListFromTimeRange(invalidated_video_); TimeRangeListFrameIterator frames(invalidated_video_, viewer_node_->video_frame_cache()->GetTimebase());
QFutureWatcher<void>* watcher = new QFutureWatcher<void>(); QFutureWatcher<void>* watcher = new QFutureWatcher<void>();
hash_tasks_.append(watcher); hash_tasks_.append(watcher);
@@ -578,9 +580,11 @@ void PreviewAutoCacher::RequeueFrames()
using_range = cache_range_; using_range = cache_range_;
} }
QVector<rational> invalidated_ranges = viewer_node_->video_frame_cache()->GetInvalidatedFrames(using_range); TimeRangeList invalidated = viewer_node_->video_frame_cache()->GetInvalidatedRanges().Intersects(using_range);
TimeRangeListFrameIterator invalidated_ranges(invalidated, viewer_node_->video_frame_cache()->GetTimebase());
foreach (const rational& t, invalidated_ranges) { rational t;
while (invalidated_ranges.GetNext(&t)) {
const QByteArray& hash = viewer_node_->video_frame_cache()->GetHash(t); const QByteArray& hash = viewer_node_->video_frame_cache()->GetHash(t);
RenderTicketWatcher* render_task = video_tasks_.key(hash); RenderTicketWatcher* render_task = video_tasks_.key(hash);
+1 -1
View File
@@ -80,7 +80,7 @@ public:
void ClearVideoDownloadQueue(bool wait = false); void ClearVideoDownloadQueue(bool wait = false);
private: private:
static void GenerateHashes(ViewerOutput *viewer, FrameHashCache *cache, const QVector<rational>& times, qint64 job_time); static void GenerateHashes(ViewerOutput *viewer, FrameHashCache *cache, TimeRangeListFrameIterator times, qint64 job_time);
void TryRender(); void TryRender();
+7 -4
View File
@@ -84,16 +84,19 @@ bool RenderTask::Render(ColorManager* manager,
if (!video_range.isEmpty()) { if (!video_range.isEmpty()) {
// Get list of discrete frames from range // Get list of discrete frames from range
QVector<rational> times = FrameHashCache::GetFrameListFromTimeRange(video_range, video_params().frame_rate_as_time_base()); TimeRangeListFrameIterator iterator(video_range, video_params().frame_rate_as_time_base());
QVector<QByteArray> hashes(times.size()); QVector<rational> times(iterator.size());
QVector<QByteArray> hashes(iterator.size());
// Generate hashes // Generate hashes
for (int i=0; i<times.size(); i++) { rational r;
for (int i=0; iterator.GetNext(&r); i++) {
if (IsCancelled()) { if (IsCancelled()) {
return true; return true;
} }
hashes[i] = RenderManager::instance()->Hash(viewer()->GetConnectedTextureOutput(), video_params_, times.at(i)); times[i] = r;
hashes[i] = RenderManager::instance()->Hash(viewer()->GetConnectedTextureOutput(), video_params_, r);
} }
// Filter out duplicates // Filter out duplicates
+1
View File
@@ -16,3 +16,4 @@
olive_add_test(General common-tests common-tests.cpp) olive_add_test(General common-tests common-tests.cpp)
olive_add_test(General rational-tests rational-tests.cpp) olive_add_test(General rational-tests rational-tests.cpp)
olive_add_test(General timerange-tests timerange-tests.cpp)
+1
View File
@@ -21,6 +21,7 @@
#include <iostream> #include <iostream>
#define OLIVE_ASSERT(x) if (!(x)) return false #define OLIVE_ASSERT(x) if (!(x)) return false
#define OLIVE_ASSERT_EQUAL(x, y) if (x != y) {std::cout << " - Equal assert failed on line " << __LINE__ << ": " << x << " != " << y; return false;}void()
#define OLIVE_TEST_END return true #define OLIVE_TEST_END return true
#define OLIVE_ADD_TEST(x) bool Test##x() #define OLIVE_ADD_TEST(x) bool Test##x()