14 votes

NaN boxing or how to make the world dynamic (2020)

6 comments

  1. Zorind
    Link
    Every time I read anything about how interpreters or low-level languages function (or how high-level languages use them), I find it really interesting, and am so glad most of the tricks are things...

    Every time I read anything about how interpreters or low-level languages function (or how high-level languages use them), I find it really interesting, and am so glad most of the tricks are things I can completely ignore (as someone who primarily uses JavaScript, Python, and C#. Sure, I have to deal with byte arrays in some of the C# code I use, but rarely have to actually manipulate things on a bit level).

    The original programming practices and how we got to modern, optimized-under-the-hood programming languages astounds me. I am so grateful for the things I can write and generally trust that either: the compiler will handle optimization for me, or the optimization doesn’t matter all that much given the constraints of the systems I work with.

    Everything happening/explained in this article is so fascinating to me, but I’m extremely glad I will likely never need to think of it or try to use it (directly).

    5 votes
  2. skybrian
    Link
    This is something you can do in low-level languages such as C that I really missed when I wrote an interpreter in Go. These bit-packing tricks are unsafe (particularly for pointers) and would need...

    This is something you can do in low-level languages such as C that I really missed when I wrote an interpreter in Go. These bit-packing tricks are unsafe (particularly for pointers) and would need to be built in the implementation of union types in a safe, high-level language.

    Writing interpreters is a niche, though, so it’s understandable that the Go implementers don’t prioritize it.

    3 votes
  3. cazydave
    Link
    First time I post link, so sorry for any missing tag (there will be many). This is a walkthrough (guide?) on a way to optimize having a single variable storing (and switching) value of different type

    First time I post link, so sorry for any missing tag (there will be many).

    This is a walkthrough (guide?) on a way to optimize having a single variable storing (and switching) value of different type

    1 vote
  4. [3]
    donn
    Link
    Super, super interesting read. Nice seeing IEEE 754 being used like this. What's surprising to me though is that V8 uses tagged pointers, and thus no direct doubles. That's interesting given that...

    Super, super interesting read. Nice seeing IEEE 754 being used like this.

    What's surprising to me though is that V8 uses tagged pointers, and thus no direct doubles. That's interesting given that I think unless you do something like let x = (0 << 0); numbers in JS are doubles by default…

    1 vote
    1. [2]
      zestier
      (edited )
      Link Parent
      I've not read the V8 code, but I'd guess they let numbers be integers in basically every case that they're allowed to both to address that and just because integers should usually be faster for a...

      I've not read the V8 code, but I'd guess they let numbers be integers in basically every case that they're allowed to both to address that and just because integers should usually be faster for a variety of the common use cases. The JS spec doesn't really care how stuff is represented for as long as it can be treated as a double at the application layer. So stuff like 5 + 2 and arr.length - 1 can safely all be done with integers internally. It's not until stuff like introducing a double into the op (ex. 5 + 1.5) or operations that can yield different results (ex. arr.length / 2) that it becomes possible for an integer to be insufficient to fake a double.

      1 vote
      1. donn
        Link Parent
        That would make a lot of sense, thanks for the insight

        That would make a lot of sense, thanks for the insight