Git Product home page Git Product logo

Comments (9)

oscardssmith avatar oscardssmith commented on May 18, 2024 1

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.

mbauman avatar mbauman commented on May 18, 2024 1

at x -> sizeof(x) == sizeof(typeof(x)) should always be true, but seems like that's not the case for Array 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.

Seelengrab avatar Seelengrab commented on May 18, 2024 1

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 Arrays, the number of bytes needed to reference all of the stored objects. (Note: this doesn't include the reserved capacity!)
  • For Strings, the number of bytes needed to store the contents of that String 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.

mbauman avatar mbauman commented on May 18, 2024

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 object obj if it is not a DataType.

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.

LilithHafner avatar LilithHafner commented on May 18, 2024
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.

mbauman avatar mbauman commented on May 18, 2024

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 and write do not mention each other.

:)

from julia.

Seelengrab avatar Seelengrab commented on May 18, 2024

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.

oscardssmith avatar oscardssmith commented on May 18, 2024

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.

Seelengrab avatar Seelengrab commented on May 18, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.