Skip to contentSkip to conversation

erofs-utils: lib: converted division to shift in z_erofs_load_compact_lcluster

PATCH In review Ashley Lee 3 participants 5 messages Feb 25 – Feb 27 Library code

lore
Share & tools
lore

Ashley Lee

author
#0 lore raw

PATCH v2 1/1 erofs-utils: lib: converted division to shift in z_erofs_load_compact_lcluster

perf on fsck.erofs in gcc reports that z_erofs_load_compact_lcluster was spending 20% of its time doing the div instruction. While the function itself is ~40% of user runtime. In the source code, it seems that dividing by vcnt doesn't optimize to a shift despite the two possible states being powers of 2.

Changing the division into a ilog2() function call encourages the compiler to recognize it as a power of 2. Thus performing a shift.

Running a benchmark on lzma compressed freebsd code on x86, shows there is a ~4% increase in performance in gcc. While clang shows virtually no regression in performance. The tradeoff is slightly obfuscated source code.

The following command was run locally on x86.

$ hyperfine -w 5 -m 30 "./fsck.erofs ./bsd.erofs.lzma"

patch on gcc 15.2.1
Time (mean ± σ): 354.8 ms ± 6.0 ms \
[User: 227.8 ms, System: 126.1 ms]
Range (min … max): 345.8 ms … 366.2 ms 30 runs

dev on gcc 15.2.1
Time (mean ± σ): 370.7 ms ± 6.7 ms \
[User: 246.5 ms, System: 123.4 ms]
Range (min … max): 362.7 ms … 390.7 ms 30 runs

patch on clang 21.1.8
Time (mean ± σ): 371.9 ms ± 2.4 ms \
[User: 247.2 ms, System: 123.9 ms]
Range (min … max): 369.1 ms … 380.0 ms 30 runs

dev on clang 21.1.8
Time (mean ± σ): 371.0 ms ± 1.9 ms \
[User: 245.5 ms, System: 124.5 ms]
Range (min … max): 368.4 ms … 377.7 ms 30 runs

---
v2: changed vdiv to ilog2 call

 lib/zmap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/zmap.c +1 -1
