-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Runtime] String 优化 #171
Comments
StringView 会引入什么 bug |
主要是内存所有权的问题,得限制一下 StringView 的使用场景: 举个例子,手写 Json 序列化: Archive ar;
const char var[] = "fuck";
ar.write("fuck", f);
ar.write("shit", s);
ar.write(ar.hold(var), v); 其实这里用 string 比较合适,但不舍得性能,主要还是期望使用常量优化来改善这类场景 |
这个 hold 起了什么作用,不调用 hold 会有什么问题呢 |
hold 是为了让串跟着 ar 的生命周期跑,这个函数调用本身没法假设 ar 的生命周期。 其实主要是涉及 API 期望持有串,还是单纯的期望信息获取,常量的考虑在这里被排除了,因为 String 与 StringView 对常量的处理都是低成本的 |
意思是在单纯传参的情况下 View 是没什么问题的,但是当涉及到储存的时候(包括闭包捕获)就得进行生命周期同步,在这个时候一般要提升到 String,如果漏掉了这一步就容易野指针? |
yes,主要问题就出在传统 String 缺少对常量的优化,导致性能和安全之间没法进行良好的权衡,如果进行常量优化可以很好的解决这个问题 |
String 类型
关于 StringView
StringView 用于统一常量字符串和 String 对象的差异,但是它不持有具体的内存,容易引入 Bug,并且在某些希望转让内存的情况下会增加开销,因此考虑使用 String 常量优化来替代 StringView 的需求
关于 c-style string 兼容性
c-style string 强制要求以
\0
结尾,在 C++ 代码中,由于 String/StringView 的引入,代码尽量不以来 c-style string常量优化
https://www.youtube.com/watch?v=fglXeSWGVDc
The text was updated successfully, but these errors were encountered: