Skip to content

败犬日报 2025-03-16

1. 群友锐评 io_uring

buffer size 需要 8K 才有收益,收益在 5% 以内,大多数情况都是劣化。

https://github.com/axboe/liburing/issues/801

2. unordered_map<string, int> 不能查找 string_view

下面的代码会报错 error: no matching function for call to 'xxx::find(std::string_view)'

cpp
#include <iostream>
#include <string>
#include <unordered_map>

int main() {
    std::unordered_map<std::string, int> a;
    a.find(std::string_view());
}

虽然 https://zh.cppreference.com/w/cpp/container/unordered_map/find 在 C++20 之后支持了模板接口 template< class K > const_iterator find( const K& x ) const;,但是 Hash 和 KeyEqual 必须是透明的,所以需要自定义 Hash:

cpp
#include <iostream>
#include <string>
#include <unordered_map>

struct my_hash : std::hash<std::string>, std::hash<std::string_view> {
    using std::hash<std::string>::operator();
    using std::hash<std::string_view>::operator();
    using is_transparent = void;
};

int main() {
    std::unordered_map<std::string, int, my_hash, std::equal_to<>> a;
    a.find(std::string_view());
}