Skip to content

Commit

Permalink
Use lower_bound to find jumpdest
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed Aug 3, 2019
1 parent fdc1573 commit f49efce
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
11 changes: 4 additions & 7 deletions lib/evmone/analysis.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,10 @@ struct code_analysis

inline int find_jumpdest(const code_analysis& analysis, int offset) noexcept
{
// TODO: Replace with lower_bound().
for (const auto& d : analysis.jumpdest_map)
{
if (d.first == offset)
return d.second;
}
return -1;
const auto& m = analysis.jumpdest_map;
const auto it = std::lower_bound(std::begin(m), std::end(m), offset,
[](std::pair<int, int> p, int v) noexcept { return p.first < v; });
return (it != std::end(m) && it->first == offset) ? it->second : -1;
}

EVMC_EXPORT code_analysis analyze(
Expand Down
1 change: 1 addition & 0 deletions test/unittests/analysis_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ TEST(analysis, jump1)
EXPECT_EQ(analysis.jumpdest_map[0], std::pair(6, 5));
EXPECT_EQ(find_jumpdest(analysis, 6), 5);
EXPECT_EQ(find_jumpdest(analysis, 0), -1);
EXPECT_EQ(find_jumpdest(analysis, 7), -1);
}

TEST(analysis, empty)
Expand Down

0 comments on commit f49efce

Please sign in to comment.