A lot of it comes down to the tradeoff among costs. As you say, creating a String
from a SubString{String}
does have a cost:
julia> str = SubString("hello", 2:4)
"ell"
julia> using BenchmarkTools
julia> @btime String($str)
11.030 ns (1 allocation: 32 bytes)
"ell"
If that’s in the inner loop of your program, you might come to hate that 11ns—you’d be better off if you didn’t need to convert.
In contrast, suppose you have
function somebigfunction(filename::AbstractString)
# this takes 300ms to compile and 1ms to execute
end
In terms of runtime performance, if somebigfunction
takes 1ms to execute, you’ll never notice the 11ns it would take to convert the filename
to a String. So from a runtime performance standpoint, it’s completely, utterly irrelevant whether you convert to a String
before calling it. But you might notice paying 300ms each time you compile it for:
filename::String
filename::SubString{String}
-
filename::AbstractString
(for those cases where you’re calling this with poor inferrability offilename
)
If you have to compile it for all 3 types, you might have to wait a full second, whereas it’s only a third of a second if you could get away with compiling it only once.
A way to standardize on filename::String
and ensure that you never pay that 300ms for any other type is to define your methods like this:
function somebigfunction(filename::String)
# this takes 300ms to compile and 1ms to execute
end
somebigfunction(filename::AbstractString) = somebigfunction(String(filename)::String)
Now, you can’t always do this: part of Julia’s power is the ability to write generic functions that can handle “anything.” But you don’t always need that power, and that power comes with the cost of pretty bad latencies. Fortunately, Julia is flexible enough that you don’t have to use that power if you don’t want to.
As is hopefully clear, these are not black vs white issues: it requires understanding where the costs of a package are coming from, and you might do one thing in one circumstance and the exact opposite in a different circumstance. It comes down to understanding the tradeoffs in each case.