TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Schwartzian Transform

21 pointsby licornaover 9 years ago

3 comments

dangover 9 years ago
I like the Arc idiom for this. Here&#x27;s how to sort a list (say of stories) by some computed value (say a function called score):<p><pre><code> (sort (compute &gt; score) stories) </code></pre> If score is expensive, you just wrap it in memo:<p><pre><code> (sort (compare &gt; (memo score)) stories) </code></pre> Now it will compute each score once, save them in a table, and throw the table away when done.
评论 #10757036 未加载
twotwotwoover 9 years ago
A possibly counterintuitive case where &quot;the key...is expensive to compute&quot; can be when accessing the key just requires following a pointer to something not in cache. It&#x27;s not the same thing syntactically, but, for example, Postgres sped up collation by packing bytes into integers:<p><a href="http:&#x2F;&#x2F;pgeoghegan.blogspot.com&#x2F;2015&#x2F;01&#x2F;abbreviated-keys-exploiting-locality-to.html" rel="nofollow">http:&#x2F;&#x2F;pgeoghegan.blogspot.com&#x2F;2015&#x2F;01&#x2F;abbreviated-keys-expl...</a><p>I&#x27;ve also seen speedups sorting strings by first sorting prefixes of them packed into uint64s (rather than sorting pointer&#x2F;length references to string data), and in an application where a friend needed to sort a lot of indices into an array, it turned out to greatly speed the sort (by avoiding indirections) to copy the whole array, sort the indices and the copy of the array together, then toss out the copy.
paulsutterover 9 years ago
tl&#x2F;dr, a sort optimization: avoid recomputing the sort keys by temporarily associating them with the input items.<p>Wouldn&#x27;t it be embarrassing to have one&#x27;s name in the title of a long winded Wikipedia article for a super simple and obvious optimization.