Skip to content

Use Go strings instead of C.CString #252

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

Merged
merged 2 commits into from
Feb 21, 2025
Merged

Use Go strings instead of C.CString #252

merged 2 commits into from
Feb 21, 2025

Conversation

qmuntal
Copy link
Collaborator

@qmuntal qmuntal commented Feb 20, 2025

We can define all C strings passed from Go to C as good all Go constant strings casted on demand using unsafe.StringData, which is almost no-op.

My main motivation here is to eliminate all C.CString calls, as they have some small performance penalty (2 cgo calls, one for C.CString itself and the other for the companion C.free) and it is easy to forget calling C.free after.

A nice side-effect is that now string global variables can be statically initialized by the linker thanks to them being constant strings. This will result in faster initialization times.

@qmuntal qmuntal merged commit 136f75d into v2 Feb 21, 2025
50 checks passed
@qmuntal qmuntal deleted the nocstring branch February 21, 2025 09:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants