git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 10/10] pack-revindex: radix-sort the revindex

From
Jeff King <peff@peff.net>
Date
Jul 11, 2013, 11:03 UTC
Message-ID
<20130711110332.GA6015@sigill.intra.peff.net>
In-Reply-To
<CALkWK0=_6-RN2dHrhMdKu8nTyj1iwVe388Y57pOac17xhbqn5Q@mail.gmail.com>
On Wed, Jul 10, 2013 at 06:47:49PM +0530, Ramkumar Ramachandra wrote:
> > For a 64-bit off_t, using 16-bit "digits" gives us k=4.
> 
> Wait, isn't off_t a signed data type?  Did you account for that in
> your algorithm?

It is signed, but the values we are storing in the revindex are all positive file offsets. Right-shifting a positive signed type is explicitly allowed in C.

Show 6 quoted lines
> > -static int cmp_offset(const void *a_, const void *b_)
> > +/*
> > + * This is a least-significant-digit radix sort.
> > + */
> 
> Any particular reason for choosing LSD, and not MSD?

Simplicity. An MSD implementation should have the same algorithmic complexity and in theory, one can do MSD in-place. I'm happy enough with the speedup here, but if you want to take a stab at beating my times with MSD, please feel free.

The other "usual" downside of MSD is that it is typically not stable, but we don't care about that here. We know that our sort keys are unique.

Show 6 quoted lines
> > +#define DIGIT_SIZE (16)
> > +#define BUCKETS (1 << DIGIT_SIZE)
> 
> Okay, NUMBER_OF_BUCKETS = 2^RADIX, and you choose a hex radix.  Is
> off_t guaranteed to be fixed-length though?  I thought only the ones
> in stdint.h were guaranteed to be fixed-length?

I'm not sure what you mean by fixed-length. If you mean does it have the same size on every platform, then no. It will typically be 32-bit on platforms without largefile support, and 64-bit elsewhere. But it shouldn't matter. We'll first sort the entries by the lower 16 bits, and then if we have more bits, by the next 16 bits, and so on. We quit when the maximum value to sort (which we know ahead of time from the size of the packfile) is smaller than the 16-bits we are on. So we don't need to know the exact size of off_t, only the maximum value in our list (which must, by definition, be smaller than what can be represented by off_t).

Show 8 quoted lines
> > +       /*
> > +        * We want to know the bucket that a[i] will go into when we are using
> > +        * the digit that is N bits from the (least significant) end.
> > +        */
> > +#define BUCKET_FOR(a, i, bits) (((a)[(i)].offset >> (bits)) & (BUCKETS-1))
> 
> Ouch!  This is unreadable.  Just write an inline function instead?  A
> % would've been easier on the eyes, but you chose base-16.

I specifically avoided an inline function because they are subject to compiler settings. This isn't just "it would be a bit faster if this got inlined, and OK otherwise" but "this would be horribly slow if not inlined".

I'm also not sure that
  static inline unsigned bucket_for(const struct revindex *a,
                                    unsigned i,
                                    unsigned bits)
  {
          return a[i].offset >> bits & (BUCKETS-1);
  }
is actually any more readable.

I'm not sure what you mean by base-16. No matter the radix digit size, as long as it is an integral number of bits, we can mask it off, which is more efficient than modulo. A good compiler should see that it is a constant and convert it to a bit-mask, but I'm not sure I agree that modular arithmetic is more readable. This is fundamentally a bit-twiddling operation, as we are shifting and masking.

I tried to explain it in the comment; suggestions to improve that are welcome.

Show 9 quoted lines
> > +       /*
> > +        * We need O(n) temporary storage, so we sort back and forth between
> > +        * the real array and our tmp storage. To keep them straight, we always
> > +        * sort from "a" into buckets in "b".
> > +        */
> > +       struct revindex_entry *tmp = xcalloc(n, sizeof(*tmp));
> 
> Shouldn't this be sizeof (struct revindex_entry), since tmp hasn't
> been declared yet?

No, the variable is declared (but uninitialized) in its initializer. Despite its syntax, sizeof() is not a function and does not care about the state of the variable, only its type.

