[PATCH] erofs-utils: use qsort() to sort dir->i_subdirs
Gao Xiang
hsiangkao at redhat.com
Fri Apr 2 13:17:41 AEDT 2021
On Fri, Apr 02, 2021 at 10:12:50AM +0800, Gao Xiang wrote:
> Hi Weiwen,
>
> On Thu, Apr 01, 2021 at 09:52:51PM +0800, Hu Weiwen wrote:
> > Original implementation use insertion sort, and its time complexity is
> > O(n^2). This patch use qsort instead. When I create a directory with
> > 100k entries, this reduces the user space time from around 3 mins to
> > 0.5s.
> >
> > Create such a large directory for benchmark with:
> > mkdir large; cd large; touch $(seq 100000);
> >
> > Signed-off-by: Hu Weiwen <sehuww at mail.scut.edu.cn>
>
> Thanks for your work. Yeah, it's another path that needs to be
> optimized for huge dirs.
>
> The overall looks good to me, some nits below...
>
> > ---
> > lib/inode.c | 53 +++++++++++++++++++++++++++++++++--------------------
> > 1 file changed, 33 insertions(+), 20 deletions(-)
> >
> > diff --git a/lib/inode.c b/lib/inode.c
> > index d52facf..9217127 100644
> > --- a/lib/inode.c
> > +++ b/lib/inode.c
> > @@ -96,21 +96,6 @@ unsigned int erofs_iput(struct erofs_inode *inode)
> > return 0;
> > }
> >
> > -static int dentry_add_sorted(struct erofs_dentry *d, struct list_head *head)
> > -{
> > - struct list_head *pos;
> > -
> > - list_for_each(pos, head) {
> > - struct erofs_dentry *d2 =
> > - container_of(pos, struct erofs_dentry, d_child);
> > -
> > - if (strcmp(d->name, d2->name) < 0)
> > - break;
> > - }
> > - list_add_tail(&d->d_child, pos);
> > - return 0;
> > -}
> > -
> > struct erofs_dentry *erofs_d_alloc(struct erofs_inode *parent,
> > const char *name)
> > {
> > @@ -122,7 +107,7 @@ struct erofs_dentry *erofs_d_alloc(struct erofs_inode *parent,
> > strncpy(d->name, name, EROFS_NAME_LEN - 1);
> > d->name[EROFS_NAME_LEN - 1] = '\0';
> >
> > - dentry_add_sorted(d, &parent->i_subdirs);
> > + list_add_tail(&d->d_child, &parent->i_subdirs);
> > return d;
> > }
> >
> > @@ -156,10 +141,19 @@ static int __allocate_inode_bh_data(struct erofs_inode *inode,
> > return 0;
> > }
> >
> > +static int comp_subdir(const void *a, const void *b)
> > +{
> > + const struct erofs_dentry *d_a, *d_b;
> > +
> > + d_a = *((const struct erofs_dentry **)a);
> > + d_b = *((const struct erofs_dentry **)b);
> > + return strcmp(d_a->name, d_b->name);
> > +}
>
> How about just use `da' and `db' for size?
... for these...
>
> > +
> > -int erofs_prepare_dir_file(struct erofs_inode *dir)
> > +int erofs_prepare_dir_file(struct erofs_inode *dir, unsigned int nr_subdirs)
> > {
> > - struct erofs_dentry *d;
> > - unsigned int d_size, i_nlink;
> > + struct erofs_dentry *d, **all_d;
> > + unsigned int d_size, i_nlink, i;
> > int ret;
> >
> > /* dot is pointed to the current dir inode */
> > @@ -172,6 +166,22 @@ int erofs_prepare_dir_file(struct erofs_inode *dir)
> > d->inode = erofs_igrab(dir->i_parent);
> > d->type = EROFS_FT_DIR;
> >
> > + /* sort subdirs */
> > + nr_subdirs += 2;
> > + all_d = malloc(nr_subdirs * sizeof(d));
>
> maybe just use `sorted' name here?
>
> > + if (!all_d)
> > + return -ENOMEM;
> > + i = 0;
> > + list_for_each_entry(d, &dir->i_subdirs, d_child)
>
> I think we could list_del here, and use list_for_each_entry
Ah, I meant list_for_each_entry_safe. The reply was somewhat
buggy as well..
>
> > + all_d[i++] = d;
> > + DBG_BUGON(i != nr_subdirs);
> > + qsort(all_d, nr_subdirs, sizeof(d), comp_subdir);
> > + init_list_head(&dir->i_subdirs);
>
> After list_del, no need to init_list_head again.
> The another reason is that some list_add_tail implementation
> could check elements isn't in a list first.
>
> > + for (i = 0; i < nr_subdirs; i++)
> > + list_add_tail(&all_d[i]->d_child, &dir->i_subdirs);
> > + free(all_d);
> > + all_d = NULL;
>
> no need to NULLify it..
>
> Thanks,
> Gao Xiang
>
More information about the Linux-erofs
mailing list