Skip to content

Commit f3cc21d

Browse files
author
nullccxsy
committed
fix: fix comments, add transparent string hash to eliminate temporary std::string creation
1 parent 1348bd0 commit f3cc21d

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

src/iceberg/schema.cc

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ class IdToFieldVisitor {
4646
class NameToIdVisitor {
4747
public:
4848
explicit NameToIdVisitor(
49-
std::unordered_map<std::string, int32_t>& name_to_id, bool case_sensitive = true,
49+
std::unordered_map<std::string, int32_t, string_hash, std::equal_to<>>& name_to_id,
50+
bool case_sensitive = true,
5051
std::function<std::string(std::string_view)> quoting_func = {});
5152
Status Visit(const ListType& type, const std::string& path,
5253
const std::string& short_path);
@@ -64,8 +65,9 @@ class NameToIdVisitor {
6465

6566
private:
6667
bool case_sensitive_;
67-
std::unordered_map<std::string, int32_t>& name_to_id_;
68-
std::unordered_map<std::string, int32_t> short_name_to_id_;
68+
std::unordered_map<std::string, int32_t, string_hash, std::equal_to<>>& name_to_id_;
69+
std::unordered_map<std::string, int32_t, string_hash, std::equal_to<>>
70+
short_name_to_id_;
6971
std::function<std::string(std::string_view)> quoting_func_;
7072
};
7173

@@ -91,7 +93,7 @@ Result<std::optional<std::reference_wrapper<const SchemaField>>> Schema::FindFie
9193
std::string_view name, bool case_sensitive) const {
9294
if (case_sensitive) {
9395
ICEBERG_RETURN_UNEXPECTED(InitNameToIdMap());
94-
auto it = name_to_id_.find(std::string(name));
96+
auto it = name_to_id_.find(name);
9597
if (it == name_to_id_.end()) return std::nullopt;
9698
return FindFieldById(it->second);
9799
}
@@ -167,8 +169,8 @@ Status IdToFieldVisitor::VisitNestedType(const Type& type) {
167169
}
168170

169171
NameToIdVisitor::NameToIdVisitor(
170-
std::unordered_map<std::string, int32_t>& name_to_id, bool case_sensitive,
171-
std::function<std::string(std::string_view)> quoting_func)
172+
std::unordered_map<std::string, int32_t, string_hash, std::equal_to<>>& name_to_id,
173+
bool case_sensitive, std::function<std::string(std::string_view)> quoting_func)
172174
: name_to_id_(name_to_id),
173175
case_sensitive_(case_sensitive),
174176
quoting_func_(std::move(quoting_func)) {}

src/iceberg/schema.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@
3535

3636
namespace iceberg {
3737

38+
/// \brief Transparent hash function that supports std::string_view as lookup key
39+
///
40+
/// Enables std::unordered_map to directly accept std::string_view lookup keys
41+
/// without creating temporary std::string objects, using C++20's transparent lookup.
42+
struct string_hash {
43+
using hash_type = std::hash<std::string_view>;
44+
using is_transparent = void;
45+
46+
std::size_t operator()(std::string_view str) const { return hash_type{}(str); }
47+
};
48+
3849
/// \brief A schema for a Table.
3950
///
4051
/// A schema is a list of typed columns, along with a unique integer ID. A
@@ -78,9 +89,11 @@ class ICEBERG_EXPORT Schema : public StructType {
7889
mutable std::unordered_map<int32_t, std::reference_wrapper<const SchemaField>>
7990
id_to_field_;
8091
/// Mapping from field name to field id.
81-
mutable std::unordered_map<std::string, int32_t> name_to_id_;
92+
mutable std::unordered_map<std::string, int32_t, string_hash, std::equal_to<>>
93+
name_to_id_;
8294
/// Mapping from lowercased field name to field id
83-
mutable std::unordered_map<std::string, int32_t> lowercase_name_to_id_;
95+
mutable std::unordered_map<std::string, int32_t, string_hash, std::equal_to<>>
96+
lowercase_name_to_id_;
8497

8598
private:
8699
/// \brief Compare two schemas for equality.

0 commit comments

Comments
 (0)