Skip to content

Box.box does not need to allocate for values smaller than a pointer #15943

Open
@straight-shoota

Description

@straight-shoota

Box.box can cast values of small primitives types to pointer and pass that around directly, instead of allocating space and passing the pointer to that memory.

Extracted from #15562 (comment)

perhaps values smaller than a pointer also don't need an allocation either? Primitive numbers for example:

struct Box(T)
  def self.box(object : T) : Void*
    {% if T <= Int::Primitive && sizeof(T) <= sizeof(Void*) %}
      Pointer(Void).new(object.to_u64!)
    {% end %}
  end

  def self.unbox(pointer : Void*) : T
    {% if T <= Int::Primitive && sizeof(T) <= sizeof(Void*) %}
      T.new!(pointer.address)
    {% end %}
  end
end

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions