All of lore.kernel.org
 help / color / mirror / Atom feed
From: David Sterba <dsterba@suse.com>
To: linux-btrfs@vger.kernel.org
Cc: David Sterba <dsterba@suse.com>
Subject: [PATCH 08/19] btrfs: speed up btrfs_get_token_##bits helpers
Date: Thu,  7 May 2020 22:19:46 +0200	[thread overview]
Message-ID: <430a17cee835574132a49c60883b64fdf39875cc.1588853772.git.dsterba@suse.com> (raw)
In-Reply-To: <cover.1588853772.git.dsterba@suse.com>

The set/get token helpers either use the cached address in the token or
unconditionally call map_private_extent_buffer to get the address of
page containing the requested offset plus the mapping start and length.
Depending on the return value, the fast path uses unaligned read to get
data within a page, or fall back to read_extent_buffer that can handle
reads spanning more pages.

This is all wasteful. We know the number of bytes to read, 1/2/4/8 and
can find out the page. Then simply check if it's contained or the
fallback is needed. The token address is updated to the page, or the on
the next index, expecting that the next read will use that.

This saves one function call to map_private_extent_buffer and several
unnecessary temporary variables.

Signed-off-by: David Sterba <dsterba@suse.com>
---
 fs/btrfs/struct-funcs.c | 43 +++++++++++++++--------------------------
 1 file changed, 16 insertions(+), 27 deletions(-)