> Also, s/n/revindex_nr/, and something more appropriate for tmp?
What name would you suggest be be more appropriate for tmp?
> > +       int bits = 0;
> > +       unsigned *pos = xmalloc(BUCKETS * sizeof(*pos));
> 
> sizeof(unsigned int), for clarity, if not anything else.

I disagree; in general, I prefer using sizeof(*var) rather than sizeof(type), because it avoids repeating ourselves, and there is no compile-time check that you have gotten it right.

In the initializer it is less important, because the type is right there. But when you are later doing:

  memset(pos, 0, BUCKETS * sizeof(*pos));

this is much more robust. If somebody changes the type of pos, the memset line does not need changed. If you used sizeof(unsigned), then the code is now buggy (and the compiler cannot notice).

> You picked malloc over calloc here, because you didn't want to incur
> the extra cost of zero-initializing the memory?

Yes. We have to zero-initialize in each loop, so there is no point spending the extra effort on calloc.

We could also xcalloc inside each loop iteration, but since we need the same-size allocation each time, I hoisted the malloc out of the loop.

> Also, pos is the actual buckets array, I presume (hence unsigned,
> because there can't be a negative number of keys in any bucket)?

Exactly. I called it "pos" rather than "buckets" because the goal is to get the start-position of each bucket (as explained in the comment in the loop).

Show 5 quoted lines
> > +       while (max >> bits) {
> 
> No clue what max is.  Looked at the caller and figured out that it's
> the pack-size, although I'm still clueless about why it's appearing
> here.

It's larger than the largest sort key in the array. On an LSD radix sort, we can stop sorting when we are looking at a radix digit whose value is larger than the max, because we know all of the entries will simply have "0" in that digit.

So even if off_t is 64-bit, we can quit after the 32nd bit (i.e., k=2) if the packfile is smaller than 4G.

An MSD radix sort could do the same trick, but would obviously skip the zero digits at the beginning rather than the end.

Show 7 quoted lines
> > +               struct revindex_entry *swap;
> > +               int i;
> > +
> > +               memset(pos, 0, BUCKETS * sizeof(*pos));
> 
> Ah, so that's why you used malloc there.  Wait, shouldn't this be
> memset(pos, 0, sizeof(*pos))?

No, that would zero only the first entry of the array. We allocated BUCKETS * sizeof(*pos) bytes, and we want to zero them all.

Show 7 quoted lines
> > +               swap = a;
> > +               a = b;
> > +               b = swap;
> 
> Wait a minute: why don't you just throw away b?  You're going to
> rebuild the queue in the next iteration anyway, no?  a is what is
> being sorted.

For each iteration, we need to sort into temporary storage. So you can do it like:

  1. sort entries into tmp
  2. copy tmp back into entries
  3. bump radix digit and goto 1

But you can eliminate the copy in step 2 if you instead go back and forth, like:

  1. sort entries into tmp
  2. bump radix digit
  3. sort tmp into entries
  ...etc

To do that in a loop, we need an alias for "the thing we are sorting from" and "the thing we are sorting to". Hence the "a" and "b" pointers. Perhaps these comments make more sense now:

+       /*
+        * We need O(n) temporary storage, so we sort back and forth between
+        * the real array and our tmp storage. To keep them straight, we always
+        * sort from "a" into buckets in "b".
+        */
+       struct revindex_entry *tmp = xcalloc(n, sizeof(*tmp));
+       struct revindex_entry *a = entries, *b = tmp;
[...]
+               /*
+                * Now "b" contains the most sorted list, so we swap "a" and
+                * "b" for the next iteration.
+                */
> > +               /* And bump our bits for the next round. */
> > +               bits += DIGIT_SIZE;
> 
> I'd have gone for a nice for-loop.
Yeah, that would look like:
  for (bits = 0; max >> bits; bits += DIGIT_SIZE) {
     ...
  }
I don't really find one more readable than the other.
Show 10 quoted lines
> > +       /*
> > +        * If we ended with our data in the original array, great. If not,
> > +        * we have to move it back from the temporary storage.
> > +        */
> > +       if (a != entries)
> > +               memcpy(entries, tmp, n * sizeof(*entries));
> 
> How could a be different from entries?  It has no memory allocated for
> itself, no?  Why did you even create a, and not directly operate on
> entries?
See the back-and-forth explanation above.
> > +       free(tmp);
> > +       free(pos);
> 
> Overall, I found it quite confusing :(

Clearly. It was confusing to write (especially because there are a number of optimizations, and because radix sort is not well known, at least to me), which is why I tried to comment profusely. It seems quite a few of them didn't help, as the answers to your questions were there. If you have suggestions for improvement to the comments, I'm all ears.

> > +#undef BUCKET_FOR
> 
> Why not DIGIT_SIZE and BUCKETS too, while at it?

I forgot. I added them later (they were originally magic numbers in the code). Will add.

-Peff
Previous: Ramkumar RamachandraNext: Brandon Casey
Message 48 of 52 in “cat-file --batch-disk-sizes”
  1. 0/4 cat-file --batch-disk-sizesJeff King, Jul 7, 2013
  2. 1/4 zero-initialize object_info structsJeff King, Jul 7, 2013
  3. Junio C HamanoJul 7, 2013
  4. 2/4 teach sha1_object_info_extended a "disk_size" queryJeff King, Jul 7, 2013
  5. 3/4 cat-file: add --batch-disk-sizes optionJeff King, Jul 7, 2013
  6. Junio C HamanoJul 7, 2013
  7. Jeff KingJul 7, 2013
  8. Duy NguyenJul 8, 2013
  9. Ramkumar RamachandraJul 8, 2013
  10. Duy NguyenJul 8, 2013
  11. Ramkumar RamachandraJul 8, 2013
  12. Duy NguyenJul 9, 2013
  13. Ramkumar RamachandraJul 9, 2013
  14. Jeff KingJul 10, 2013
  15. Junio C HamanoJul 8, 2013
  16. Jeff KingJul 10, 2013
  17. Junio C HamanoJul 11, 2013
  18. brian m. carlsonJul 7, 2013
  19. Jeff KingJul 10, 2013
  20. 4/4 pack-revindex: radix-sort the revindexJeff King, Jul 7, 2013
  21. Shawn PearceJul 7, 2013
  22. Jeff KingJul 8, 2013
  23. Shawn PearceJul 8, 2013
  24. Brandon CaseyJul 8, 2013
  25. Brandon CaseyJul 8, 2013
  26. Jeff KingJul 10, 2013
  27. Jeff KingJul 10, 2013
  28. 00/10 cat-file formats/on-disk sizesJeff King, Jul 10, 2013
  29. 01/10 zero-initialize object_info structsJeff King, Jul 10, 2013
  30. 02/10 teach sha1_object_info_extended a "disk_size" queryJeff King, Jul 10, 2013
  31. 03/10 t1006: modernize output comparisonsJeff King, Jul 10, 2013
  32. 04/10 cat-file: teach --batch to stream blob objectsJeff King, Jul 10, 2013
  33. 05/10 cat-file: refactor --batch option parsingJeff King, Jul 10, 2013
  34. 06/10 cat-file: add --batch-check=<format>Jeff King, Jul 10, 2013
  35. Eric SunshineJul 10, 2013
  36. Ramkumar RamachandraJul 10, 2013
  37. Jeff KingJul 11, 2013
  38. 07/10 cat-file: add %(objectsize:disk) format atomJeff King, Jul 10, 2013
  39. 08/10 cat-file: split --batch input lines on whitespaceJeff King, Jul 10, 2013
  40. Ramkumar RamachandraJul 10, 2013
  41. Jeff KingJul 11, 2013
  42. Junio C HamanoJul 11, 2013
  43. 08/10 cat-file: split --batch input lines on whitespaceJeff King, Jul 11, 2013
  44. 09/10 pack-revindex: use unsigned to store number of objectsJeff King, Jul 10, 2013
  45. 10/10 pack-revindex: radix-sort the revindexJeff King, Jul 10, 2013
  46. Jeff KingJul 10, 2013
  47. Ramkumar RamachandraJul 10, 2013
  48. Jeff KingJul 11, 2013
  49. Brandon CaseyJul 10, 2013
  50. Jeff KingJul 11, 2013
  51. 10/10 pack-revindex: radix-sort the revindexJeff King, Jul 11, 2013
  52. Brandon CaseyJul 11, 2013

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.