Skip to content

Commit 029e4a4

Browse files
committed
btrfs: speed up btrfs_set_##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 put to write data within a page, or fall back to write_extent_buffer that can handle writes spanning more pages. This is all wasteful. We know the number of bytes to write, 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 8f9da81 commit 029e4a4

File tree

1 file changed

+10
-17
lines changed

1 file changed

+10
-17
lines changed

fs/btrfs/struct-funcs.c

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -142,27 +142,20 @@ void btrfs_set_token_##bits(struct btrfs_map_token *token, \
142142
void btrfs_set_##bits(struct extent_buffer *eb, void *ptr, \
143143
unsigned long off, u##bits val) \
144144
{ \
145-
unsigned long part_offset = (unsigned long)ptr; \
146-
unsigned long offset = part_offset + off; \
147-
void *p; \
148-
int err; \
149-
char *kaddr; \
150-
unsigned long map_start; \
151-
unsigned long map_len; \
152-
int size = sizeof(u##bits); \
145+
const unsigned long member_offset = (unsigned long)ptr + off; \
146+
const unsigned long oip = offset_in_page(member_offset); \
147+
const int size = sizeof(u##bits); \
148+
__le##bits leres; \
153149
\
154150
ASSERT(check_setget_bounds(eb, ptr, off, size)); \
155-
err = map_private_extent_buffer(eb, offset, size, \
156-
&kaddr, &map_start, &map_len); \
157-
if (err) { \
158-
__le##bits val2; \
159-
\
160-
val2 = cpu_to_le##bits(val); \
161-
write_extent_buffer(eb, &val2, offset, size); \
151+
if (oip + size <= PAGE_SIZE) { \
152+
const unsigned long idx = member_offset >> PAGE_SHIFT; \
153+
char *kaddr = page_address(eb->pages[idx]); \
154+
put_unaligned_le##bits(val, kaddr + oip); \
162155
return; \
163156
} \
164-
p = kaddr + part_offset - map_start; \
165-
put_unaligned_le##bits(val, p + off); \
157+
leres = cpu_to_le##bits(val); \
158+
write_extent_buffer(eb, &leres, member_offset, size); \
166159
}
167160

168161
DEFINE_BTRFS_SETGET_BITS(8)

0 commit comments

Comments
 (0)