Comments (7)
What's the bug? That the compiler is able to make the copying faster? That seems like a thing we want to keep.
It doesn't actually expose in the type system the internals.
from bytes.
I don't think people expect a from_iter
to be free.
They could think it from something like a Vec<u8>
, as it happens with Bytes::from(vec)
You see it like its free in the benchmark most likely because the compiler optimizes that a lot, since it is a hardcoded Vec
.
Try with something like this (note the std::hint::black_box
):
use bytes::BytesMut; // 1.6.1
#[global_allocator]
static ALLOC: dhat::Alloc = dhat::Alloc; // 0.3.3
fn main() {
let _guard = dhat::Profiler::new_heap();
let src = vec![1; 1024];
let mut_from_vec = std::hint::black_box(measure(|| BytesMut::from_iter(src)));
assert_eq!(mut_from_vec, 0); // doesn't allocate!
let mut_from_array = std::hint::black_box(measure(|| BytesMut::from_iter([1; 1024])));
assert_eq!(mut_from_array, 1024);
}
fn measure<T>(f: impl FnOnce() -> T) -> u64 {
let before = dhat::HeapStats::get();
f();
let after = dhat::HeapStats::get();
after.total_bytes - before.total_bytes
}
from bytes.
The fix would be to turn off specialization like this:
let src = vec![1; 1024];
let from_vec = measure(|| BytesMut::from_iter(NoSpecialize(src)));
assert_eq!(from_vec, 1024); // this used to be 0
struct NoSpecialize<T>(T);
impl Iterator for NoSpecialize<vec::IntoIter<u8>> {
type Item = u8;
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
}
impl IntoIterator for NoSpecialize<Vec<u8>> {
type Item = u8;
type IntoIter = NoSpecialize<vec::IntoIter<u8>>;
fn into_iter(self) -> Self::IntoIter {
NoSpecialize(self.0.into_iter())
}
}
from bytes.
I believe the section you're quoting is only talking about BytesMut
. We already have impl From<Vec<u8>> for Bytes
.
from bytes.
I believe the section you're quoting is only talking about BytesMut. We already have impl From<Vec> for Bytes.
Ah, my mistake! I've updated the issue and comments as appropriate :)
from bytes.
If you agree that this is a bug, I'll happily open a PR
from bytes.
Perhaps "bug" is a bit strong.
BytesMut
goes to moderate lengths to not allow cheap conversion from a Vec<u8>
so that users cannot rely on it.
But because of the interaction of the above traits, a wily user can get a "free" conversion to BytesMut
.
If the allocation strategy changes in future (which the comment in from_vec
seems to want to leave open), the "free" conversion becomes not free, which violates the comment.
from bytes.
Related Issues (20)
- Enable shrinking of allocations
- Contradictory allocation behaviour compared to documentation HOT 9
- Confusing documentation around `Arc<[u8]>` compatibility HOT 1
- Should clone benchmark use use `test::blackbox`? HOT 1
- Explicitly guarantee `Bytes` to be immutable HOT 2
- Expose UTF-8 validated string type HOT 2
- Buf::chunks_vectored() is wrong if chunk() isn't the whole buf HOT 3
- Feature request: fallible version of `BytesMut::unsplit` (i.e. make `BytesMut::try_unsplit` public) HOT 2
- Test for unknown --cfg flags in ci
- Splice for BytesMut HOT 2
- Consider replacing Bytes::make_mut by impl From<Bytes> for BytesMut HOT 5
- Is the conversion from bytes to Vec<u8> O(1) HOT 5
- Feature request: implement `From<Cow<'static, [u8]>>` for `Bytes`
- BytesMut::zeroed doc header improvement HOT 3
- Docs: `Buf::chunk` should have more restrictions. HOT 2
- 1.6.0 yanked? HOT 12
- BytesMut::advance no longer advances cursor HOT 4
- Feature Request? Viewing []Bytes as a single Bytes HOT 6
- `Buf::get_int()` implementation for `Bytes` returns positive number instead of negative when `nbytes` < 8. HOT 4
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 bytes.