Skip to content

Commit

Permalink
165 (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
wanghenshui authored Jul 28, 2024
1 parent 6597de8 commit 1d83217
Show file tree
Hide file tree
Showing 4 changed files with 210 additions and 29 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
Expand Down Expand Up @@ -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
- 沧海
- 彩虹蛇皮虾
Expand All @@ -110,4 +110,5 @@ RSS使用仓库的release RSS [链接](https://github.com/wanghenshui/cppweeklyn
- 寻找我的皮卡丘
- 岚岚路
- LH_mouse x 666
- 终盛 x2
- 终盛 x2
- Damon
23 changes: 23 additions & 0 deletions posts/164.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
25 changes: 0 additions & 25 deletions posts/16405.md

This file was deleted.

182 changes: 182 additions & 0 deletions posts/165.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
---
layout: post
title: 第165期
---
# C++ 中文周刊 2024-07-27 第165期

[周刊项目地址](https://github.com/wanghenshui/cppweeklynews)

公众号

<img src="https://wanghenshui.github.io/cppweeklynews/assets/code.png" alt="" width="30%">

点击「查看原文」跳转到 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 <typename T>
void clear(T & t);


template <typename T>
concept not_string =
!std::is_same_v<T, std::string>;


template <>
void clear(std::string & t) {
t.clear();
}


template <class T>
void clear(T& container) requires not_string<T> {
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)

0 comments on commit 1d83217

Please sign in to comment.