index baec278..3ac7fe9 100644
@@ -160,7 +160,7 @@ static int z_erofs_load_compact_lcluster(struct z_erofs_maprecorder *m,
 	m->nextpackoff = round_down(pos, vcnt << amortizedshift) +
 			 (vcnt << amortizedshift);
 	lobits = max(lclusterbits, ilog2(Z_EROFS_LI_D0_CBLKCNT) + 1U);
-	encodebits = ((vcnt << amortizedshift) - sizeof(__le32)) * 8 / vcnt;
+	encodebits = (((vcnt << amortizedshift) - sizeof(__le32)) * 8) >> ilog2(vcnt);
 	bytes = pos & ((vcnt << amortizedshift) - 1);
 	in -= bytes;
 	i = bytes >> amortizedshift;
Signature · 1 line
2.53.0
#1 lore raw

Hi Lee,

A quick search shows that x86 kernel implementation also use div instruction

under Linux v6.19 and GCC 15.2.1, add GCC correctly generate shift instruction

in my arm64 machine with GCC 14.2.0.

Could you also consider evaluate this optimization in kernel?

Thanks,

Yifan

Quoted 58 lines · Ashley Lee

perf on fsck.erofs in gcc reports that z_erofs_load_compact_lcluster was spending 20% of its time doing the div instruction. While the function itself is ~40% of user runtime. In the source code, it seems that dividing by vcnt doesn't optimize to a shift despite the two possible states being powers of 2.

Changing the division into a ilog2() function call encourages the compiler to recognize it as a power of 2. Thus performing a shift.

Running a benchmark on lzma compressed freebsd code on x86, shows there is a ~4% increase in performance in gcc. While clang shows virtually no regression in performance. The tradeoff is slightly obfuscated source code.

The following command was run locally on x86.

$ hyperfine -w 5 -m 30 "./fsck.erofs ./bsd.erofs.lzma"

patch on gcc 15.2.1

Time (mean ± σ):     354.8 ms ±   6.0 ms    \
   [User: 227.8 ms, System: 126.1 ms]

Range (min … max): 345.8 ms … 366.2 ms 30 runs

dev on gcc 15.2.1

Time (mean ± σ):     370.7 ms ±   6.7 ms    \
   [User: 246.5 ms, System: 123.4 ms]

Range (min … max): 362.7 ms … 390.7 ms 30 runs

patch on clang 21.1.8

Time (mean ± σ):     371.9 ms ±   2.4 ms    \
   [User: 247.2 ms, System: 123.9 ms]

Range (min … max): 369.1 ms … 380.0 ms 30 runs

dev on clang 21.1.8

Time (mean ± σ):     371.0 ms ±   1.9 ms    \
   [User: 245.5 ms, System: 124.5 ms]

Range (min … max): 368.4 ms … 377.7 ms 30 runs

---
v2: changed vdiv to ilog2 call

  lib/zmap.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
lib/zmap.c +1 -1
index baec278..3ac7fe9 100644
@@ -160,7 +160,7 @@ static int z_erofs_load_compact_lcluster(struct z_erofs_maprecorder *m,
  	m->nextpackoff = round_down(pos, vcnt << amortizedshift) +
  			 (vcnt << amortizedshift);
  	lobits = max(lclusterbits, ilog2(Z_EROFS_LI_D0_CBLKCNT) + 1U);
-	encodebits = ((vcnt << amortizedshift) - sizeof(__le32)) * 8 / vcnt;
+	encodebits = (((vcnt << amortizedshift) - sizeof(__le32)) * 8) >> ilog2(vcnt);
  	bytes = pos & ((vcnt << amortizedshift) - 1);
  	in -= bytes;
  	i = bytes >> amortizedshift;
#2 lore raw
Quoted 6 lines · Yifan Zhao

Hi Lee,

A quick search shows that x86 kernel implementation also use div instruction

under Linux v6.19 and GCC 15.2.1, add GCC correctly generate shift instruction

Could we try more GCC versions and find the impacts?

Does it a recent GCC regression?

Quoted 4 lines

in my arm64 machine with GCC 14.2.0.

Could you also consider evaluate this optimization in kernel?

Yes, but I wonder if div is already used for these years on x86, could you check this?

Thanks,
Gao Xiang

Quoted 65 lines

Thanks,

Yifan

Quoted 58 lines · Ashley Lee

perf on fsck.erofs in gcc reports that z_erofs_load_compact_lcluster was spending 20% of its time doing the div instruction. While the function itself is ~40% of user runtime. In the source code, it seems that dividing by vcnt doesn't optimize to a shift despite the two possible states being powers of 2.

Changing the division into a ilog2() function call encourages the compiler to recognize it as a power of 2. Thus performing a shift.

Running a benchmark on lzma compressed freebsd code on x86, shows there is a ~4% increase in performance in gcc. While clang shows virtually no regression in performance. The tradeoff is slightly obfuscated source code.

The following command was run locally on x86.

$ hyperfine -w 5 -m 30 "./fsck.erofs ./bsd.erofs.lzma"

patch on gcc 15.2.1
Time (mean ± σ):     354.8 ms ±   6.0 ms    \
   [User: 227.8 ms, System: 126.1 ms]
Range (min … max):   345.8 ms … 366.2 ms    30 runs

dev on gcc 15.2.1
Time (mean ± σ):     370.7 ms ±   6.7 ms    \
   [User: 246.5 ms, System: 123.4 ms]
Range (min … max):   362.7 ms … 390.7 ms    30 runs

patch on clang 21.1.8
Time (mean ± σ):     371.9 ms ±   2.4 ms    \
   [User: 247.2 ms, System: 123.9 ms]
Range (min … max):   369.1 ms … 380.0 ms    30 runs

dev on clang 21.1.8
Time (mean ± σ):     371.0 ms ±   1.9 ms    \
   [User: 245.5 ms, System: 124.5 ms]
Range (min … max):   368.4 ms … 377.7 ms    30 runs

---
v2: changed vdiv to ilog2 call

  lib/zmap.c | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
lib/zmap.c +0 -0
index baec278..3ac7fe9 100644
@@ -160,7 +160,7 @@ static int z_erofs_load_compact_lcluster(struct z_erofs_maprecorder *m,

      m->nextpackoff = round_down(pos, vcnt << amortizedshift) +
               (vcnt << amortizedshift);
      lobits = max(lclusterbits, ilog2(Z_EROFS_LI_D0_CBLKCNT) + 1U);
-    encodebits = ((vcnt << amortizedshift) - sizeof(__le32)) * 8 / vcnt; +    encodebits = (((vcnt << amortizedshift) - sizeof(__le32)) * 8) >> ilog2(vcnt); bytes = pos & ((vcnt << amortizedshift) - 1);
      in -= bytes;
      i = bytes >> amortizedshift;

author
#3 lore raw
Quoted 15 lines · Gao Xiang
Quoted 4 lines · Yifan Zhao

A quick search shows that x86 kernel implementation also use div instruction

under Linux v6.19 and GCC 15.2.1, add GCC correctly generate shift instruction

Could we try more GCC versions and find the impacts?

Does it a recent GCC regression?

Quoted 1 line
Quoted 6 lines
Quoted 3 lines

in my arm64 machine with GCC 14.2.0.

Could you also consider evaluate this optimization in kernel?

Yes, but I wonder if div is already used for these years on x86, could you check this?

Hello all,

I'll attempt to perform benchmarks on as many architectures and compiler variants as I can. I was looking into lkp-tests to perform this however I was facing trouble running on my OS. Is there a simple way to do this?

Thank you,

Ashley

#4 lore raw
Quoted 23 lines · Ashley Lee
Quoted 15 lines · Gao Xiang
Quoted 4 lines · Yifan Zhao

A quick search shows that x86 kernel implementation also use div instruction

under Linux v6.19 and GCC 15.2.1, add GCC correctly generate shift instruction

Could we try more GCC versions and find the impacts?

Does it a recent GCC regression?

Quoted 1 line
Quoted 6 lines
Quoted 3 lines

in my arm64 machine with GCC 14.2.0.

Could you also consider evaluate this optimization in kernel?

Yes, but I wonder if div is already used for these years on x86, could you check this?

Quoted 6 lines

Hello all,

I'll attempt to perform benchmarks on as many architectures and compiler variants as I can. I was looking into lkp-tests to perform this however I was facing trouble running on my OS. Is there a simple way to do this?

I never customize lkp-tests, but I guess just triggering fio on common platfroms with a given dataset (like enwik9) is enough.

Thanks,
Gao Xiang

Quoted 4 lines

Thank you,

Ashley