Impact of OREF support

There are some classes in our code base that contain Methods only (no properties). I told my colleagues that converting them into the ClassMethods should improve performance as it would eliminate unnecessary OREF support at run-time. Some of them replied that it would be microseconds, so what is the reason to bother.

Is it possible to estimate the impact of OREF support of method calls at run-time? E.g., as a % of all CPU load. 

  • 0
  • 0
  • 261
  • 7
  • 5

Answers

Dear colleagues,

Thank you for paying so much attention to this tiny question. Maybe it was too tiny formulated: I should mention that objects instantiation impact is beyond the scope of the question as all of them are instantiated once; correspondent OREFs are stored in global scope variables for "public" use.

Going deep inside with %SYS.MONLBL is possible, while I'm too lazy to do it having no real performance problem. So, I've wrote several dummy methods, doubling instance and class ones, with different numbers of formal arguments, from 0 to 10. Here is the code I managed to write.

Class Scratch.test Extends %Library.RegisteredObject [ ProcedureBlock ]
ClassMethod dummyClassNull() As %String
{
  1
}

Method dummyInstNull() As %String
{
  1
}

ClassMethod dummyClass5(a1, a2, a3, a4, a5) As %String
{
  1
}

Method dummyInst5(a1, a2, a3, a4, a5) As %String
{
  1
}

ClassMethod dummyClass10(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) As %String
{
  1
}

Method dummyInst10(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) As %String
{
  1
}

}

My testing routine was:

ClassVsInst
   p1="пропоывшыщзшвыщшв"
   p2="гшщыгвыовлдыовдьыовдлоыдлв"
   p3="widuiowudoiwudoiwudoiwud"
   p4="прпроыпворыпворыпворыпв"
   p5="uywyiusywisywzxbabzjhagjЭ"
   p6="пропоывшыщзшвыщшв"
   p7="гшщыгвыовлдыовдьыовдлоыдлв"
   p8="widuiowudoiwudoiwudoiwud"
   p9="прпроыпворыпворыпворыпв"
   p10="uywyiusywisywzxbabzjhagjЭ"
   run^zmawr("s sc=##class(Scratch.test).dummyClass10(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10)",1000000,"dummyClass10 "_$p($zv,"(Build"))
   st st=##class(Scratch.test).%New() run^zmawr("s sc=st.dummyInst10(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10)",1000000,"dummyInst10 "_$p($zv,"(Build"))
   st=""
   run^zmawr("s sc=##class(Scratch.test).dummyClass5(p1,p2,p3,p4,p5)",1000000,"dummyClass5 "_$p($zv,"(Build"))
   st st=##class(Scratch.test).%New() run^zmawr("s sc=st.dummyInst5(p1,p2,p3,p4,p5)",1000000,"dummyInst5 "_$p($zv,"(Build"))
   st=""
   run^zmawr("s sc=##class(Scratch.test).dummyClassNull()",1000000,"dummyClassNull "_$p($zv,"(Build"))
   st st=##class(Scratch.test).%New() run^zmawr("s sc=st.dummyInstNull()",1000000,"dummyInstNull "_$p($zv,"(Build"))
   q

run(what, n, comment) ; execute line 'what' 'n' times
   n=$g(n,1)
   comment=$g(comment,"********** "_what_" "_n_" run(s) **********")
   comment,!
   zzh0=$zh
   i=1:1:what
   zzdt=$zh-zzh0 "total time = "_zzdt_" avg time = "_(zzdt/n),!
   q

The results were:

USER>d ClassVsInst^zmawr
dummyClass10 Cache for Windows (x86-64) 2017.2.2
total time = .377751 avg time = .000000377751
dummyInst10 Cache for Windows (x86-64) 2017.2.2
total time = .338336 avg time = .000000338336
dummyClass5 Cache for Windows (x86-64) 2017.2.2
total time = .335734 avg time = .000000335734
dummyInst5 Cache for Windows (x86-64) 2017.2.2
total time = .280145 avg time = .000000280145
dummyClassNull Cache for Windows (x86-64) 2017.2.2
total time = .256858 avg time = .000000256858
dummyInstNull Cache for Windows (x86-64) 2017.2.2
total time = .225813 avg time = .000000225813

