Skip to content

Commit 8752526

Browse files
authoredMar 22, 2024··
Nacho/minor fixes tf2 cache (#658)
* Remove unused parameter * Make use of API function to improve redability ```cpp TimePoint TimeCache::getLatestTimestamp() { return storage_.front().stamp_; } ``` And std::list<T>::front() is(gcclib): ```cpp reference front() _GLIBCXX_NOEXCEPT { return *begin(); } ``` * Same argument as 321bd22 ```cpp TimePoint TimeCache::getLatestTimestamp() { // empty list case // ... return storage_.front().stamp_; } ``` and std::list<T>::front(): ```cpp reference front() _GLIBCXX_NOEXCEPT { return *begin(); } ``` * Improve readbility by relying on STL functions By now reading to this block I can tell that we are preventing to inserting a new element in the list, that has a timestamp that is actually older than the max_storage_time_ we allow for * Remove hardcoded algorithmg for STL one The intent of the code is now more clear, instead of relying on raw loops, we "find if" there is any element in the list that has a stamp older than the incoming one. With this we find the position in the list where we should insert the current timestamp: `storage_it` * Remove to better express what this pointer is represetngin * Replace raw loop for STL algorithm Remove if any element is older thant the max_storage_time_ allowed, relative to the latest(sooner) time seems clear npw
1 parent 5eabd34 commit 8752526

File tree

2 files changed

+16
-20
lines changed

2 files changed

+16
-20
lines changed
 

‎tf2/include/tf2/time_cache.h

-4
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,6 @@ constexpr tf2::Duration TIMECACHE_DEFAULT_MAX_STORAGE_TIME = std::chrono::second
104104
class TimeCache : public TimeCacheInterface
105105
{
106106
public:
107-
/// Number of nano-seconds to not interpolate below.
108-
TF2_PUBLIC
109-
static const int MIN_INTERPOLATION_DISTANCE = 5;
110-
111107
/// Maximum length of linked list, to make sure not to be able to use unlimited memory.
112108
TF2_PUBLIC
113109
static const unsigned int MAX_LENGTH_LINKED_LIST = 1000000;

‎tf2/src/cache.cpp

+16-16
Original file line numberDiff line numberDiff line change
@@ -247,23 +247,22 @@ CompactFrameID TimeCache::getParent(
247247

248248
bool TimeCache::insertData(const TransformStorage & new_data)
249249
{
250-
L_TransformStorage::iterator storage_it = storage_.begin();
250+
const TimePoint latest_time = getLatestTimestamp();
251251

252-
if (storage_it != storage_.end()) {
253-
if (storage_it->stamp_ > new_data.stamp_ + max_storage_time_) {
254-
return false;
255-
}
252+
// Avoid inserting data in the past that already exceeds the max_storage_time_
253+
if (!storage_.empty() && new_data.stamp_ < latest_time - max_storage_time_) {
254+
return false;
256255
}
257256

258-
while (storage_it != storage_.end()) {
259-
if (storage_it->stamp_ <= new_data.stamp_) {
260-
break;
261-
}
262-
storage_it++;
263-
}
257+
// Find the oldest element in the list before the incoming stamp.
258+
auto last_transform_pos = std::find_if(
259+
storage_.begin(), storage_.end(), [&](const auto & transfrom) {
260+
return transfrom.stamp_ <= new_data.stamp_;
261+
});
262+
264263
// Insert elements only if not already present
265264
if (std::find(storage_.begin(), storage_.end(), new_data) == storage_.end()) {
266-
storage_.insert(storage_it, new_data);
265+
storage_.insert(last_transform_pos, new_data);
267266
}
268267

269268
pruneList();
@@ -310,10 +309,11 @@ TimePoint TimeCache::getOldestTimestamp()
310309

311310
void TimeCache::pruneList()
312311
{
313-
TimePoint latest_time = storage_.begin()->stamp_;
312+
const TimePoint latest_time = getLatestTimestamp();
314313

315-
while (!storage_.empty() && storage_.back().stamp_ + max_storage_time_ < latest_time) {
316-
storage_.pop_back();
317-
}
314+
storage_.remove_if(
315+
[&](const auto & transform) {
316+
return transform.stamp_ < latest_time - max_storage_time_;
317+
});
318318
}
319319
} // namespace tf2

0 commit comments

Comments
 (0)
Please sign in to comment.