Comments (13)
copystring
sounds like a string is being copied which is confusing.
from julia.
One solution could be to make take!
and read
on IOBuffer
not use Base.StringVector
, but allocate a normal vector. This will make using IOBuffer
for creating strings less efficient, but that is addressed by the new takestring!
function in #54372.
I'm willing to pitch a PR if that's an acceptable solution.
from julia.
Here is an example where using the String
constructor on a Vector{UInt8}
can result in mutating a String
io = IOBuffer()
write(io, [0x61,0x62,0x63])
seekstart(io)
a = read(io, 3)
b = reshape(a, 1, 3)
c = reshape(b, 3)
s = String(a)
@show s
c[1] = 0x62
@show s
This changes s
from "abc"
to "bbc"
This happens on Julia 1.6 as well as nightly.
from julia.
Maybe bytes2string
is a better name?
from julia.
Python calls this function bytes.decode
.
bytes2string
is clear, which is the most important factor imo.
2
names in Base: bytes2hex
, deg2rad
, hex2bytes
, hex2bytes!
, rad2deg
.
to
names in Base: htol
, hton
, ltoh
, ntoh
.
It should maybe take a Type{AbstractString}
argument so it can support Strs.jl etc.
from julia.
There is already a transcode(::Type{String}, src)
function.
Line 179 in d01d256
Maybe this can be changed to avoid mutating the input.
from julia.
I think that is a good solution.
from julia.
This will make using IOBuffer for creating strings less efficient,
That seems completely unacceptable? Isn't the whole reason not to make this slow pretty much the reason for the current behavior of String
?
from julia.
@KristofferC the point is that with this change takestring!(iobuffer)
will be as efficient as String(take!(iobuffer))
is now, while String(take!(iobuffer))
will have a copy. The reason for this change is that a single takestring!
fully encapsulates the sketchy part where an immutable String
is created from mutable data without a copy, while our current idiom for this exposes the mutable object to the user which creates the possibility of the user accidentally holding on to a reference of the mutable data.
With this change in combination with #54372, existing users will see a regression that is fixed by replacing instances of String(take!(iobuffer))
with takestring!(iobuffer)
(we will likely make a Compat PR to make takestring!
available in older julia versions).
from julia.
he point is that with this change takestring!(iobuffer) will be as efficient as String(take!(iobuffer)) is now, while String(take!(iobuffer)) will have a copy.
The whole reason we didn't do this X years ago was that adding this type of perf regression to code run everywhere was not acceptable. I don't see why it would be now.
from julia.
This is the comment I was thinking about: #24388 (comment)
from julia.
Those who don't know history are destined to repeat it.
Reading through #24388, #25241, #25846, #26093 is probably a good idea for people interested in this to know a bit about the discussing and history that has lead to the current behavior.
from julia.
String(copy(v))
still makes two copies as noted in #26093 (comment) In ZipArchives.jl I would like to avoid the extra copy. This is currently possible with unsafe_string
, so an extra function in Base is not needed, but IMO it would be nice to avoid having to deal with pointers.
from julia.
Related Issues (20)
- SEO problem with Julia documentation? HOT 3
- 'make test' fails for julia-1.11.0-beta2 HOT 1
- `MethodError: no method matching active_module()` (leading to world age error), on Julia 1.11 HOT 2
- bugs involving `IdentityUnitRange` HOT 2
- `getindex` and `isassigned` with big `BigInt` ranges and trailing 1 indices errors HOT 2
- `const` before destructuring is inconsistent
- Source build of 1.12.0 on macOS ARM: failure of Pkg repl prompt after `3fc35778cc` HOT 2
- Add 2-arg `include` `mapexpr` to cache header?
- Feature Request: Add Syntactic Sugar for Compile-Time Constants in Function Definitions HOT 4
- Problem with `isfile_casesensitive()` on Windows HOT 1
- exit_current_timer implementation is unsound HOT 1
- export statement lowering correctness, causing regression HOT 5
- sleep_state not_sleeping is not reset as required by signals
- [Test] need shim for FieldError HOT 1
- Julia builds for `native` CPU, even with `XC_HOST = x86_64-w64-mingw32` HOT 2
- Performance regressions in linear algebra benchmarks with Hermitian and Triangular matrices
- Performance regressions in BaseBenchmarks due to #54647 (Cleanup `MemoryRef`) HOT 3
- observed failure of TestPkg thread test
- race condition in waitany test
- Regression in threads tests on FreeBSD
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from julia.