So, despite my expectations, oref.Method() call turned to be quicker than its ##class(myClass).myMethod() analogue. As there is only less than microsecond effect per call, I don't see any reason for refactoring.

Thanks!
One headache less in the discussion about the "right" way to write code.
+a tiny piece for "creative freedom".  wink 

yes

Changed a bit and added new code:

/// d ##class(Scratch.test).ClassVsInst()
ClassMethod ClassVsInst(= {1e6}) [ ProcedureBlock = 0, PublicList = st ]
{
   p1p2p3p4p5p6p7p8p9p10xClassxInstst
   
   p1="пропоывшыщзшвыщшв"
    ,p2="гшщыгвыовлдыовдьыовдлоыдлв"
    ,p3="widuiowudoiwudoiwudoiwud"
    ,p4="прпроыпворыпворыпворыпв"
    ,p5="uywyiusywisywzxbabzjhagjЭ"
    ,p6="пропоывшыщзшвыщшв"
    ,p7="гшщыгвыовлдыовдьыовдлоыдлв"
    ,p8="widuiowudoiwudoiwudoiwud"
    ,p9="прпроыпворыпворыпворыпв"
    ,p10="uywyiusywisywzxbabzjhagjЭ"
    
    ,xClass="(args...) f i=1:1:N s sc=##class(Scratch.test).%1(args...)"
    ,xInst="(args...) f i=1:1:N s sc=st.%1(args...)"
   
   
   st=##class(Scratch.test).%New()
   
   w $p($zv,"(Build"),!!
   
   runClassmethod("dummyClass10"p1p2p3p4p5p6p7p8p9p10)
    ,runMethod("dummyClass10"p1p2p3p4p5p6p7p8p9p10)
    ,runMethod("dummyInst10"p1p2p3p4p5p6p7p8p9p10)
    ,runClassmethod("dummyClass5"p1p2p3p4p5)
    ,runMethod("dummyClass5"p1p2p3p4p5)
    ,runMethod("dummyInst5"p1p2p3p4p5)
    ,runClassmethod("dummyClassNull")
    ,runMethod("dummyClassNull")
    ,runMethod("dummyInstNull")
   
    ,runX("dummyClass10",xClassp1p2p3p4p5p6p7p8p9p10)
    ,runX("dummyInst10",xInstp1p2p3p4p5p6p7p8p9p10)
    ,runX("dummyClass5",xClassp1p2p3p4p5)
    ,runX("dummyInst5",xInstp1p2p3p4p5)
    ,runX("dummyClassNull",xClass)
    ,runX("dummyInstNull",xInst)
  
runMethod(methodname,args...)
   t,i,sc
   t=$zh f i=1:1:sc=$method(stmethodnameargs...)
   t=$zh-methodname,?16,"total time = "_t,?38,"avg time = "_(t/N),!
   q
runClassmethod(methodname,args...)
   t,i,sc
   t=$zh f i=1:1:sc=$classmethod("Scratch.test"methodnameargs...)
   t=$zh-methodname_"*",?16,"total time = "_t,?38,"avg time = "_(t/N),!
   q
runX(methodname,x,args...)
   t,i,sc
   t=$zh
   x ($$$FormatText(x,methodname), args...)
   t=$zh-"X"_methodname,?16,"total time = "_t,?38,"avg time = "_(t/N),!
   q
}
Result:

USER>##class(Scratch.test).ClassVsInst()
Cache for Windows (x86-64) 2018.1
 
