mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Mark Fasheh <mfasheh@suse.com>
To: linux-kernel@vger.kernel.org
Cc: ocfs2-devel@oss.oracle.com, Joel Becker <joel.becker@oracle.com>,
	Mark Fasheh <mfasheh@suse.com>
Subject: [PATCH 24/35] ocfs2: One more hamming code optimization.
Date: Thu, 25 Dec 2008 10:04:39 -0800	[thread overview]
Message-ID: <1230228290-22803-25-git-send-email-mfasheh@suse.com> (raw)
In-Reply-To: <1230228290-22803-1-git-send-email-mfasheh@suse.com>

From: Joel Becker <joel.becker@oracle.com>

The previous optimization used a fast find-highest-bit-set operation to
give us a good starting point in calc_code_bit().  This version lets the
caller cache the previous code buffer bit offset.  Thus, the next call
always starts where the last one left off.

This reduces the calculation another 39%, for a total 80% reduction from
the original, naive implementation.  At least, on my machine.  This also
brings the parity calculation to within an order of magnitude of the
crc32 calculation.

Signed-off-by: Joel Becker <joel.becker@oracle.com>
Signed-off-by: Mark Fasheh <mfasheh@suse.com>
---
 fs/ocfs2/blockcheck.c |   61 +++++++++++++++---------------------------------
 1 files changed, 19 insertions(+), 42 deletions(-)

diff --git a/fs/ocfs2/blockcheck.c b/fs/ocfs2/blockcheck.c
index f102ec9..2a947c4 100644
--- a/fs/ocfs2/blockcheck.c
+++ b/fs/ocfs2/blockcheck.c
@@ -41,34 +41,6 @@
 
 
 /*
- * Find the log base 2 of 32-bit v.
- *
- * Algorithm found on http://graphics.stanford.edu/~seander/bithacks.html,
- * by Sean Eron Anderson.  Code on the page is in the public domain unless
- * otherwise noted.
- *
- * This particular algorithm is credited to Eric Cole.
- */
-static int find_highest_bit_set(unsigned int v)
-{
-
-	static const int MultiplyDeBruijnBitPosition[32] =
-	{
-		0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
-		31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
-	};
-
-	v |= v >> 1; /* first round down to power of 2 */
-	v |= v >> 2;
-	v |= v >> 4;
-	v |= v >> 8;
-	v |= v >> 16;
-	v = (v >> 1) + 1;
-
-	return MultiplyDeBruijnBitPosition[(u32)(v * 0x077CB531UL) >> 27];
-}
-
-/*
  * Calculate the bit offset in the hamming code buffer based on the bit's
  * offset in the data buffer.  Since the hamming code reserves all
  * power-of-two bits for parity, the data bit number and the code bit
@@ -81,10 +53,14 @@ static int find_highest_bit_set(unsigned int v)
  * so it's a parity bit.  2 is a power of two (2^1), so it's a parity bit.
  * 3 is not a power of two.  So bit 1 of the data buffer ends up as bit 3
  * in the code buffer.
+ *
+ * The caller can pass in *p if it wants to keep track of the most recent
+ * number of parity bits added.  This allows the function to start the
+ * calculation at the last place.
  */
-static unsigned int calc_code_bit(unsigned int i)
+static unsigned int calc_code_bit(unsigned int i, unsigned int *p_cache)
 {
-	unsigned int b, p;
+	unsigned int b, p = 0;
 
 	/*
 	 * Data bits are 0-based, but we're talking code bits, which
@@ -92,24 +68,25 @@ static unsigned int calc_code_bit(unsigned int i)
 	 */
 	b = i + 1;
 
-	/*
-	 * As a cheat, we know that all bits below b's highest bit must be
-	 * parity bits, so we can start there.
-	 */
-        p = find_highest_bit_set(b);
+	/* Use the cache if it is there */
+	if (p_cache)
+		p = *p_cache;
         b += p;
 
 	/*
 	 * For every power of two below our bit number, bump our bit.
 	 *
-	 * We compare with (b + 1) becuase we have to compare with what b
+	 * We compare with (b + 1) because we have to compare with what b
 	 * would be _if_ it were bumped up by the parity bit.  Capice?
 	 *
-	 * We start p at 2^p because of the cheat above.
+	 * p is set above.
 	 */
-	for (p = (1 << p); p < (b + 1); p <<= 1)
+	for (; (1 << p) < (b + 1); p++)
 		b++;
 
+	if (p_cache)
+		*p_cache = p;
+
 	return b;
 }
 
