Skip to content

Commit e48264e

Browse files
fdmananakdave
authored andcommitted
btrfs: avoid unnecessary memory allocation and copy at overwrite_item()
There's no need to allocate memory and copy from both the destination and source extent buffers to compare if the items are equal, we can instead use memcmp_extent_buffer() which allows to do only one memory allocation and copy instead of two. So use memcmp_extent_buffer() instead of memcmp(), allowing us to avoid one memory allocation, which can fail or be slow while under memory heavy pressure, avoid the memory copying and reducing code. Signed-off-by: Filipe Manana <fdmanana@suse.com> Reviewed-by: David Sterba <dsterba@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent 9db9c7d commit e48264e

File tree

1 file changed

+2
-10
lines changed

1 file changed

+2
-10
lines changed

fs/btrfs/tree-log.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,6 @@ static int overwrite_item(struct btrfs_trans_handle *trans,
422422

423423
if (ret == 0) {
424424
char *src_copy;
425-
char *dst_copy;
426425
u32 dst_size = btrfs_item_size(path->nodes[0],
427426
path->slots[0]);
428427
if (dst_size != item_size)
@@ -432,23 +431,16 @@ static int overwrite_item(struct btrfs_trans_handle *trans,
432431
btrfs_release_path(path);
433432
return 0;
434433
}
435-
dst_copy = kmalloc(item_size, GFP_NOFS);
436434
src_copy = kmalloc(item_size, GFP_NOFS);
437-
if (!dst_copy || !src_copy) {
435+
if (!src_copy) {
438436
btrfs_release_path(path);
439-
kfree(dst_copy);
440-
kfree(src_copy);
441437
return -ENOMEM;
442438
}
443439

444440
read_extent_buffer(eb, src_copy, src_ptr, item_size);
445-
446441
dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
447-
read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
448-
item_size);
449-
ret = memcmp(dst_copy, src_copy, item_size);
442+
ret = memcmp_extent_buffer(path->nodes[0], src_copy, dst_ptr, item_size);
450443

451-
kfree(dst_copy);
452444
kfree(src_copy);
453445
/*
454446
* they have the same contents, just return, this saves

0 commit comments

Comments
 (0)