TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Show HN: SSO – Small String Optimization

4 点作者 feelamee7 个月前
I wrote this just for fun when saw article about sso in Rust[1]. My string can store up to 23 (excluding null-terminator) 8-bit chars without calling allocator.<p>Here I can mistake, but.. Curious fact: both - libstdc++[2] and libc++[3] - do access to union member without any check that it is active now. AFAIK, this is UB in C++. But I assume that they just rely on theirs compiler features. I tried to avoid this using `std::byte[]`. But I&#x27;m still sure that there are several UB&#x27;s in my code :)<p>[1] <a href="https:&#x2F;&#x2F;tunglevo.com&#x2F;note&#x2F;an-optimization-thats-impossible-in-rust&#x2F;" rel="nofollow">https:&#x2F;&#x2F;tunglevo.com&#x2F;note&#x2F;an-optimization-thats-impossible-i...</a><p>[2] <a href="https:&#x2F;&#x2F;github.com&#x2F;gcc-mirror&#x2F;gcc&#x2F;blob&#x2F;d09131eea083e80ccad60cc2686c09e9fdae0188&#x2F;libstdc%2B%2B-v3&#x2F;include&#x2F;bits&#x2F;basic_string.h#L269">https:&#x2F;&#x2F;github.com&#x2F;gcc-mirror&#x2F;gcc&#x2F;blob&#x2F;d09131eea083e80ccad60...</a><p>[3] <a href="https:&#x2F;&#x2F;github.com&#x2F;llvm&#x2F;llvm-project&#x2F;blob&#x2F;4468d58080d0502a050b71a33413d5206ad5e8fd&#x2F;libcxx&#x2F;include&#x2F;string#L1880">https:&#x2F;&#x2F;github.com&#x2F;llvm&#x2F;llvm-project&#x2F;blob&#x2F;4468d58080d0502a05...</a>

2 条评论

quuxplusone7 个月前
&gt; Curious fact: both - libstdc++ and libc++ - do access to union member without any check that it is active now.<p>Accessing a data member that&#x27;s within the common initial sequence[1] of both union alternatives is perfectly well-defined.[2]<p>However, it&#x27;s true that in this case (I&#x27;m looking at libc++) the member isn&#x27;t quite the same in both alternatives: In one case it&#x27;s a `char:1` and in the other case a `size_t:1`. Also, in both cases it&#x27;s nested inside an anonymous `struct __attribute__((packed))`, which means we&#x27;re dealing with two different compiler extensions already. (Standard C++ supports anonymous unions,[3] but not anonymous structs.) So yes, pedantically speaking, they&#x27;re relying on the compiler&#x27;s behavior.<p>&gt; I tried to avoid this using `std::byte[]`<p>I don&#x27;t know about Rust, but in C++ you probably wouldn&#x27;t be able to type-pun `std::byte[]` in all the ways you&#x27;d need to during constant evaluation (i.e., at constexpr time). C++20-and-later require `std::string` to be constexpr-friendly. So that&#x27;s probably relevant to the library vendors&#x27; choices here.<p>[1] <a href="https:&#x2F;&#x2F;eel.is&#x2F;c++draft&#x2F;class.mem#def:common_initial_sequence" rel="nofollow">https:&#x2F;&#x2F;eel.is&#x2F;c++draft&#x2F;class.mem#def:common_initial_sequenc...</a><p>[2] <a href="https:&#x2F;&#x2F;eel.is&#x2F;c++draft&#x2F;class.mem#general-28" rel="nofollow">https:&#x2F;&#x2F;eel.is&#x2F;c++draft&#x2F;class.mem#general-28</a><p>[3] <a href="https:&#x2F;&#x2F;eel.is&#x2F;c++draft&#x2F;class.union.anon" rel="nofollow">https:&#x2F;&#x2F;eel.is&#x2F;c++draft&#x2F;class.union.anon</a>
评论 #41909467 未加载
breck7 个月前
Interesting. The writing is a little unclear, but I enjoyed nonetheless!<p>Here&#x27;s my user test:<p><a href="https:&#x2F;&#x2F;news.pub&#x2F;?try=https:&#x2F;&#x2F;www.youtube.com&#x2F;embed&#x2F;tQXoCbUhWOQ?si=Zc-Uo4pF6QaX2hyV" rel="nofollow">https:&#x2F;&#x2F;news.pub&#x2F;?try=https:&#x2F;&#x2F;www.youtube.com&#x2F;embed&#x2F;tQXoCbUh...</a>
评论 #41826612 未加载