Hacker News

Top stories

Live mirror
30 storiesupdated just nowView source snapshot
  1. Jeff – Jev-compatible 0.8B decision models, trained at home, ~30 ms (github.com/firelex)
    145comments
  2. 1996 chat room simulator connected to Win95 and System 7 web desktops (lolchat.rip)
    34comments
  3. Pirating the Pirates (mubi.com)
    241comments
  4. MicroLLM Lab – Try 7 tiny LLM's in the browser (stateofutopia.com)
    68comments
  5. Phyllotaxis: An audio-reactive LED display (jagi.studio)
    1comments
  6. Tank Body Problem (jimsitu.com)
    9comments
  7. 12,000-year-old Göbeklitepe burials explain scattered bones (archaeologymag.com)
    27comments
  8. California farmers are struggling to sell grapes as demand for wine drops (kqed.org)
    266comments
  9. ESP32S3 cluster running 1.58-bit (BitNet) Language model (github.com/low-zi-hong)
    8comments
  10. Sonnet 5.5 (anthropic.com)
    447comments
  11. Scientists solve 1840s space weather mystery (arstechnica.com)
    41comments
  12. Who Killed Paulina Borsook's Career? (wired.com)
    —discuss
  13. Hijacking the PS5's RTMP stream (yashgarg.dev)
    69comments
  14. Show HN: Pac-Bench – How well can models one-shot a Pac-Man game? (jonclegg.github.io)
    5comments
  15. World Labs Is Joining AMD (worldlabs.ai)
    94comments
  16. Kids turned low-traffic NPR Spotify comments into a secret group chat (thisamericanlife.org)
    195comments
  17. Does Reddit have an astroturfing problem? What the data suggests (petervijeh.com)
    184comments
  18. How to win a beer with high-dimensional statistics (jamiesimon.io)
    6comments
  19. Updated Google Maps shows destruction of the city of Rafah (twitter.com/aliabunimah)
    171comments
  20. Nvidia wants to put a watchdog chip next to every AI agent (cnbc.com)
    161comments
  21. What is the best shape of a city? Modelling effect of urban form on distance (sagepub.com)
    12comments
  22. Bluegraph – Explore NOAA buoy data, rebuilt in 3D from measured spectra (bluegraph.io)
    2comments
  23. It's Time to Investigate the AI Labs (calnewport.com)
    136comments
  24. Profit Margins of the Largest Companies (visualcapitalist.com)
    3comments
  25. The Art Forger Who Became a National Hero (priceonomics.com)
    5comments
  26. U.S. Strategic Petroleum Reserve Falls to Lowest Level Since 1982 (oilprice.com)
    122comments
  27. Show HN: HN.watch – Videos of all Hacker News posts (hn.watch)
    86comments
  28. Cf: The Agentic CLI for the Cloudflare API (cloudflare.com)
    59comments
  29. What reversing, modernising old games tells us about the economic impact of AI (isfine.org)
    40comments
  30. Behold the pawpaw (cbc.ca)
    26comments

Beating Decades of Optimized C with 80 Lines of Haskell

14 pointsby 7y agochrispenner.ca
13 comments
7y agoHN ↗

I don't consider this "beating" the C version. The new version isn't even semantically equivalent. You had to resort to multiple cores. A parallelized C version of wc would probably be even faster.

There's not much content here, IMO.

7y agoHN ↗

This is a pretty good example of why I don't like Haskell:

* The claim is Haskell is better because it's simpler: the final code is decidedly not simple

* You don't need to know about machine behavior: the example required numerous manual additions, e.g. explicit inlining, explicit strictness, and not using the "obvious" data structures

After all that contortion the only reason it was able to beat the C version was by using multiple cores, specifically on a 4 core machine it was only ~60% faster, and around 80% faster when parsing utf8. Better yet the C implementation actually does true utf8 parsing, via libc, so it's not inlined, whereas the "faster" Haskell code only counts the beginning of multibyte sequences.

I would argue that sans the special cases (which the example doesn't trigger), the C version is the obvious first pass implementation.

7y agoHN ↗

I just wrote a very dumb implementation of wc and it’s easily twice as fast.

I think the core issue is that wc is not a super optimized program. It is sufficiently fast for most purposes and so hasn’t ever been improved.

7y agoHN ↗

I’m comparing to Mac wc, which is apparently what they were testing against, and I was getting twice that perf without anything clever.

That said I’ll try to post my horrifying impl to GitHub later :)

7y agoHN ↗

Here is my super dumb implementation. Doesn't handle multibyte, doesn't handle stdin -- but I don't think the Haskell version did either, so I don't have a problem with that limit.

https://github.com/ojhunt/wc

It's the simplest thing that could obviously work, but its generally not tested beyond the most basic versions.

7y agoHN ↗

All the Haskell version is doing is counting through bytes with the high bit set. If you look at the exciting function in the wc sources they link to it is doing way more work.

7y agoHN ↗

I see calls to mbrtowc, which means they support non-utf-8 locales, but I'd love to know, given utf-8, what the semantic difference would be. Are there utf-8 inputs for which the Haskell and C `wc -m` give different answers?

7y agoHN ↗

You are correct - I was wrong about utf8, it is just doing a more correct decode than the inline trivial test that the Haskell version does.

7y agoHN ↗

The "flux monoid" for word counts is pretty cool. IIRC there was a paper about doing something like that with regular expression monoids to make parallelizable RE matchers, etc. I can't recall the title at the moment.

7y agoHN ↗

What a clickbait indeed. The end version is still eating 3 times more memory, but the worst is that it just doesn't work in the general case.

The way I generally use "wc" is inside a somewhat complex command line, with commands preceding it. As in "feeding characters" to it.

There is a reason why "wc" is not multithreaded: it just can't. It must work sequentially, because in the general case the input of "wc" cannot be skipped over.

This is one of the two big assumptions that are made by the author ("wc works only for files, so we can lseek") -- the second, identified by the author, being that the underlying hardware and filesystem must support concurrent access to the same location efficiently.

7y agoHN ↗

Don’t forget: the best case (where it can be parallel), is still only slightly faster than the single threaded version.