diff --git a/fs/btrfs/struct-funcs.c b/fs/btrfs/struct-funcs.c
index e6d2bd019444..e357e0bab397 100644
--- a/fs/btrfs/struct-funcs.c
+++ b/fs/btrfs/struct-funcs.c
@@ -62,39 +62,28 @@ static bool check_setget_bounds(const struct extent_buffer *eb,
 u##bits btrfs_get_token_##bits(struct btrfs_map_token *token,		\
 			       const void *ptr, unsigned long off)	\
 {									\
-	unsigned long part_offset = (unsigned long)ptr;			\
-	unsigned long offset = part_offset + off;			\
-	void *p;							\
-	int err;							\
-	char *kaddr;							\
-	unsigned long map_start;					\
-	unsigned long map_len;						\
-	int size = sizeof(u##bits);					\
-	u##bits res;							\
+	const unsigned long member_offset = (unsigned long)ptr + off;	\
+	const unsigned long idx = member_offset >> PAGE_SHIFT;		\
+	const unsigned long oip = offset_in_page(member_offset);	\
+	const int size = sizeof(u##bits);				\
+	__le##bits leres;						\
 									\
 	ASSERT(token);							\
 	ASSERT(token->kaddr);						\
 	ASSERT(check_setget_bounds(token->eb, ptr, off, size));		\
-	if (token->offset <= offset &&					\
-	   (token->offset + PAGE_SIZE >= offset + size)) {	\
-		kaddr = token->kaddr;					\
-		p = kaddr + part_offset - token->offset;		\
-		res = get_unaligned_le##bits(p + off);			\
-		return res;						\
+	if (token->offset <= member_offset &&				\
+	    member_offset + size <= token->offset + PAGE_SIZE) {	\
+		return get_unaligned_le##bits(token->kaddr + oip);	\
 	}								\
-	err = map_private_extent_buffer(token->eb, offset, size,	\
-					&kaddr, &map_start, &map_len);	\
-	if (err) {							\
-		__le##bits leres;					\
-									\
-		read_extent_buffer(token->eb, &leres, offset, size);	\
-		return le##bits##_to_cpu(leres);			\
+	if (oip + size <= PAGE_SIZE) {					\
+		token->kaddr = page_address(token->eb->pages[idx]);	\
+		token->offset = idx << PAGE_SHIFT;			\
+		return get_unaligned_le##bits(token->kaddr + oip);	\
 	}								\
-	p = kaddr + part_offset - map_start;				\
-	res = get_unaligned_le##bits(p + off);				\
-	token->kaddr = kaddr;						\
-	token->offset = map_start;					\
-	return res;							\
+	token->kaddr = page_address(token->eb->pages[idx + 1]);		\
+	token->offset = (idx + 1) << PAGE_SHIFT;			\
+	read_extent_buffer(token->eb, &leres, member_offset, size);	\
+	return le##bits##_to_cpu(leres);				\
 }									\
 u##bits btrfs_get_##bits(const struct extent_buffer *eb,		\
 			 const void *ptr, unsigned long off)		\
-- 
2.25.0


  parent reply	other threads:[~2020-05-07 20:20 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-07 20:19 [PATCH 00/19] Set/get helpers speedups and cleanups David Sterba
2020-05-07 20:19 ` [PATCH 01/19] btrfs: use the token::eb for all set/get helpers David Sterba
2020-05-08 12:05   ` Johannes Thumshirn
2020-05-07 20:19 ` [PATCH 02/19] btrfs: drop eb parameter from set/get token helpers David Sterba
2020-05-08 12:09   ` Johannes Thumshirn
2020-05-11 13:02     ` David Sterba
2020-05-11 14:41       ` Johannes Thumshirn
2020-05-07 20:19 ` [PATCH 03/19] btrfs: don't use set/get token for single assignment in overwrite_item David Sterba
2020-05-08 13:25   ` Johannes Thumshirn
2020-05-07 20:19 ` [PATCH 04/19] btrfs: don't use set/get token in leaf_space_used David Sterba
2020-05-08 13:27   ` Johannes Thumshirn
2020-05-07 20:19 ` [PATCH 05/19] btrfs: preset set/get token with first page and drop condition David Sterba
2020-05-08 13:37   ` Johannes Thumshirn
2020-05-07 20:19 ` [PATCH 06/19] btrfs: add separate bounds checker for set/get helpers David Sterba
2020-05-08 13:39   ` Johannes Thumshirn
2020-05-07 20:19 ` [PATCH 07/19] btrfs: speed up btrfs_get_##bits helpers David Sterba
2020-05-08 13:42   ` Johannes Thumshirn
2020-05-07 20:19 ` David Sterba [this message]
2020-05-08 13:46   ` [PATCH 08/19] btrfs: speed up btrfs_get_token_##bits helpers Johannes Thumshirn
2020-05-07 20:19 ` [PATCH 09/19] btrfs: speed up btrfs_set_##bits helpers David Sterba
2020-05-08 13:48   ` Johannes Thumshirn
2020-05-07 20:19 ` [PATCH 10/19] btrfs: speed up btrfs_set_token_##bits helpers David Sterba
2020-05-08 13:50   ` Johannes Thumshirn
2020-05-11 13:17     ` David Sterba
2020-05-07 20:19 ` [PATCH 11/19] btrfs: speed up and simplify generic_bin_search David Sterba
2020-05-08 14:04   ` Johannes Thumshirn
2020-05-07 20:19 ` [PATCH 12/19] btrfs: remove unused map_private_extent_buffer David Sterba
2020-05-08 14:05   ` Johannes Thumshirn
2020-05-07 20:19 ` [PATCH 13/19] btrfs: constify extent_buffer in the API functions David Sterba
2020-05-08 14:07   ` Johannes Thumshirn
2020-05-07 20:20 ` [PATCH 14/19] btrfs: drop unnecessary offset_in_page in extent buffer helpers David Sterba
2020-05-08 14:13   ` Johannes Thumshirn
2020-05-07 20:20 ` [PATCH 15/19] btrfs: optimize split page read in btrfs_get_##bits David Sterba
2020-05-08 14:17   ` Johannes Thumshirn
2020-05-07 20:20 ` [PATCH 16/19] btrfs: optimize split page read in btrfs_get_token_##bits David Sterba
2020-05-08 14:18   ` Johannes Thumshirn
2020-05-07 20:20 ` [PATCH 17/19] btrfs: optimize split page write in btrfs_set_##bits David Sterba
2020-05-08 14:20   ` Johannes Thumshirn
2020-05-07 20:20 ` [PATCH 18/19] btrfs: optimize split page write in btrfs_set_token_##bits David Sterba
2020-05-08 14:21   ` Johannes Thumshirn
2020-05-07 20:20 ` [PATCH 19/19] btrfs: update documentation of set/get helpers David Sterba
2020-05-07 21:33   ` Nikolay Borisov
2020-05-11 13:10     ` David Sterba
2020-05-11 14:16       ` Nikolay Borisov

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=430a17cee835574132a49c60883b64fdf39875cc.1588853772.git.dsterba@suse.com \
    --to=dsterba@suse.com \
    --cc=linux-btrfs@vger.kernel.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.