RFE - lazy loading of TclOO for faster interp creation
Tcl interp creation time more than doubled in 9.{0,1,2} compared to 8.6 as reported in 62019f8aa9. This difference has mostly been negated for 9.0.3 with zipfs related fixes. However, opportunities for further optimization remain. One of these is loading of TclOO. As measured on my system, TclOO initialization takes up close to 40% of interp creation time.
Since a very substantial (majority even?) number of Tcl applications do not use oo, one possibility is to lazy load TclOO. The apn-oo-lazy-init branch implements a proof of concept for this.
For comparison:
Current main branch:
% timerate {interp delete [interp create]}
1861.39 µs/# 537 # 537.23 #/sec 999.566 net-ms
apn-oo-lazy-init branch:
% timerate {interp delete [interp create]}
1175.20 µs/# 850 # 850.92 #/sec 998.919 net-ms
Test suite passes.
The implementation creates stubs commands for the public oo commands that do initialization on first call. The initialization in turn overwrites the stub implementations with the "real" oo implementation.
Other implementation strategies may be possible (tclIndex, unknown etc.) but need to keep in mind all code paths need to be covered (e.g. initial invocation through namespace path, use of info class commands before any public oo commands etc.
Comments on the proposal, improvements, breakage or even alternative methods appreciated.
8ab9fecd56Rewrite slots in C to get a performance boost. [effa2e2346] check-in2025-08-19 14:49:155abd67e1eaAccelerate definition of [oo::define initialise]. [effa2e2346] check-in2025-08-18 08:56:117aa55929adStart making TclOO faster to initialise [effa2e2346] check-in2025-08-17 21:22:29
Note: the branch has not been tested with TclOO's C API. Looking for an extension that uses it ...
Nice thing.
As for objections or rather theoretical breakage, I think the way you did it is one of the best under these circumstances (without having namespace-based lazy-load facility).
For instance one can rewrite `package require tcl::oo` to force initialisation, so could help for some theoretical case it is really needed (for example like below).
And the possible breakage may be theoretic endless loop in TclOOInitModuleObjCmd, if some command gets injected, mocked-up etc, e. g. by something like this (red is lazy branch):
% rename ::oo::class ::oo::_mocked_class;
% proc ::oo::class args { puts "mockup $args"; tailcall ::oo::_mocked_class {*}$args }
% ::oo::class create A
mockup create A
- too many nested evaluations (infinite loop?)
+ ::A
As for improvements, I'd later provide my attempt.
> Looking for an extension that uses it ...
I think, Itcl would be good candidate (v4.x is based on tclOO).
Thanks for taking a look. I will test the C API with Itcl, which triggered my memory that I think TDBC also uses it. I think it should be possible to protect the infinite recursion with the mockup as well. Will check.
You're welcome.
[6a997a7f19eaa6fa] provides several improvements:
package require tcl::ooforces the initialization through new oo::_init command (so one could circumvent the delayed init issues for mockup, injection etc);- update of mocked-up command by execution (although it wouldn't "fix" every case sane, see below);
- code review, dedeplication, etc.
As for mz previous mocked-up example, it'd work now, and would not enter endless loop anymore, but the mock-up becomes outdated (replaced by mentioned update after lazy load), see the new diff (red is lazy branch):
% rename ::oo::class ::oo::_mocked_class;
% proc ::oo::class args { puts "mockup $args"; tailcall ::oo::_mocked_class {*}$args }
% ::oo::class create A
mockup create A
::A
% ::oo::class create B
- mockup create B
::B
So as one can see, the mock-up is quasi removed here.
And the only solution would be to use package require tcl::oo before mocking-up or injection to force the initialization.
Alternative would to create all this command properly (to point to correct handler).
Or as already said, a new facility able to load NS on demand by first access to it (in any resolver, etc). In my opinion, that would be the only right way.
What is the slow part of TclOO's startup? It might be possible to make some parts lazy without having to tackle everything. (The entanglement between oo::object and oo::class at the bottom is awkward to delay.)
@sebres, thanks for the patch. Will wait on comments from others before merging.
@dkf, the tclOOSetupScript takes up a little over 80% of TclOOInit. I was afraid of breaking up the init sequence in any form as I understand only a bit of code and changing the sequence would both add risk and complicate testing. Right now the sequence itself is more or less unchanged but takes place at a different point. Of course, this is not going to be merged without your blessing no matter other opinions :-)
We can probably make the scripted part quite a bit more efficient.
An initial (not very high effort) analysis indicates that making some parts be either implemented as C code or by lazy loading them should help.
The
oo::singleton,oo::abstractandoo::configurableclasses can likely be handled lazily (except we may need to teach the code how to do that). This would be the biggest win for extracting code from the script.Implementing
oo::define::callbackandoo::define::initialisein C would be reasonable, due to the way they've got alternate names. (Other procedures appear to not take very long; still investigating.)Some of the methods could go to C, except that's probably quite a bit of work.
I've made the callback command (not oo::define::callback; my mistake in my previous comment) faster by implementing it in C; that seems to knock 5-10% off the tclOOSetupScript time.
Still need to do oo::define::initialise along the same model, which is a little bit trickier.
@dkf, even if lazy loaded, it would be worthwhile to make the loading more efficient in any case with C implementation wherever possible.
And that's oo::define::initialise done. It and callback were slow to define because they were using namespace import tricks to map a procedure into a namespace with two names, and those tricks are slow.
Next, I need to re-evaluate what parts of the startup sequence are slow before making another move; this was just some low-hanging fruit.
After the current changes, it looks like about half the cost is the oo::singleton class, though that appears to really be just carrying bytecode compilation costs for the slot machinery, which probably needs to be the next thing converted to C.
Created a branch apn-oo-lazy-init-sbmod-91 for 9.1. This includes trunk changes from dkf that fix the (bogus?) test failure reported in 8a13caa3
Also, a change such as this is probably best left for 9.1, not a 9.0 patch release so planning to close the original branch.
I found another cheap win; using full slot syntax in the init script instead of directing things via the slot's unknown method handler.
Slots are now almost completely done in C; the only bits still in the init script are setting the default operations and hiding the destroy method.
We now need to reevaluate how slow things are. They're probably still not fast enough... but I don't know how much of a loss things are now.
I've moved a lot more of the cost into C and adjusted the code to avoid some of the slower paths in slotted definitions (getting a nice overall saving; the bits that remain are more awkward). I don't know if the changes make this RFE's code less useful or if we should still go ahead with it (and if they still technically apply); I'll investigate.
I'm not convinced that it is a good idea to make info object (and info class, though less critically) be lazily loaded; they're ensembles containing bytecoded commands. There's no scripted components in there.
@dkf, the `info object` and `info class` were made lazy loaded not for performance but for compatibility. I wanted the messages for "info class,object" to be exactly the same as 9.0.2 irrespective of whether any oo calls that would lazy load oo had been made or not. Without loading them on demand, error messages were a little different causing couple of test case failures. If that is not important or there is some other way to achieve the same, that would be fine with me. /Ashok
I'd love to see a rerun of the timings with the current trunk tip; I've different hardware and likely different build options too.
TclOO is now implemented entirely in C except for the package ifneeded calls; there's no satisfactory option there.
The current main branch:
% timerate {interp delete [interp create]}
1339.52 µs/# 746 # 746.54 #/sec 999.280 net-ms
The lazy loading branch
% timerate {interp delete [interp create]}
1199.97 µs/# 833 # 833.35 #/sec 999.577 net-ms
The starting point was 1861 so much of the cost of oo init has been eliminated by the move to C which is great news! Not sure whether it is worth lazy loading to save an additional 10% of initialization cost until further investigation of where else the initialization might be optimized.
On travel on and off for a few weeks so that might be a while.
Thanks! (My testbed's using a little faster hardware, when it decides to turn on the speed-stepping.)
You'll likely need to rethink how you lazily loading the classes, but I'm not sure how yet. Likely by postponing calling MakeAdditionalClasses (in tclOO.c). It's possible that some bits of TclOOInitInfo could be improved, but that's trickier because info object has some fully compiled subcommands. Postponing the creation of all the oo::define stuff (including the oo::Slot class; directly in InitFoundation and in TclOODefineSlots) would be the other place a win might be possible, though I'm not currently sure how.
The main thing to beware of here is that there may be cases where a class (or object) is used in user code before calling it as a command, and before any call to package require. Educating users to change that is likely quite difficult!
Closing as the additional gains with lazy init do not seem worthwhile after moving the oo init code to C.