Patch: More __builtin_expect fun

Bryce McKinlay bryce@albatross.co.nz
Mon May 1 17:48:00 GMT 2000


Anthony Green wrote:

> Tom wrote:
> > I think we should definitely only do this when there is a measurable
> > benefit.  There is a tradeoff with maintainability.
>
> Bryce - did you compile with -freorder-blocks?  I think you need to
> use that flag for __builtin_expect to have any effect.  I was never
> able to get it working (there are some general compiler issues to sort
> out still - I think an important fix went in today).

No, I didn't explicitly use -freorder-blocks. Presumably, this option is
automatically enabled at -O2?

Anyway, heres a little test that calls _Jv_CheckArrayStore and _Jv_AllocObj a
lot. I built two identical libgcj's, one with __builtin_expect #define'd away,
and one with it enabled. They were both built with the same compiler (20000427)
and CXXFLAGS="-g -pg -O2".

class A
{
}

class B extends A
{
}

public class ArrayStore
{
  static B[] ba = new B[30000];

  public static void main(String args[])
  {
    for (int j=0; j < 200; j++)
      for (int i=0; i < 30000; i++)
      {
 Object[] array = ba;
 Object b = new B();
 array[i] = b;
      }
  }
}

The test completes about 1% faster overall with __builtin_expect enabled.

Here's the top of the profile for the noexpect case:

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total
 time   seconds   seconds    calls  ms/call  ms/call  name
 38.61      1.39     1.39    17162     0.08     0.08  GC_mark_from_mark_stack
 13.33      1.87     0.48        1   480.00  3568.22
ArrayStore::main(JArray<java::lang::String *> *)
 10.83      2.26     0.39  6000160     0.00     0.00  _Jv_AllocObject
 10.28      2.63     0.37  6000024     0.00     0.00  _Jv_CheckArrayStore
  8.33      2.93     0.30  6000170     0.00     0.00  GC_gcj_malloc
  7.50      3.20     0.27    11669     0.02     0.02  GC_build_fl_clear2
  5.00      3.38     0.18  6000000     0.00     0.00  A::A(void)
  4.44      3.54     0.16  6000000     0.00     0.00  B::B(void)

And for the __builtin_expect case:

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total
 time   seconds   seconds    calls  ms/call  ms/call  name
 41.76      1.47     1.47    17162     0.09     0.09  GC_mark_from_mark_stack
 14.20      1.97     0.50        1   500.00  3377.35
ArrayStore::main(JArray<java::lang::String *> *)
  9.94      2.32     0.35  6000170     0.00     0.00  GC_gcj_malloc
  8.52      2.62     0.30  6000024     0.00     0.00  _Jv_CheckArrayStore
  7.95      2.90     0.28  6000160     0.00     0.00  _Jv_AllocObject
  4.55      3.06     0.16    11669     0.01     0.01  GC_build_fl_clear2
  3.69      3.19     0.13
java::lang::Object::Object(void)
  3.41      3.31     0.12  6000000     0.00     0.00  A::A(void)
  3.41      3.43     0.12  6000000     0.00     0.00  B::B(void)

Unfortunatly, the profiler is rather inaccurate, and the numbers vary too much
between runs to draw any hard-and-fast conclusions, but I believe the
_Jv_AllocObj and _Jv_CheckArrayStore comparisons do reflect a general trend.

regards

  [ bryce ]




More information about the Java-patches mailing list