@@ -126,7 +103,7 @@ static unsigned int calc_code_bit(unsigned int i)
  */
 u32 ocfs2_hamming_encode(u32 parity, void *data, unsigned int d, unsigned int nr)
 {
-	unsigned int i, b;
+	unsigned int i, b, p = 0;
 
 	BUG_ON(!d);
 
@@ -145,7 +122,7 @@ u32 ocfs2_hamming_encode(u32 parity, void *data, unsigned int d, unsigned int nr
 		 * i is the offset in this hunk, nr + i is the total bit
 		 * offset.
 		 */
-		b = calc_code_bit(nr + i);
+		b = calc_code_bit(nr + i, &p);
 
 		/*
 		 * Data bits in the resultant code are checked by
@@ -201,7 +178,7 @@ void ocfs2_hamming_fix(void *data, unsigned int d, unsigned int nr,
 	 * nr + d is the bit right past the data hunk we're looking at.
 	 * If fix after that, nothing to do
 	 */
-	if (fix >= calc_code_bit(nr + d))
+	if (fix >= calc_code_bit(nr + d, NULL))
 		return;
 
 	/*
@@ -209,7 +186,7 @@ void ocfs2_hamming_fix(void *data, unsigned int d, unsigned int nr,
 	 * start b at the offset in the code buffer.  See hamming_encode()
 	 * for a more detailed description of 'b'.
 	 */
-	b = calc_code_bit(nr);
+	b = calc_code_bit(nr, NULL);
 	/* If the fix is before this hunk, nothing to do */
 	if (fix < b)
 		return;
-- 
1.5.6


  parent reply	other threads:[~2008-12-25 18:13 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-12-25 18:04 [git patches] Ocfs2 patches for merge window, batch 3/3 Mark Fasheh
2008-12-25 18:04 ` [PATCH 01/35] jbd2: Add buffer triggers Mark Fasheh
2008-12-31 18:16   ` [Ocfs2-devel] " Mark Fasheh
2008-12-25 18:04 ` [PATCH 02/35] ocfs2: Add the on-disk structures for metadata checksums Mark Fasheh
2008-12-25 18:04 ` [PATCH 03/35] ocfs2: Add the underlying blockcheck code Mark Fasheh
2008-12-25 18:04 ` [PATCH 04/35] ocfs2: Add a validation hook for quota block reads Mark Fasheh
2008-12-25 18:04 ` [PATCH 05/35] ocfs2: block read meta ecc Mark Fasheh
2008-12-25 18:04 ` [PATCH 06/35] ocfs2: Add journal_access functions with jbd2 triggers Mark Fasheh
2008-12-25 18:04 ` [PATCH 07/35] ocfs2: Wrap up the common use cases of ocfs2_new_path() Mark Fasheh
2008-12-25 18:04 ` [PATCH 08/35] ocfs2: Use metadata-specific ocfs2_journal_access_*() functions Mark Fasheh
2008-12-25 18:04 ` [PATCH 09/35] ocfs2: Add ecc and checksums to ocfs2 xattr buckets Mark Fasheh
2008-12-25 18:04 ` [PATCH 10/35] ocfs2: Create ocfs2_xattr_value_buf Mark Fasheh
2008-12-25 18:04 ` [PATCH 11/35] ocfs2: Pull ocfs2_xattr_value_buf up from __ocfs2_remove_xattr_range() Mark Fasheh
2008-12-25 18:04 ` [PATCH 12/35] ocfs2: Pull ocfs2_xattr_value_buf up into ocfs2_xattr_value_truncate() Mark Fasheh
2008-12-25 18:04 ` [PATCH 13/35] ocfs2: Pass ocfs2_xattr_value_buf " Mark Fasheh
2008-12-25 18:04 ` [PATCH 14/35] ocfs2: Pass value buf to ocfs2_xattr_update_entry() Mark Fasheh
2008-12-25 18:04 ` [PATCH 15/35] ocfs2: Use ocfs2_xattr_value_buf in ocfs2_xattr_set_entry() Mark Fasheh
2008-12-25 18:04 ` [PATCH 16/35] ocfs2: Pass value buf to ocfs2_remove_value_outside() Mark Fasheh
2008-12-25 18:04 ` [PATCH 17/35] ocfs2: Use proper journal_access function in xattr.c Mark Fasheh
2008-12-25 18:04 ` [PATCH 18/35] ocfs2: Add directory block trailers Mark Fasheh
2008-12-25 18:04 ` [PATCH 19/35] ocfs2: Checksum and ECC for directory blocks Mark Fasheh
2008-12-25 18:04 ` [PATCH 20/35] ocfs2: Validate superblock with checksum and ecc Mark Fasheh
2008-12-25 18:04 ` [PATCH 21/35] ocfs2: Enable metadata checksums Mark Fasheh
2008-12-25 18:04 ` [PATCH 22/35] ocfs2: Don't hand-code xor in ocfs2_hamming_encode() Mark Fasheh
2008-12-25 18:04 ` [PATCH 23/35] ocfs2: Another hamming code optimization Mark Fasheh
2008-12-25 18:04 ` Mark Fasheh [this message]
2008-12-25 18:04 ` [PATCH 25/35] ocfs2/dlm: Fix a race between migrate request and exit domain Mark Fasheh
2008-12-25 18:04 ` [PATCH 26/35] ocfs2/dlm: Clean up errors in dlm_proxy_ast_handler() Mark Fasheh
2008-12-25 18:04 ` [PATCH 27/35] ocfs2/dlm: Hold off sending lockres drop ref message while lockres is migrating Mark Fasheh
2008-12-25 18:04 ` [PATCH 28/35] ocfs2/dlm: Fix race in adding/removing lockres' to/from the tracking list Mark Fasheh
2008-12-25 18:04 ` [PATCH 29/35] ocfs2/dlm: Fix race during lockres mastery Mark Fasheh
2008-12-25 18:04 ` [PATCH 30/35] ocfs2/xattr: Remove extend_trans call and add its credits from the beginning Mark Fasheh
2008-12-25 18:04 ` [PATCH 31/35] ocfs2/xattr: Always updating ctime during xattr set Mark Fasheh
2008-12-25 18:04 ` [PATCH 32/35] ocfs2/xattr: fix credits calculation during index create Mark Fasheh
2008-12-25 18:04 ` [PATCH 33/35] ocfs2: calculate and reserve credits for xattr value in mknod Mark Fasheh
2008-12-25 18:04 ` [PATCH 34/35] ocfs2: alloc xattr bucket in ocfs2_xattr_set_handle Mark Fasheh
2008-12-25 18:04 ` [PATCH 35/35] ocfs2: Add xattr support checking in init_security Mark Fasheh

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1230228290-22803-25-git-send-email-mfasheh@suse.com \
    --to=mfasheh@suse.com \
    --cc=joel.becker@oracle.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ocfs2-devel@oss.oracle.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®