Skip to content
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

Open
zhurongjun opened this issue Jan 15, 2024 · 6 comments
Open

[Runtime] String 优化 #171

zhurongjun opened this issue Jan 15, 2024 · 6 comments
Assignees
Labels
enhancement New feature or request feature New engine feature

Comments

@zhurongjun
Copy link
Contributor

String 类型

  • String:平时流通的 SSO String
  • Name:本质是一个 ID,用于快速索引,构造代价略大于 String
  • CowString:用于 GUI 等多地引用的 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

@zhurongjun zhurongjun self-assigned this Jan 15, 2024
@zhurongjun zhurongjun added enhancement New feature or request feature New engine feature labels Jan 15, 2024
@BenzzzX
Copy link
Contributor

BenzzzX commented Jan 16, 2024

StringView 会引入什么 bug

@zhurongjun
Copy link
Contributor Author

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 比较合适,但不舍得性能,主要还是期望使用常量优化来改善这类场景

@BenzzzX
Copy link
Contributor

BenzzzX commented Jan 17, 2024

这个 hold 起了什么作用,不调用 hold 会有什么问题呢

@zhurongjun
Copy link
Contributor Author

这个 hold 起了什么作用,不调用 hold 会有什么问题呢

hold 是为了让串跟着 ar 的生命周期跑,这个函数调用本身没法假设 ar 的生命周期。

其实主要是涉及 API 期望持有串,还是单纯的期望信息获取,常量的考虑在这里被排除了,因为 String 与 StringView 对常量的处理都是低成本的

@BenzzzX
Copy link
Contributor

BenzzzX commented Jan 17, 2024

这个 hold 起了什么作用,不调用 hold 会有什么问题呢

hold 是为了让串跟着 ar 的生命周期跑,这个函数调用本身没法假设 ar 的生命周期。

其实主要是涉及 API 期望持有串,还是单纯的期望信息获取,常量的考虑在这里被排除了,因为 String 与 StringView 对常量的处理都是低成本的

意思是在单纯传参的情况下 View 是没什么问题的,但是当涉及到储存的时候(包括闭包捕获)就得进行生命周期同步,在这个时候一般要提升到 String,如果漏掉了这一步就容易野指针?

@zhurongjun
Copy link
Contributor Author

这个 hold 起了什么作用,不调用 hold 会有什么问题呢

hold 是为了让串跟着 ar 的生命周期跑,这个函数调用本身没法假设 ar 的生命周期。
其实主要是涉及 API 期望持有串,还是单纯的期望信息获取,常量的考虑在这里被排除了,因为 String 与 StringView 对常量的处理都是低成本的

意思是在单纯传参的情况下 View 是没什么问题的,但是当涉及到储存的时候(包括闭包捕获)就得进行生命周期同步,在这个时候一般要提升到 String,如果漏掉了这一步就容易野指针?

yes,主要问题就出在传统 String 缺少对常量的优化,导致性能和安全之间没法进行良好的权衡,如果进行常量优化可以很好的解决这个问题

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request feature New engine feature
Projects
None yet
Development

No branches or pull requests

2 participants