‹ BackHN Continuity

Thread

Anecdotally, programmers dislike "reduce"

175 points · 283 comments · vinhnx

  1. chubot · · focus · HN ↗
    Related to the point about worse performance, I'm pretty sure I was there when reduce was "banished" from Python 3 -- demoted to functools.reduce(), instead of the builtin reduce() in Python 2

    The story is that sometime in 2006 or 2007, Guido van Rossum was debugging why a web page in Google's internal code review tool (which he wrote) was taking 30+ seconds to render.

    This is basically a "production" incident, since thousands of Google engineers relied on the tool. Requests like this were probably tying up threads and exhausting thread pools, perhaps

    Eventually it was tracked down to a line wrapping algorithm written with reduce(). I don't think he wrote it -- it may have come in through a dependency. As many know, reduce() is basically:

         s1 + s2
         s1 + s2 + s3
         s1 + s2 + s3 + s4 
         ...
    
    And that's O(n^2) when s_i are strings. And I think it showed up if you viewed a 5000+ line diff, or a 5000+ line file. (Newer programs like Github also suffer here)

    I believe, in Python at that time, += was already optimized to avoid this (just like essentially all JS VMs are). Or you can use the idiom of append() to list and join() after.

    But reduce() basically forces the inefficient implementation, and I'm sure this is still true in Python 3.

    ---

    So basically Guido spent a long time debugging a performance problem related to reduce(), and made the decision to eject it, to help users avoid "footguns". I was his officemate at the time, so I recall this, but I wasn't involved directly

    Also, somebody contributed reduce() to Python way back in the 90's, as well as other functional idioms. He wouldn't have added that himself -- it was never his preferred style.

    He preferred a more imperative style. But he allowed those contributions, and then slightly regretted it later.

    <a href="https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;functools.html#functools.reduce" rel="nofollow">https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;functools.html#functools.r...

    1. bjourne · · focus · HN ↗
      Maybe you are misremembering the story? += deferred concatenation requires lazy strings and that didn&#x27;t come until 10-15 years later. However, concatenating string lists with sum() was a common Python idiom at the time and it indeed incurred O(n^2) complexity. Gvr&#x27;s reduce dislike was more about its syntax. It doesn&#x27;t mesh well with Python&#x27;s lambda syntax.
      1. Maxatar · · focus · HN ↗
        &gt; += deferred concatenation requires lazy strings and that didn&#x27;t come until 10-15 years later.

        CPython&#x27;s += does not perform deferred concatenation and CPython does not use lazy strings. The optimization uses an eager in-place realloc if the string&#x27;s ref-count is 1. This remains the optimization used even to this day and was introduced in 2005:

        <a href="https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;whatsnew&#x2F;2.4.html#optimizations" rel="nofollow">https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;whatsnew&#x2F;2.4.html#optimizations

        &gt;However, concatenating string lists with sum() was a common Python idiom at the time

        It could not possibly have been a common Python idiom since sum() explicitly rejected strings by throwing a TypeError. This was explicitly special cased to avoid the degenerate performance and the TypeError even has an error message saying &quot;TypeError: sum() can&#x27;t sum strings [use &#x27;&#x27;.join(seq) instead]&quot;.

        &gt;Gvr&#x27;s reduce dislike was more about its syntax. It doesn&#x27;t mesh well with Python&#x27;s lambda syntax.

        No it had nothing to do with mixing with lambda syntax, on the contrary GvR actually wanted to remove reduce and lambda (and map and filter as well). Here is the actual article by GvR regarding removing reduce, absolutely nothing in it involves how it mixes with lambda expressions.

        <a href="https:&#x2F;&#x2F;www.artima.com&#x2F;weblogs&#x2F;viewpost.jsp?thread=98196" rel="nofollow">https:&#x2F;&#x2F;www.artima.com&#x2F;weblogs&#x2F;viewpost.jsp?thread=98196

        &gt;So now reduce(). This is actually the one I&#x27;ve always hated most, because, apart from a few examples involving + or *, almost every time I see a reduce() call with a non-trivial function argument, I need to grab pen and paper to diagram what&#x27;s actually being fed into that function before I understand what the reduce() is supposed to do. So in my mind, the applicability of reduce() is pretty much limited to associative operators, and in all other cases it&#x27;s better to write out the accumulation loop explicitly.

        1. chubot · · focus · HN ↗
          Yes thanks for finding the Python 2.4 release page which shows the optimization! So I remembered correctly -- Python already had that optimization back then. (There seems to be a large amount of confusion on that in this subthread)

          And the March 2005 Artima post is also a very good reference! That actually predates my story, since Guido hadn&#x27;t joined Google by then. I recall that he joined in December 2005.

          So maybe the bug I remember was more of a &quot;push&quot; in the direction he had already thought of, not the direct inspiration.

          It&#x27;s clear from the blog post that he disliked all of map &#x2F; filter &#x2F; reduce, and then I&#x27;m sure that users or python-dev pushed back on removing them, so he settled for banishing reduce() to the stdlib.

      2. vhcr · · focus · HN ↗
        It was never possible to sum() strings.

        <a href="https:&#x2F;&#x2F;github.com&#x2F;python&#x2F;cpython&#x2F;commit&#x2F;a70b19147fd163744be34745d393af7be603629f" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;python&#x2F;cpython&#x2F;commit&#x2F;a70b19147fd163744be...

Open on Hacker News to reply ↗

Unofficial Hacker News client; not affiliated with Y Combinator.