Re: [PATCH v4 2/2] btrfs: index free space entries on size
From: David Sterba <hidden>
Date: 2021-11-18 16:43:40
On Thu, Nov 18, 2021 at 10:26:16AM -0500, Josef Bacik wrote:
+ */
+static inline u64 get_max_extent_size(struct btrfs_free_space *entry)
+{
+ if (entry->bitmap && entry->max_extent_size)
+ return entry->max_extent_size;
+ return entry->bytes;
+}
+
+/*
+ * This is indexed in reverse of what we generally do for rb-tree's, the largest
+ * chunks are left most and the smallest are rightmost. This is so that we can
+ * take advantage of the cached property of the cached rb-tree and simply get
+ * the largest free space chunk right away.
+ */
+static void tree_insert_bytes(struct btrfs_free_space_ctl *ctl,
+ struct btrfs_free_space *info)
+{
+ struct rb_root_cached *root = &ctl->free_space_bytes;
+ struct rb_node **p = &root->rb_root.rb_node;Please don't use single letter variables, other than 'i' for indexing. For tree nodes I've been renaming it to 'node' in any new code.
+ struct rb_node *parent_node = NULL;
+ struct btrfs_free_space *tmp;
+ bool leftmost = true;
+
+ while (*p) {
+ parent_node = *p;
+ tmp = rb_entry(parent_node, struct btrfs_free_space,
+ bytes_index);
+ if (get_max_extent_size(info) < get_max_extent_size(tmp)) {
+ p = &(*p)->rb_right;
+ leftmost = false;
+ } else {
+ p = &(*p)->rb_left;
+ }
+ }
+
+ rb_link_node(&info->bytes_index, parent_node, p);
+ rb_insert_color_cached(&info->bytes_index, root, leftmost);
+}
+
/*
* searches the tree for the given offset.
*