Comments (9)
Yeah that would have been very sensible. Unfortunately, sizeof(rand(100))
returns 800 as far back as at least 1.0, and I think probably closer to ~0.1 so we decided that changing that behavior would have been very breaking.
from julia.
at
x -> sizeof(x) == sizeof(typeof(x))
should always be true, but seems like that's not the case forArray
anymore
That's never been the case for Array
, but Array is a case where it didn't use to have a Julia-visible struct backing it — you couldn't ask for sizeof(Vector{Int})
before 1.11.
(edit: oh, sorry for the duplicate content; I had a stale page and didn't see Oscar's response until I posted)
from julia.
Ok, so are there any other exceptions to the intuition I posted above, other than Array
? That is, is the contract of sizeof
:
- The number of bytes (with padding) the object takes up in memory, without recursion into fields
- For
Array
s, the number of bytes needed to reference all of the stored objects. (Note: this doesn't include the reserved capacity!) - For
String
s, the number of bytes needed to store the contents of thatString
in memory. (Note: this doesn't include the stored length & possible trailing null)
FWIW, I don't think we'll get around having very low level details mentioned here. The meaning of sizeof
is intrinsically linked to how objects are stored in Julia. Without those details, the docstring really isn't useful when you really need to know exactly what sizeof
means.
from julia.
The whole docstring is this:
sizeof(T::DataType) sizeof(obj)
Size, in bytes, of the canonical binary representation of the given
DataType
T
, if any.
Or the size, in bytes, of objectobj
if it is not aDataType
.
Which seems a little confused. The two descriptions here actually seem backwards. In talking about what the size of a type is, it really seems like we shouldn't need to use any of the words "canonical" or "binary" or "representation," do we? It's just the number of bytes needed for the struct
or primitive type
.
It's when we're talking about the generic behaviors of arbitrary objects where the "canonical binary representation" seems to be both relevant and necessary. To be concrete, I think of sizeof
as x->length(sprint(write, x))
.
Cf. #12791 (comment)
from julia.
julia> using Serialization
julia> sizeofs(x) = sizeof(x), length(sprint(serialize, x)), write(devnull, x)
sizeofs (generic function with 1 method)
julia> sizeofs(falses(100))
(16, 61, 16)
julia> sizeofs(fill(false, 100))
(100, 16, 100)
julia> sizeofs("abc")
(3, 13, 3)
julia> sizeofs("aβc")
(4, 13, 4)
julia> sizeofs(['a', 'b', 'c'])
(12, 24, 12)
julia> sizeofs(view(['a', 'b', 'c'], 1:2))
(8, 99, 8)
julia> sizeofs(SubString("aβc", 2))
(3, 37, 3)
from julia.
I'm not sure what you're trying to say there, but yes, I initially wrote serialize
and then edited to write
. It's write
's docstring that mirrors the "canonical binary representation" language. But if you look further down in #12791 you can see that it wasn't 100% clear back then, either.
Good thing the help for
sizeof
andwrite
do not mention each other.
:)
from julia.
To me, sizeof
is "the number of contiguous bytes the object takes up in memory, without following pointers". That may or may not match write
, if you consider e.g. padding!
from julia.
I will say that this isn't just a user-level question. @vtjnash and I spent at least an hour trying to figure out what sizeof
and Core.sizeof
were supposed to return in the case of Memory
and Array
written on top of Memory
. (and I don't remember what we ended up going with and am only ~60% sure we chose something reasonable).
from julia.
My intuition tells me that x -> sizeof(x) == sizeof(typeof(x))
should always be true, but seems like that's not the case for Array
anymore.. So is that true for Core.sizeof
then, since it's not documented? Quite unexpected, why is there a difference between the two in the first place?
from julia.
Related Issues (20)
- Warning in the Building HTML documentation HOT 2
- Inverse of `Hermitian` complex matrix giving wrong results HOT 3
- What should the mangled names of anonymous functions and nested closures be?
- 'make test' fails for julia-1.10.2 HOT 16
- Julia REPL should show environment name inline
- `BoundsError` in type inference when abstract-evaluating OpaqueClosure HOT 1
- We should have no throw `setindex!` for existing indices. HOT 2
- whether `Core.checked_dims` decides to throw depends on dimension order (for empty arrays) HOT 2
- colonful `reshape` may spuriously throw `DivideError`
- GC and multithreading. Strange behaviour. HOT 3
- Update main entry-point docs to use lowercase `args` HOT 2
- Segmentation fault with Distributed when --threads is set HOT 2
- Julia is incorrectly passing arguments to C on Apple M1 HOT 11
- Strange code generation with `unsafe_trunc` and dynamic dispatch HOT 17
- Segfault during push!() if using a large struct in 1.10 due to elsize overflow HOT 7
- segfault in `jl_datatype_layout` while constructing `Memory` HOT 5
- Slow/delays in REPL upon start HOT 3
- Creating struct instances is slow when another inner constructor returns `undef` fields HOT 4
- Possible bug when constructing a string from an array of characters HOT 4
- Too long stack traces at the REPL shell mode HOT 1
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.