Skip to content

Commit 06d3b86

Browse files
committed
Use lower_bound to find jumpdest
1 parent 454f866 commit 06d3b86

File tree

2 files changed

+5
-7
lines changed

2 files changed

+5
-7
lines changed

lib/evmone/analysis.hpp

+4-7
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,10 @@ struct code_analysis
157157

158158
inline int find_jumpdest(const code_analysis& analysis, int offset) noexcept
159159
{
160-
// TODO: Replace with lower_bound().
161-
for (const auto& d : analysis.jumpdest_map)
162-
{
163-
if (d.first == offset)
164-
return d.second;
165-
}
166-
return -1;
160+
const auto& m = analysis.jumpdest_map;
161+
const auto it = std::lower_bound(std::begin(m), std::end(m), offset,
162+
[](std::pair<int, int> p, int v) noexcept { return p.first < v; });
163+
return (it != std::end(m) && it->first == offset) ? it->second : -1;
167164
}
168165

169166
EVMC_EXPORT code_analysis analyze(

test/unittests/analysis_test.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ TEST(analysis, jump1)
9090
EXPECT_EQ(analysis.jumpdest_map[0], std::pair(6, 5));
9191
EXPECT_EQ(find_jumpdest(analysis, 6), 5);
9292
EXPECT_EQ(find_jumpdest(analysis, 0), -1);
93+
EXPECT_EQ(find_jumpdest(analysis, 7), -1);
9394
}
9495

9596
TEST(analysis, empty)

0 commit comments

Comments
 (0)