前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++ 中文周刊 2024-03-25 第152期

C++ 中文周刊 2024-03-25 第152期

作者头像
王很水
发布2024-07-30 15:03:39
810
发布2024-07-30 15:03:39
举报
文章被收录于专栏:C++ 动态新闻推送

欢迎投稿,推荐或自荐文章/软件/资源等,评论区留言

资讯

标准委员会动态/ide/编译器信息放在这里

编译器信息最新动态推荐关注hellogcc公众号 本周更新 2024-03-13 第245期

c++26 东京会议如火如荼,详情Mick235711已经发了,公众号也发了,这里不再赘述

文章

Introduction To Low Latency Programming: External Processing https://tech.davidgorski.ca/introduction-to-low-latency-programming-external-processing/

其实就是提前算好,包括不限于利用编译期 利用脚本生成以及constexpr

异步拆分,让别人算

Condvars and atomics do not mix https://zeux.io/2024/03/23/condvars-atomic/

用condvar 条件就要放到mutex下修改,即便这个变量是atomic,也要放到mutex下修改

Jumbled Protocol Buffer Message Layout https://eason-zhan.github.io/posts/jumbled-protocol-buffer-message-layout/

protoc会重排你定义的字段布局

代码语言:javascript
复制
static void OptimizeLayoutHelper(std::vector<const FieldDescriptor*>* fields,
                                 const Options& options,
                                 MessageSCCAnalyzer* scc_analyzer) {
  if (fields->empty()) return;

  // The sorted numeric order of Family determines the declaration order in the
  // memory layout.
  enum Family {
    REPEATED = 0,
    STRING = 1,
    // Laying out LAZY_MESSAGE before MESSAGE allows a single memset to zero
    // MESSAGE and ZERO_INITIALIZABLE fields together.
    LAZY_MESSAGE = 2,
    MESSAGE = 3,
    ZERO_INITIALIZABLE = 4,
    OTHER = 5,
    kMaxFamily
  };

作者观察到一个现象,本来字段很多,删掉一部份字段,性能应该有提升,结果并没有

代码语言:javascript
复制
message Stats {
    int64 ts                   = 1;
    int64 show                 = 2;
    int64 click                = 3;
    int64 cost                 = 4;
    int64 hour_show            = 5;
    int64 hour_click           = 6;
    int64 hour_cost            = 7;
    int64 acc_show             = 8;
    int64 acc_click            = 9;
    int64 acc_cost             = 10;
    repeated int64 bucket      = 11;
    repeated int64 hour_bucket = 12;
    repeated int64 acc_bucket  = 13;
}

优化成这样

代码语言:javascript
复制
// After remove the `hour_*` fields
message StatsOpt {
    int64 ts                   = 1;
    int64 show                 = 2;
    int64 click                = 3;
    int64 cost                 = 4;
    int64 acc_show             = 5;
    int64 acc_click            = 6;
    int64 acc_cost             = 7;
    repeated int64 bucket      = 8;
    repeated int64 acc_bucket  = 9;
}

内存布局原来是这样

代码语言:javascript
复制
+------ 16 BYTE ------+- 8 BYTE -+------ 16 BYTE ------+- 8 BYTE -+------ 16 BYTE ------+
|    (11)bucket       | (11)size |      (12)hour       | (12)size |  (13)acc_bucket     |
+- 8 BYTE -+- 8 BYTE -+- 8 BYTE -+- 8 BYTE -+- 8 BYTE -+- 8 BYTE -+- 8 BYTE -+- 8 BYTE -+
| (13.a)   |   (1)ts  |  (2)show | (3)click |  (4)cost |    (5)   |    (6)   | (7)      |
+- 8 BYTE -+- 8 BYTE -+- 8 BYTE -+- 8 BYTE -+- 8 BYTE -+- 8 BYTE -+- 8 BYTE -+- 8 BYTE -+
|    (8)   |    (9)   |   (10)   |     *    |     *    |     *    |     *    |     *    |
+- 8 BYTE -+- 8 BYTE -+- 8 BYTE -+- 8 BYTE -+- 8 BYTE -+- 8 BYTE -+- 8 BYTE -+- 8 BYTE -+

现在是这样

代码语言:javascript
复制
+------ 16 BYTE ------+- 8 BYTE -+------ 16 BYTE ------+- 8 BYTE -+- 8 BYTE -+- 8 BYTE -+
|    (8)bucket        | (8)size  |      (9)hour        | (9)size  | (13.a)   |   (1)ts  |
+- 8 BYTE -+- 8 BYTE -+- 8 BYTE -+- 8 BYTE -+- 8 BYTE -+- 8 BYTE -+- 8 BYTE -+- 8 BYTE -+
|  (2)show | (3)click |  (4)cost |    (5)   |    (6)   | (7)      |     *    |     *    |
+- 8 BYTE -+- 8 BYTE -+- 8 BYTE -+- 8 BYTE -+- 8 BYTE -+- 8 BYTE -+- 8 BYTE -+- 8 BYTE -+

生成文件是这样的

代码语言:javascript
复制
 struct Impl_ {
  ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t > bucket_;
  mutable std::atomic<int> _bucket_cached_byte_size_;
  ::PROTOBUF_NAMESPACE_ID::RepeatedField< int64_t > acc_bucket_;
  mutable std::atomic<int> _acc_bucket_cached_byte_size_;
  int64_t ts_;
  int64_t show_;
  int64_t click_;
  int64_t cost_;
  int64_t acc_show_;
  int64_t acc_click_;
  int64_t acc_cost_;
  mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_;
};

union { Impl_ _impl_; };

能看到ts和cost跨cacheline了。以前的字段虽然大,但不是夸cacheline的,优化后反而跨cacheline导致变慢

C++ exit-time destructors https://maskray.me/blog/2024-03-17-c++-exit-time-destructors

介绍析构 runtime细节,非常细,值得一看

Daily bit(e) of C++ | Coroutines: step by step https://simontoth.substack.com/p/daily-bite-of-c-coroutines-step-by

又一个协程教程

C++23: Encoding related changes https://www.sandordargo.com/blog/2024/03/20/cpp23-encoding-related-changes

介绍了一些编码方面的改进,包括多语言支持/unicode等等

代码语言:javascript
复制

std::locale::global(std::locale("Russian.1251"));
auto s = std::format("День недели: {}", std::chrono::Monday);

Bug hunting in Btrfs https://tavianator.com/2024/btrfs_bug.html

调试代码发现了btrfs有bug,然后去找bug在哪里,很细,值得一看,这里标记TODO

C++20: Basic Chrono Terminology with Time Duration and Time Point https://www.modernescpp.com/index.php/c20-basic-chrono-terminology-with-time-duration-and-time-point/

std::chrono::months 你猜是多少?30.436875

std::chrono::years 你猜是多少?365.2425

非常之令人无语,这种傻逼接口有存在的必要吗

Two handy GDB breakpoint tricks https://nullprogram.com/blog/2024/01/28/

gdb小技巧

Daily bit(e) of C++ | Coroutines: step by step https://simontoth.substack.com/p/daily-bite-of-c-coroutines-step-by

coroutine介绍,代码很长

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2024-03-25,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 CPP每周推送 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 资讯
  • 文章
    • Introduction To Low Latency Programming: External Processing https://tech.davidgorski.ca/introduction-to-low-latency-programming-external-processing/
      • Condvars and atomics do not mix https://zeux.io/2024/03/23/condvars-atomic/
        • Jumbled Protocol Buffer Message Layout https://eason-zhan.github.io/posts/jumbled-protocol-buffer-message-layout/
          • C++ exit-time destructors https://maskray.me/blog/2024-03-17-c++-exit-time-destructors
            • Daily bit(e) of C++ | Coroutines: step by step https://simontoth.substack.com/p/daily-bite-of-c-coroutines-step-by
              • C++23: Encoding related changes https://www.sandordargo.com/blog/2024/03/20/cpp23-encoding-related-changes
                • Bug hunting in Btrfs https://tavianator.com/2024/btrfs_bug.html
                  • C++20: Basic Chrono Terminology with Time Duration and Time Point https://www.modernescpp.com/index.php/c20-basic-chrono-terminology-with-time-duration-and-time-point/
                    • Two handy GDB breakpoint tricks https://nullprogram.com/blog/2024/01/28/
                      • Daily bit(e) of C++ | Coroutines: step by step https://simontoth.substack.com/p/daily-bite-of-c-coroutines-step-by
                      领券
                      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档