Skip to content

Commit 007c263

Browse files
authored
Improve latest model version lookup (#240)
1 parent 669a7cd commit 007c263

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

src/model_lifecycle.cc

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -344,18 +344,17 @@ ModelLifeCycle::GetModel(
344344

345345
// The case where the request is asking for latest version
346346
int64_t latest = -1;
347-
for (auto& version_model : mit->second) {
348-
if (version_model.first > latest) {
349-
std::lock_guard<std::mutex> lock(version_model.second->mtx_);
350-
if (version_model.second->state_ == ModelReadyState::READY) {
351-
latest = version_model.first;
352-
// Tedious, but have to set handle for any "latest" version
353-
// at the moment to avoid edge case like the following:
354-
// "versions : 1 3 2", version 3 is latest but is requested
355-
// to be unloaded when the iterator is examining version 2,
356-
// then 'model' will ensure version 3 is still valid
357-
*model = version_model.second->model_;
358-
}
347+
static_assert(
348+
std::is_same<
349+
decltype(mit->second)::key_compare, std::less<int64_t>>::value,
350+
"Below assume versions are sorted in specific order");
351+
for (auto version_model = mit->second.rbegin();
352+
version_model != mit->second.rend(); ++version_model) {
353+
std::lock_guard<std::mutex> lock(version_model->second->mtx_);
354+
if (version_model->second->state_ == ModelReadyState::READY) {
355+
latest = version_model->first;
356+
*model = version_model->second->model_;
357+
break;
359358
}
360359
}
361360
if (latest == -1) {

0 commit comments

Comments
 (0)