Skip to content

Commit

Permalink
For hashing type name, using std::hash<std::string_view> instead of
Browse files Browse the repository at this point in the history
std::hash<string> to avoid creating new std::string when doing hash,
which involve unnecessary memory allocation and copy
  • Loading branch information
Qingran Zheng committed Nov 1, 2023
1 parent 3c2b64a commit 9e676b1
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/nb_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <nanobind/nanobind.h>
#include <tsl/robin_map.h>
#include <cstring>
#include <string>
#include <string_view>
#include <functional>
#include "hash.h"

Expand Down Expand Up @@ -153,7 +153,8 @@ struct nb_weakref_seq {

struct std_typeinfo_hash {
size_t operator()(const std::type_info *a) const {
return std::hash<std::string>()(a->name());
const char *name = a->name();
return std::hash<std::string_view>()({name, strlen(name)});
}
};

Expand Down

0 comments on commit 9e676b1

Please sign in to comment.