Skip to content

Commit

Permalink
Fix performance issue (resolve #1069)
Browse files Browse the repository at this point in the history
  • Loading branch information
romainthomas committed Jun 9, 2024
1 parent fe10cd9 commit 7c3f631
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 16 deletions.
3 changes: 2 additions & 1 deletion include/LIEF/MachO/BinaryParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <limits>
#include <set>
#include <map>
#include <unordered_map>

#include "LIEF/visibility.h"
#include "LIEF/errors.hpp"
Expand Down Expand Up @@ -241,7 +242,7 @@ class LIEF_API BinaryParser : public LIEF::Parser {
bool is64_ = true;
ParserConfig config_;
std::set<uint64_t> visited_;
std::map<std::string, Symbol*> memoized_symbols_;
std::unordered_map<std::string, Symbol*> memoized_symbols_;
std::map<uint64_t, Symbol*> memoized_symbols_by_address_;

std::vector<DylibCommand*> binding_libs_;
Expand Down
3 changes: 0 additions & 3 deletions include/LIEF/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,6 @@ LIEF_API std::string hex_dump(const std::vector<uint8_t>& data,
LIEF_API std::string hex_dump(span<const uint8_t> data,
const std::string& sep = ":");

//! Check if the given string in printable
LIEF_API bool is_printable(const std::string& str);

//! Check if the given number is a hex-like string
LIEF_API bool is_hex_number(const std::string& nb);
}
Expand Down
5 changes: 2 additions & 3 deletions src/MachO/BinaryParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include "LIEF/MachO/ExportInfo.hpp"
#include "LIEF/MachO/DyldExportsTrie.hpp"

#include "LIEF/utils.hpp"
#include "internal_utils.hpp"

namespace LIEF {
namespace MachO {
Expand Down Expand Up @@ -302,10 +302,9 @@ ok_error_t BinaryParser::parse_export_trie(exports_list_t& exports, uint64_t sta
break;
}

if (visited_.count(start + child_node_offet) > 0) {
if (!visited_.insert(start + child_node_offet).second) {
break;
}
visited_.insert(start + child_node_offet);
size_t current_pos = stream_->pos();
stream_->setpos(start + child_node_offet);
parse_export_trie(exports, start, end, name, invalid_names);
Expand Down
4 changes: 0 additions & 4 deletions src/internal_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@
#include "internal_utils.hpp"
namespace LIEF {

inline bool is_printable(char c) {
return static_cast<bool>(::isprint(c)) && c != '\n' && c != '\r';
}

std::string printable_string(const std::string& str) {
std::string out;
out.reserve(str.size());
Expand Down
11 changes: 11 additions & 0 deletions src/internal_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@
namespace LIEF {
std::string printable_string(const std::string& str);

inline bool is_printable(char c) {
return ' ' <= c && c <= '~';
}

inline bool is_printable(const std::string& str) {
return std::all_of(std::begin(str), std::end(str),
[] (char c) { return is_printable(c); }
);
}

template<class T>
inline std::vector<T> as_vector(span<T> s) {
return std::vector<T>(s.begin(), s.end());
Expand Down Expand Up @@ -163,6 +173,7 @@ std::vector<std::string> optimize(const HANDLER& container,

return string_table_optimized;
}

}

#endif
5 changes: 0 additions & 5 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,6 @@ std::string hex_dump(span<const uint8_t> data, const std::string& sep) {
}


bool is_printable(const std::string& str) {
return std::all_of(std::begin(str), std::end(str),
[] (char c) { return std::isprint<char>(c, std::locale("C")); });
}

bool is_hex_number(const std::string& str) {
return std::all_of(std::begin(str), std::end(str), isxdigit);
}
Expand Down

0 comments on commit 7c3f631

Please sign in to comment.