Skip to content

Commit e72c158

Browse files
committed
Micro optimization of map lookup-or-insert
1 parent 1a8663c commit e72c158

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

lib/nlcore.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -546,19 +546,18 @@ size_t add_hessian_index(size_t x1, size_t x2, size_t &m_hessian_nnz,
546546

547547
size_t hessian_index;
548548
VariablePair varpair(x1, x2);
549-
auto iter = m_hessian_index_map.find(varpair);
550-
if (iter != m_hessian_index_map.end())
551-
{
552-
hessian_index = iter->second;
553-
}
554-
else
549+
auto [iter, inserted] = m_hessian_index_map.emplace(varpair, m_hessian_nnz);
550+
if (inserted)
555551
{
556552
hessian_index = m_hessian_nnz;
557-
m_hessian_index_map[varpair] = m_hessian_nnz;
558553
m_hessian_rows.push_back(x1);
559554
m_hessian_cols.push_back(x2);
560555
m_hessian_nnz += 1;
561556
}
557+
else
558+
{
559+
hessian_index = iter->second;
560+
}
562561
return hessian_index;
563562
}
564563

0 commit comments

Comments
 (0)