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

Define getindex(::InlineString, range) to return InlineString #44

Merged
merged 9 commits into from
Oct 4, 2022

Conversation

nickrobinson251
Copy link
Collaborator

@nickrobinson251 nickrobinson251 commented Oct 3, 2022

This is another case where we could be keeping things as InlineString rather than returning a SubString{InlineString} (like chop, chomp, strip etc) (xref #5)

...but i don't love the (worse than linear) slowdown i'm seeing for larger InlineString types

I guess it makes sense since we're doing more work to construct a new InlineString (two nextind calls and then several bit-shifts) than the work involved in creating a SubString (which is basically a single nextind call)?

Micro-benchmark

      for S in (
           InlineString3, InlineString7, InlineString15, InlineString31,
           InlineString63, InlineString127, InlineString255,
       )
           str = S(repeat("x", sizeof(S)-1))
           ix = (firstindex(str) + 1):(lastindex(str) - 1)
           @btime $str[$ix]
       end

On main (returning SubString)

  14.194 ns (0 allocations: 0 bytes)
  11.720 ns (0 allocations: 0 bytes)
  16.658 ns (0 allocations: 0 bytes)
  18.681 ns (0 allocations: 0 bytes)
  29.732 ns (0 allocations: 0 bytes)
  57.562 ns (0 allocations: 0 bytes)
  109.481 ns (0 allocations: 0 bytes)

On this branch (returning InlineString)

  10.218 ns (0 allocations: 0 bytes)
  12.930 ns (0 allocations: 0 bytes)
  14.570 ns (0 allocations: 0 bytes)
  29.313 ns (0 allocations: 0 bytes)
  98.969 ns (0 allocations: 0 bytes)
  328.744 ns (0 allocations: 0 bytes)
  1.283 μs (0 allocations: 0 bytes)

There's also getindex(str, vec) that allows selecting non-contiguous elements... so maybe if we add this we should add that too? (currently returns SubString)

@nickrobinson251 nickrobinson251 marked this pull request as ready for review October 3, 2022 22:25
Copy link
Member

@quinnj quinnj left a comment

Choose a reason for hiding this comment

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

This looks great; thanks @nickrobinson251! The test failure is just Julia 1.3 being really old and having some other ambiguous range error. I'm happy to bump up to Julia 1.6 and we can do a new minor release.

@nickrobinson251 nickrobinson251 changed the title RFC: Define getindex(::InlineString, range) to return InlineString Define getindex(::InlineString, range) to return InlineString Oct 4, 2022
@nickrobinson251
Copy link
Collaborator Author

oooft, looks like v1.6 has the same ambiguity error as 1.3 😞 I think i'd rather us support 1.6 if we can, so i'll probably just oush a fix

- Before on Julia v1.6:
  MethodError: getindex(::String255, ::UnitRange{Int64}) is ambiguous. Candidates:
    getindex(s::AbstractString, r::UnitRange{var"#s77"} where var"#s77"<:Integer) in Base at strings/substring.jl:255
    getindex(s::InlineString, r::AbstractUnitRange{var"#s17"} where var"#s17"<:Integer) in InlineStrings at src/InlineStrings.jl:359
@nickrobinson251 nickrobinson251 merged commit 27955ec into main Oct 4, 2022
@nickrobinson251 nickrobinson251 deleted the npr/getindex-range branch October 4, 2022 10:57
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.

None yet

2 participants