Skip to content

Commit 1441ed9

Browse files
committed
btrfs: speed up btrfs_get_##bits helpers
The helpers 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. This saves one function call to map_private_extent_buffer and several unnecessary temporary variables. Reviewed-by: Johannes Thumshirn <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent 5e39468 commit 1441ed9

File tree

1 file changed

+10
-19
lines changed

1 file changed

+10
-19
lines changed

fs/btrfs/struct-funcs.c

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -99,28 +99,19 @@ u##bits btrfs_get_token_##bits(struct btrfs_map_token *token, \
9999
u##bits btrfs_get_##bits(const struct extent_buffer *eb, \
100100
const void *ptr, unsigned long off) \
101101
{ \
102-
unsigned long part_offset = (unsigned long)ptr; \
103-
unsigned long offset = part_offset + off; \
104-
void *p; \
105-
int err; \
106-
char *kaddr; \
107-
unsigned long map_start; \
108-
unsigned long map_len; \
109-
int size = sizeof(u##bits); \
110-
u##bits res; \
102+
const unsigned long member_offset = (unsigned long)ptr + off; \
103+
const unsigned long oip = offset_in_page(member_offset); \
104+
const int size = sizeof(u##bits); \
105+
__le##bits leres; \
111106
\
112107
ASSERT(check_setget_bounds(eb, ptr, off, size)); \
113-
err = map_private_extent_buffer(eb, offset, size, \
114-
&kaddr, &map_start, &map_len); \
115-
if (err) { \
116-
__le##bits leres; \
117-
\
118-
read_extent_buffer(eb, &leres, offset, size); \
119-
return le##bits##_to_cpu(leres); \
108+
if (oip + size <= PAGE_SIZE) { \
109+
const unsigned long idx = member_offset >> PAGE_SHIFT; \
110+
const char *kaddr = page_address(eb->pages[idx]); \
111+
return get_unaligned_le##bits(kaddr + oip); \
120112
} \
121-
p = kaddr + part_offset - map_start; \
122-
res = get_unaligned_le##bits(p + off); \
123-
return res; \
113+
read_extent_buffer(eb, &leres, member_offset, size); \
114+
return le##bits##_to_cpu(leres); \
124115
} \
125116
void btrfs_set_token_##bits(struct btrfs_map_token *token, \
126117
const void *ptr, unsigned long off, \

0 commit comments

Comments
 (0)