dummyClass10*   total time = .328227  avg time = .000000328227
dummyClass10    total time = .27655   avg time = .00000027655
dummyInst10     total time = .259913  avg time = .000000259913
dummyClass5*    total time = .286983  avg time = .000000286983
dummyClass5     total time = .25666   avg time = .00000025666
dummyInst5      total time = .240312  avg time = .000000240312
dummyClassNull* total time = .274406  avg time = .000000274406
dummyClassNull  total time = .250926  avg time = .000000250926
dummyInstNull   total time = .234486  avg time = .000000234486
XdummyClass10   total time = .312917  avg time = .000000312917
XdummyInst10    total time = .264871  avg time = .000000264871
XdummyClass5    total time = .286985  avg time = .000000286985
XdummyInst5     total time = .238557  avg time = .000000238557
XdummyClassNull total time = .278684  avg time = .000000278684
XdummyInstNull  total time = .236815  avg time = .000000236815

Vitaliy, thanks for the contribution. It seems that it ruins another myth, that Xecute is always slower than $[class]method. I've slightly reformatted an output of your ClassVsInst() method just to make it easier to compare results. Here are mine (using i5-4460 3.20 GHz):

 USER>d ##class(Scratch.test).ClassVsInst(1e7)
Cache for Windows (x86-64) 2017.2.2
 
dummyClass10*   total time = 2.669215 avg time = .0000002669215
dummyClass10    total time = 2.375893 avg time = .0000002375893
XdummyClass10   total time = 2.676997 avg time = .0000002676997
 
dummyInst10     total time = 2.221366 avg time = .0000002221366
XdummyInst10    total time = 2.276357 avg time = .0000002276357
 
dummyClass5*    total time = 2.540907 avg time = .0000002540907
dummyClass5     total time = 2.232347 avg time = .0000002232347
XdummyClass5    total time = 2.541123 avg time = .0000002541123
 
dummyInst5      total time = 2.070013 avg time = .0000002070013
XdummyInst5     total time = 2.049437 avg time = .0000002049437
 
dummyClassNull* total time = 2.362451 avg time = .0000002362451
dummyClassNull  total time = 2.097653 avg time = .0000002097653
XdummyClassNull total time = 2.352748 avg time = .0000002352748
 
dummyInstNull   total time = 2.018773 avg time = .0000002018773
XdummyInstNull  total time = 2.056379 avg time = .0000002056379

It seems that Xecute is (not surprisingly) very close to $classmethod and usually slower than $method. But what are we talking about? The difference is about several nanoseconds per call only. 

Class Test.RegObjTest Extends %RegisteredObject [ Final ]
{

ClassMethod NewMethod1() [ Final, NotInheritable ]
{
 "Neeravcxfdfd"
}

}
 

Of course, if your utility classes are all ABSTRACT it is pure code.  As any .MAC, just easier to read.
OREF is just a special data type (object pointer) and not better or worse than any other variable.

I 'd guess variable scoping and procedure block has much more (microscopic) influence on performance.

My personal preference is to have only code tightly related to stored date in "object"-classes.
Anything else outside that is not only related to this class.

To put pure methods in abstract or not purely depends if it can be overridden.

 I have never been sure what ProcedureBlock 0 or 1 means in terms of internal procedures. Can you pls put some light on it?

Procedureblock =1 

means ALL local variables except call parameters and %variables move to stack
and return when you return from your method (=procedure)

see Summary on Local Variable Scoping  for more 

this memory operation is not free but nothing to get a headache upon

I completely agree with you.

Objects should be instantiated only when they are relevant.

Calling object methods where it's possible to call class methods makes code harder to read.

Well, how I would dive into this issue.

  1. Run %SYS.MONLBL in production, for a relevant time gap, at least 1 hour. You can filter it by classes which you care, and get the only line counts, to decrease impact on speed.
  2. So, you can collect summary time for all such Methods.
  3. Change a few of them to the new way as a ClassMethod, from MONLBL you can find the list of frequently used ones. So, will be easy to find what to change.
  4. Run %SYS.MONLBL with the same conditions as in the first time.
  5. Compare results.
  6. Decide

Of course, this way not so easy to realize, and depends on how you deploy code in production. Or maybe it even possible to measure in the testing environment.

Comments

Will they ever be overridden or are you just calling them Class.Method