• wheezy@lemmy.ml
    link
    fedilink
    English
    arrow-up
    72
    ·
    edit-2
    1 day ago

    Most satisfying answer I could find.

    The designers of the Internet really knew how the hardware worked, and they designed with low level implementation in mind.

    The values 0, 127 and 255 are special in 8 bit assembly and machine language programming because there are “tricks” you can use to test for these values and branch to different code using smaller instructions that execute faster than for other integers. 127 is the highest signed 8 bit integer, so incrementing it by 1 will cause a signed overflow. Similarly, incrementing 255 will cause unsigned overflow. Merely loading the value 0 into a register will usually set a zero flag on the chip. Imagine the networking program looks like this in pseudocode:

    if (value == 0) doLocal();

    if (value == 127) doLoopback();

    if (value == 255) doNetwork();

    Although it depends on the chip, in those days most chips could encode these tests with 2 words, 3 words and 3 words respectively (total 8 words) and further those particular tests were all likely to execute in 1 clock cycle each. Using any other value would probably require 4 words each (total 12 words), a 50% increase in code size and likely a 50% increase in execution time as well.

    https://superuser.com/questions/31824/why-is-localhost-ip-127-0-0-1

    • Taldan@lemmy.world
      link
      fedilink
      arrow-up
      1
      ·
      4 hours ago

      Specifically, all loopback addresses will begin with 0111 1111

      Any time a computer reads an address beginning with those 8 digits, it can already determine it’s a loopback address without reading the other 24 digits (keep in mind, computers read IP addresses in binary)

      Why they decided to use an entire /8 CIDR block, I’ll never know. It is an absurd number of addresses to allocate out for localhost

    • Artwork@lemmy.world
      link
      fedilink
      English
      arrow-up
      27
      ·
      1 day ago

      Thank you… Thank you heartfelt… for not referencing/using sorrowful empty LLM for it, but referencing an actually accountable answer with an author to appreciate and learn from, with a direct link to the source.

      I upvoted the answers I found supportive, the question, read more to recall it all again, and bookmarked.

    • folekaule@lemmy.world
      link
      fedilink
      arrow-up
      14
      ·
      1 day ago

      Excellent explanation. Just came here to add more context: This was before CIDR routing when networks were classful. Localhost is in the class A network range and was reserved in 1981 for loopback use in IPv4. In 1981 processors were much, much slower than today and every cycle counted. If you look at bit patterns for the various networks and netmasks, it will all start to make more sense.

      IPv6 Always had ::1 for loopback.