From 1d832173c7d8a8478a61939c1b672968ea29b5aa Mon Sep 17 00:00:00 2001 From: Ted Mostly Date: Mon, 29 Jul 2024 01:17:51 +0800 Subject: [PATCH] 165 (#119) --- README.md | 9 +-- posts/164.md | 23 +++++++ posts/16405.md | 25 ------- posts/165.md | 182 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 210 insertions(+), 29 deletions(-) delete mode 100644 posts/16405.md create mode 100644 posts/165.md diff --git a/README.md b/README.md index cfb5976..081a1b0 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ RSS使用仓库的release RSS [链接](https://github.com/wanghenshui/cppweeklyn ## 2024 -| [161](./posts/161.md) |[162](./posts/162.md) | [163](./posts/163.md) |[164](./posts/164.md) [164修正](./posts/16405.md) | | | | | | | +| [161](./posts/161.md) |[162](./posts/162.md) | [163](./posts/163.md) |[164](./posts/164.md) [165](./posts/165.md) | | | | | | | | ------------------ | ------------------ | ------------------ | ------------------ | ------------------ | ------------------ | ------------------ | ------------------ | ------------------ | ------------------ | | [151](./posts/151.md) | [152](./posts/152.md) | [153](./posts/153.md) | [154](./posts/154.md) | [155](./posts/155.md) | [156](./posts/156.md) | [157](./posts/157.md) | [158](./posts/158.md) | [159](./posts/159.md) | [160](./posts/160.md) | | | | | | [145](./posts/145.md) | [146](./posts/146.md) | [147](./posts/147.md) | [148](./posts/148.md) | [149](./posts/149.md) | [150](./posts/150.md) | @@ -92,14 +92,14 @@ RSS使用仓库的release RSS [链接](https://github.com/wanghenshui/cppweeklyn - Yin x2 - YellyHornby x3 - 404 x2 -- Amnesia x5 +- Amnesia x6 - fengyiee木马 - 陈青松 - 黄亮Anthony x13 - Captain - Anien - jerry -- HNY x20 +- HNY x21 - CHENL - 沧海 - 彩虹蛇皮虾 @@ -110,4 +110,5 @@ RSS使用仓库的release RSS [链接](https://github.com/wanghenshui/cppweeklyn - 寻找我的皮卡丘 - 岚岚路 - LH_mouse x 666 -- 终盛 x2 \ No newline at end of file +- 终盛 x2 +- Damon \ No newline at end of file diff --git a/posts/164.md b/posts/164.md index 38ff92a..11bbf65 100644 --- a/posts/164.md +++ b/posts/164.md @@ -316,4 +316,27 @@ int main(void) { --- +C++ 中文周刊 164期补充 + + +昨天更新关于HFT的内容有误 + +- cache warm 效果有限,加热icache缺乏其他优化验证修复,比如pgo,比如调大tlb。当然cache warm对于可以观测数据集预估业务的场景来说,简单粗暴,不过对于优化而言,很难说问题的根因在哪里,PGO应该是最直观的,cache warm给人一种野路子歪打正着的感觉,需要进一步分析。对于不可预估后端场景,cache warm就相当于CPU做无用功了,一定要测试,测试,测试 + +- 其他例子,比如prefetch等,例子粗糙,缺少系统视角,如果缺乏这个知识需要科普,看这个小册子,反而可能造成误导 + + +需要系统了解可以看现代cpu性能分析与优化 有中文版本 + +英文版 https://book.easyperf.net/perf_book + +中文版本可能比较旧,但对于科普系统学习知识也足够,京东77应该是涨价了,我买的时候是50 + +公开课可以学一下mit 6.172 b站有视频 ppt可以这里下载 https://ocw.mit.edu/courses/6-172-performance-engineering-of-software-systems-fall-2018/ + +实际上优化相关知识广,碎,杂,需要系统整体视角 + +本文感谢崔博武 Anien 指正 +--- + [上一期](https://wanghenshui.github.io/cppweeklynews/posts/163.html) diff --git a/posts/16405.md b/posts/16405.md deleted file mode 100644 index 0cf65f5..0000000 --- a/posts/16405.md +++ /dev/null @@ -1,25 +0,0 @@ ---- -layout: post -title: 第164.5期 ---- -# C++ 中文周刊 164期补充 - - -昨天更新关于HFT的内容有误 - -- cache warm 效果有限,加热icache缺乏其他优化验证修复,比如pgo,比如调大tlb。当然cache warm对于可以观测数据集预估业务的场景来说,简单粗暴,不过对于优化而言,很难说问题的根因在哪里,PGO应该是最直观的,cache warm给人一种野路子歪打正着的感觉,需要进一步分析。对于不可预估后端场景,cache warm就相当于CPU做无用功了,一定要测试,测试,测试 - -- 其他例子,比如prefetch等,例子粗糙,缺少系统视角,如果缺乏这个知识需要科普,看这个小册子,反而可能造成误导 - - -需要系统了解可以看现代cpu性能分析与优化 有中文版本 - -英文版 https://book.easyperf.net/perf_book - -中文版本可能比较旧,但对于科普系统学习知识也足够,京东77应该是涨价了,我买的时候是50 - -公开课可以学一下mit 6.172 b站有视频 ppt可以这里下载 https://ocw.mit.edu/courses/6-172-performance-engineering-of-software-systems-fall-2018/ - -实际上优化相关知识广,碎,杂,需要系统整体视角 - -本文感谢崔博武 Anien 指正 \ No newline at end of file diff --git a/posts/165.md b/posts/165.md new file mode 100644 index 0000000..089cd19 --- /dev/null +++ b/posts/165.md @@ -0,0 +1,182 @@ +--- +layout: post +title: 第165期 +--- +# C++ 中文周刊 2024-07-27 第165期 + +[周刊项目地址](https://github.com/wanghenshui/cppweeklynews) + +公众号 + + + +点击「查看原文」跳转到 GitHub 上对应文件,链接就可以点击了 + +qq群 [点击进入](https://qm.qq.com/q/6NGizNPyG4) + +[RSS](https://github.com/wanghenshui/cppweeklynews/releases.atom) + +欢迎投稿,推荐或自荐文章/软件/资源等,评论区留言 + +本期文章由 Amniesia HNY Damon 赞助 + +最近的热门是windows蓝屏事件了,其实国内外安全都有关系户 + +本期内容不多 + +--- + +## 资讯 + +标准委员会动态/ide/编译器信息放在这里 + +[编译器信息最新动态推荐关注hellogcc公众号 本周更新 264期](https://mp.weixin.qq.com/s/U4htZhQop4nH4SG9ErJoYQ) + + +## 文章 + +### [Safer code in C++ with lifetime bounds](https://lemire.me/blog/2024/07/26/safer-code-in-c-with-lifetime-bounds/) + +llvm和msvc支持生命周期检查,返回string_view有概率悬空,用错 + +```cpp +std::string_view my_get_host(std::string_view url_string) { + auto url = ada::parse(url_string).value(); + return url.get_host(); +} +``` + +比如这种用法明显就是错的,加上编译检查能抓出来 + +```cpp +#ifndef __has_cpp_attribute + #define ada_lifetime_bound +#elif __has_cpp_attribute(msvc::lifetimebound) + #define ada_lifetime_bound [[msvc::lifetimebound]] +#elif __has_cpp_attribute(clang::lifetimebound) + #define ada_lifetime_bound [[clang::lifetimebound]] +#elif __has_cpp_attribute(lifetimebound) + #define ada_lifetime_bound [[lifetimebound]] +#else + #define ada_lifetime_bound +#endif + +... + +std::string_view get_host() const noexcept ada_lifetime_bound; +``` + +编译报错 + +```txt +fun.cpp:8:10: warning: address of stack memory associated with local variable 'url' returned [-Wreturn-stack-address] + 8 | return url.get_host(); +``` + +想要了解可以看这里 https://clang.llvm.org/docs/AttributeReference.html#lifetimebound + +### [strlcpy and how CPUs can defy common sense strlcpy and how CPUs can defy common sense](https://nrk.neocities.org/articles/cpu-vs-common-sense) + +strlcpy 实现openbsd和glibc实现不同,openbsd是这样的 + +```c +size_t strlcpy(char *dst, const char *src, size_t dsize) +{ + const char *osrc = src; + size_t nleft = dsize; + + if (nleft != 0) while (--nleft != 0) { /* Copy as many bytes as will fit. */ + if ((*dst++ = *src++) == '\0') + break; + } + + if (nleft == 0) { /* Not enough room in dst, add NUL and traverse rest of src. */ + if (dsize != 0) *dst = '\0'; /* NUL-terminate dst */ + while (*src++) ; + } + + return(src - osrc - 1); /* count does not include NUL */ +} +``` +能看到是一边复制一边移动的,没有提前算出src边界,而glibc是用strlen先计算src长度的,相当于重复计算了 + +所以openbsd版本应该比glibc版本快是不是?并不 + +考虑到strlen和memcpy有可能优化,咱们手写一个版本 + +```c +size_t bespoke_strlcpy(char *dst, const char *src, size_t size) +{ + size_t len = 0; + for (; src[len] != '\0'; ++len) {} // strlen() loop + + if (size > 0) { + size_t to_copy = len < size ? len : size - 1; + for (size_t i = 0; i < to_copy; ++i) // memcpy() loop + dst[i] = src[i]; + dst[to_copy] = '\0'; + } + return len; +} +``` + +编译使用 `-fno-builtin`避免`strlen` `memcpy`优化 + +这个也比openbsd快 + +实际上没有长度信息 每次都要判断`\0`,严重影响优化,循环出现依赖,没法彻底优化 + +### [What's so hard about constexpr allocation?](https://brevzin.github.io/c++/2024/07/24/constexpr-alloc/) + + +讨论constexpr vector难做的原因,先从unique_ptr开始讨论,constexpr导致相关的传递语义发生变化,不好优化 + +考虑引入新关键字propconst 标记常量传递 讨论的还是比较有深度的,感兴趣的可以读一下 + +### [Does C++ allow template specialization by concepts?](https://lemire.me/blog/2024/07/22/does-c-allow-template-specialization-by-concepts/) + +用require实现函数偏特化 + +```cpp +template +void clear(T & t); + + +template +concept not_string = +!std::is_same_v; + + +template <> +void clear(std::string & t) { + t.clear(); +} + + +template +void clear(T& container) requires not_string { + for(auto& i : container) { + i = typename T::value_type{}; + } +} +``` +看一乐 + +### [Scan HTML even faster with SIMD instructions (C++ and C#)](https://lemire.me/blog/2024/07/20/scan-html-even-faster-with-simd-instructions-c-and-c/) + +实现特殊版本find_first_of [向量化。代码不贴了,感兴趣的看一下](https://github.com/lemire/Code-used-on-Daniel-Lemire-s-blog/tree/master/2024/06/08) + + +## 开源项目介绍 + +- [asteria](https://github.com/lhmouse/asteria) 一个脚本语言,可嵌入,长期找人,希望胖友们帮帮忙,也可以加群753302367和作者对线 + +## 互动环节 + +看了死侍金刚狼 还可以。现在漫威太垃圾了,这还算能看的 + +实际剧情和银河护卫队差不多,不能细想反派,看个乐呵 + +--- + +[上一期](https://wanghenshui.github.io/cppweeklynews/posts/164.html)