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

more conversions: StringView from StringView, Cstring from StringView #22

Closed
wants to merge 2 commits into from
Closed

Conversation

aplavin
Copy link

@aplavin aplavin commented Aug 1, 2023

No description provided.

@@ -58,6 +59,7 @@ Base.unsafe_convert(::Type{Ptr{UInt8}}, s::DenseStringViewAndSub) = pointer(s)
Base.unsafe_convert(::Type{Ptr{Int8}}, s::DenseStringViewAndSub) = convert(Ptr{Int8}, pointer(s))
Base.cconvert(::Type{Ptr{UInt8}}, s::DenseStringViewAndSub) = s
Base.cconvert(::Type{Ptr{Int8}}, s::DenseStringViewAndSub) = s
Base.cconvert(::Type{Cstring}, s::DenseStringViewAndSub) = s.data
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not simply s?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH, I'm not that familiar with their interplay. cconvert is for C interop, that's why I define it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, now I also think it shouldn't be added because Cstring has to be nul-terminated. Will remove it, sorry for the noise!

Copy link
Member

@stevengj stevengj Aug 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, probably we need to stick with the default conversion here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand how exactly, but it works correctly:

julia> sv = StringView([0x61, 0x62, 0x63])
"abc"

julia> @ccall strlen(sv::Cstring)::Csize_t
0x0000000000000003

And as expected, much more efficient than converting to String:

julia> sv = StringView(codeunits("abc"^1000))

julia> @btime @ccall strlen($sv::Cstring)::Csize_t
  1.437 μs (2 allocations: 3.12 KiB)  # without this PR
  38.424 ns (0 allocations: 0 bytes)  # with this PR
0x0000000000000bb8  # result is the same

Both of my PRs are motivated by enabling efficient C interop when strings are involved. StringViews seems a great fit for this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, sorry, I misread your original question

Why not simply s?

Thought you are asking about convert vs cconvert.
Not just s because ccall does unsafe_convert(Cstring, x) then. It knows how to unsafe_convert Vector{UInt8} already, and that's what we return from cconvert here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m not convinced it is safe to assume that the data here is NUL-terminated. Have to go back and check what guarantees Julia provides

@stevengj
Copy link
Member

stevengj commented Aug 1, 2023

Add a couple of tests for the new methods?

@codecov-commenter
Copy link

Codecov Report

Merging #22 (ffee45c) into main (7c7f0b6) will increase coverage by 1.76%.
Report is 23 commits behind head on main.
The diff coverage is 82.29%.

❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more.

@@            Coverage Diff             @@
##             main      #22      +/-   ##
==========================================
+ Coverage   75.05%   76.82%   +1.76%     
==========================================
  Files           6        6              
  Lines         417      466      +49     
==========================================
+ Hits          313      358      +45     
- Misses        104      108       +4     
Flag Coverage Δ
unittests 76.82% <82.29%> (+1.76%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Changed Coverage Δ
src/search.jl 51.94% <30.00%> (+2.67%) ⬆️
src/StringViews.jl 94.02% <55.55%> (-2.80%) ⬇️
src/util.jl 88.33% <91.89%> (+4.69%) ⬆️
src/regex.jl 89.00% <92.10%> (+2.23%) ⬆️
src/decoding.jl 86.36% <100.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@aplavin
Copy link
Author

aplavin commented Aug 16, 2023

bump!

@aplavin aplavin closed this by deleting the head repository Aug 17, 2023
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