[PATCH 6/7] btrfs: accessors: set target address at initialization

David Sterba
2025-07-01T17:24:18Z

The target address for the read/write can be simplified as it's the same
expression for the first folio. This improves the generated code as the
folio address does not have to be cached on stack.

Stack usage reduction:

btrfs_set_32 -8 (32 -> 24)
btrfs_set_64 -8 (32 -> 24)
btrfs_get_16 -8 (24 -> 16)

Code size reduction:

text data bss dec hex filename
1454459 115665 16088 1586212 183424 pre/btrfs.ko
1454279 115665 16088 1586032 183370 post/btrfs.ko

DELTA: -180

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

diff --git a/fs/btrfs/accessors.c b/fs/btrfs/accessors.c
index 8df404b5f6a334..af11f547371815 100644
--- a/fs/btrfs/accessors.c
+++ b/fs/btrfs/accessors.c
@@ -47,7 +47,7 @@ u##bits btrfs_get_##bits(const struct extent_buffer *eb, \
const unsigned long idx = get_eb_folio_index(eb, member_offset);\
const unsigned long oil = get_eb_offset_in_folio(eb, \
member_offset);\
- char *kaddr = folio_address(eb->folios[idx]); \
+ char *kaddr = folio_address(eb->folios[idx]) + oil; \
const int part = eb->folio_size - oil; \
u8 lebytes[sizeof(u##bits)]; \
\
@@ -57,14 +57,14 @@ u##bits btrfs_get_##bits(const struct extent_buffer *eb, \
} \
if (INLINE_EXTENT_BUFFER_PAGES == 1 || sizeof(u##bits) == 1 || \
likely(sizeof(u##bits) <= part)) \
- return get_unaligned_le##bits(kaddr + oil); \
+ return get_unaligned_le##bits(kaddr); \
\
if (sizeof(u##bits) == 2) { \
- lebytes[0] = *(kaddr + oil); \
+ lebytes[0] = *kaddr; \
kaddr = folio_address(eb->folios[idx + 1]); \
lebytes[1] = *kaddr; \
} else { \
- memcpy(lebytes, kaddr + oil, part); \
+ memcpy(lebytes, kaddr, part); \
kaddr = folio_address(eb->folios[idx + 1]); \
memcpy(lebytes + part, kaddr, sizeof(u##bits) - part); \
} \
@@ -77,7 +77,7 @@ void btrfs_set_##bits(const struct extent_buffer *eb, void *ptr, \
const unsigned long idx = get_eb_folio_index(eb, member_offset);\
const unsigned long oil = get_eb_offset_in_folio(eb, \
member_offset);\
- char *kaddr = folio_address(eb->folios[idx]); \
+ char *kaddr = folio_address(eb->folios[idx]) + oil; \
const int part = eb->folio_size - oil; \
u8 lebytes[sizeof(u##bits)]; \
\
@@ -87,16 +87,16 @@ void btrfs_set_##bits(const struct extent_buffer *eb, void *ptr, \
} \
if (INLINE_EXTENT_BUFFER_PAGES == 1 || sizeof(u##bits) == 1 || \
likely(sizeof(u##bits) <= part)) { \
- put_unaligned_le##bits(val, kaddr + oil); \
+ put_unaligned_le##bits(val, kaddr); \
return; \
} \
put_unaligned_le##bits(val, lebytes); \
if (sizeof(u##bits) == 2) { \
- *(kaddr + oil) = lebytes[0]; \
+ *kaddr = lebytes[0]; \
kaddr = folio_address(eb->folios[idx + 1]); \
*kaddr = lebytes[1]; \
} else { \
- memcpy(kaddr + oil, lebytes, part); \
+ memcpy(kaddr, lebytes, part); \
kaddr = folio_address(eb->folios[idx + 1]); \
memcpy(kaddr, lebytes + part, sizeof(u##bits) - part); \
} \
--
2.49.0