Skip to content

Everything is a float and NaN packing #9

@madmalik

Description

@madmalik

A lot of scripting languages choose IEEE 754 doubles as the only numeric type and then encode all other possible values in the range of redundant NaN values. There is enough room for pointers in there since current 64 bit architectures only use 48 bits.

There are obvious downsides:

  • Floating point math is hard to get right. Floats for everything can be a footgun. Also people will make fun of our programming language because 1/3 + 2/3 != 1
  • At least in theory, there can be portability issues
  • storing pointers cannot be done in safe rust (imo not a big deal, but should be noted)
  • other values must fit into the unused NaN range. LuaJIT for example uses this approach and therefore cannot implement the i64 datatype Lua 5.3 introduced.

In summery, this is an overreaching performance hack and it totally grosses me out.

But, it works surprisingly well in practice:

  • floats have a precise integer range that is sufficient in practice and overflow fails gracefully
  • Having all values register sized has a substantial performance impact
  • cases where i/u64s, big ints or rationals are absolutely nessesary are rare enough to put them behind a pointer indirection

I‘m not really sure language design should be driven by a performance hack, but its used succesfully im practice. Imo we should at least discuss if should consider such a design.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions