前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++ 动态新闻推送 第9期

C++ 动态新闻推送 第9期

作者头像
王很水
发布2021-08-31 17:39:46
4290
发布2021-08-31 17:39:46
举报

C++ 动态新闻推送 第9期

reddit/hackernews/lobsters/meetingcpp摘抄一些c++动态。

每周更新

周刊项目地址 github,在线地址

discord讨论群组 |飞书讨论群组|知乎专栏

欢迎投稿,推荐或自荐文章/软件/资源等,请提交 issue


资讯

编译器信息最新动态推荐关注hellogcc公众号

本周周报github直达

文章

讨论append的各种场景,以及其中的拷贝策略

c++ tip of week 222 Did you know that C++23 added contains function to string_view?? 这里

int main() {
  using std::literals::string_view_literals::operator""sv;
  std::cout << "trade.price"sv.contains("order"); // prints 0
  std::cout << "trade.price"sv.contains("");      // prints 1
  std::cout << "trade.price"sv.contains("price"); // prints 1
}

c++中函数返回的过程是什么样的?

讨论了很多场景,想要了解的话可以看下

非常简单的实现,用mutex,代码看这里https://github.com/mvorbrodt/blog/blob/master/src/lesson_queue_how_to.cpp

lambda语句比较啰嗦,这里设计了一个语法,把lamdba改写的简单一些 代码在这里 https://github.com/Quincunx271/TerseLambda

std::sort(my_vector.begin(), my_vector.end(),
    [](std::string const& lhs, std::string const& rhs) {
        return lhs.size() < rhs.size();
    });

std::sort(my_vector.begin(), my_vector.end(),
    [] TL(_1.size() < _2.size());
    });

思路, 绑定参数到placeholder上,利用宏展开来绑定

如何绑定?用模版来转发arg,arg是变参,利用抓发绑定到具体的placeholder上,nth_arg<N> 匹配具体的placeholder

clang最近有个新的优化点 tailcall (尾递归调用优化) 实现在这里 不过其他的编译器没实现这个,也就是说,不可移植

作者写的upb是一个pb的parser,为了用上tailcall优化,手写了一套优化的代码,然后编译器新增了这个优化,原来写的代码就可以不用了

这里讨论了写tailcall代码的技巧 代码提交在这里 https://github.com/protocolbuffers/upb/pull/310

针对clang,就用编译器提供的优化 https://github.com/protocolbuffers/upb/pull/390

主要技术就是这段代码

#include <stdint.h>
#include <stddef.h>
#include <string.h>

typedef void *upb_msg;
struct upb_decstate;
typedef struct upb_decstate upb_decstate;

// The standard set of arguments passed to each parsing function.
// Thanks to x86-64 calling conventions, these will be passed in registers.
#define UPB_PARSE_PARAMS                                          \
  upb_decstate *d, const char *ptr, upb_msg *msg, intptr_t table, \
      uint64_t hasbits, uint64_t data
#define UPB_PARSE_ARGS d, ptr, msg, table, hasbits, data

#define UNLIKELY(x) __builtin_expect(x, 0)
#define MUSTTAIL __attribute__((musttail))

const char *fallback(UPB_PARSE_PARAMS);
const char *dispatch(UPB_PARSE_PARAMS);

// Code to parse a 4-byte fixed field that uses a 1-byte tag (field 1-15).
const char *upb_pf32_1bt(UPB_PARSE_PARAMS) {
  // Decode "data", which contains information about this field.
  uint8_t hasbit_index = data >> 24;
  size_t ofs = data >> 48;

  if (UNLIKELY(data & 0xff)) {
    // Wire type mismatch (the dispatch function xor's the expected wire type
    // with the actual wire type, so data & 0xff == 0 indicates a match).
    MUSTTAIL return fallback(UPB_PARSE_ARGS);
  }

  ptr += 1;  // Advance past tag.

  // Store data to message.
  hasbits |= 1ull << hasbit_index;
  memcpy((char*)msg + ofs, ptr, 4);

  ptr += 4;  // Advance past data.

  // Call dispatch function, which will read the next tag and branch to the
  // correct field parser function.
  MUSTTAIL return dispatch(UPB_PARSE_ARGS);
}

生成的汇编

upb_pf32_1bt:                           # @upb_pf32_1bt
        mov     rax, r9
        shr     rax, 24
        bts     r8, rax
        test    r9b, r9b
        jne     .LBB0_1
        mov     r10, r9
        shr     r10, 48
        mov     eax, dword ptr [rsi + 1]
        mov     dword ptr [rdx + r10], eax
        add     rsi, 5
        jmp     dispatch                        # TAILCALL
.LBB0_1:
        jmp     fallback                        # TAILCALL

可以https://godbolt.org/z/K8Mo6hcGa 在godbolt上看一看

注意return fallback,如果没有return,这里就不会优化,解决方法,使用 __attribute__((preserve_most))

TODO: 这个文章是UE相关的,不太了解

视频

国外up主也水视频啊,列举一些网址

Links: https://www.reddit.com/r/cpp/ https://isocpp.org/ https://leanpub.com/bookstore?type=all&category=c_and_cpp https://cppinsights.io/ https://wg21.link/ https://eel.is/c++draft/ https://compiler-explorer.com/ - https://godbolt.org/ - https://gcc.godbolt.org/ https://en.cppreference.com/w/ Blogs https://www.fluentcpp.com/ https://www.cppstories.com/ https://herbsutter.com/ https://randomascii.wordpress.com/ https://www.modernescpp.com/ https://devblogs.microsoft.com/oldnewthing/ YouTube https://www.youtube.com/user/TheChernoProject https://www.youtube.com/user/CppCon https://www.youtube.com/c/Bisqwit/videos https://www.youtube.com/channel/UC-yuWVUplUJZvieEligKBkA - one lone coder https://www.youtube.com/c/MeetingCPP/videos

介绍c++insight的使用以及局限。你只要知道他能把c++展开的让人容易理解就行了。原理没有涉及

讲color的表示以及各种库相关的实现


看到这里或许你有建议或者疑问或者指出错误,请留言评论! 多谢! 你的评论非常重要!也可以帮忙点赞收藏转发!多谢支持!

本文永久链接

     This site is open source. [Improve this page](https://github.com/wanghenshui/cppweeklynews/edit/dev/posts/009.md).
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • C++ 动态新闻推送 第9期
    • 资讯
      • 编译器信息最新动态推荐关注hellogcc公众号
    • 文章
      • 视频
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档