Skip to content
Tcl Source Code History Of Ticket effa2e2346
Login

History Of Ticket effa2e2346

Artifacts Associated With Ticket effa2e2346

  1. Ticket change [f7adab6025] (rid 244128) by apnadkarni on 2025-08-16 05:36:29:

    1. assignee initialized to: "nobody"
    2. closer initialized to: "nobody"
    3. cmimetype initialized to: "text/x-markdown"
    4. comment initialized to:
      Tcl interp creation time more than doubled in 9.{0,1,2} compared to 8.6 as reported in [62019f8aa9](https://core.tcl-lang.org/tcl/tktview/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](https://core.tcl-lang.org/tcl/timeline?r=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.
      
    5. is_private initialized to: "0"
    6. login: "apnadkarni"
    7. priority initialized to: "5 Medium"
    8. private_contact initialized to: "9546e9877f1a8899e25a89082561ee694e7fb402"
    9. resolution initialized to: "None"
    10. severity initialized to: "Minor"
    11. status initialized to: "Open"
    12. submitter initialized to: "apnadkarni"
    13. subsystem initialized to: "- New Builtin Commands"
    14. title initialized to:
      RFE - lazy loading of TclOO for faster interp creation
      
    15. type initialized to: "Bug"
  2. Ticket change [1cdbe4504f] (rid 244133) by apnadkarni on 2025-08-16 10:39:19:

    1. icomment:
      Note: the branch has not been tested with TclOO's C API. Looking for an extension that uses it ...
      
    2. login: "apnadkarni"
    3. mimetype: "text/plain"
  3. Ticket change [2b193da220] (rid 244134) by sebres on 2025-08-16 13:06:20:

    1. icomment:
      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 (<b style="color:red">red</b> is lazy branch):
      <code><pre>
        % 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
      <b style="color:red">- too many nested evaluations (infinite loop?)</b>
      <b style="color:green">+ ::A</b>
      </pre></code>
      
      As for improvements, I'd later provide my attempt.
      
      <blockquote><i>> Looking for an extension that uses it ...</i></blockquote>
      
      I think, Itcl would be good candidate (v4.x is based on tclOO).
      
    2. login: "sebres"
    3. mimetype: "text/x-fossil-wiki"
  4. Ticket change [4688a0dded] (rid 244136) by apnadkarni on 2025-08-16 13:42:17:

    1. icomment:
      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.
      
    2. login: "apnadkarni"
    3. mimetype: "text/plain"
  5. Ticket change [a37f40ea3c] (rid 244139) by sebres on 2025-08-16 14:30:26:

    1. icomment:
      You're welcome.
      
      [6a997a7f19eaa6fa] provides several improvements:
      
        *  <code>package require tcl::oo</code> forces 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 (<b style="color:red">red</b> is lazy branch):
      <code><pre>
        % 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
      <b style="color:red">- mockup create B</b>
        ::B
      </pre></code>
      
      So as one can see, the mock-up is quasi removed here.
      And the only solution would be to use <code>package require tcl::oo</code> before mocking-up or injection to force the initialization.
      
      Alternative would to create all this command properly (to point to correct handler).<br/>
      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.
      
    2. login: "sebres"
    3. mimetype: "text/x-fossil-wiki"
  6. Ticket change [7ced7d2f94] (rid 244159) by dkf on 2025-08-17 00:22:40:

    1. icomment:
      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.)
      
    2. login: "dkf"
    3. mimetype: "text/x-markdown"
  7. Ticket change [3364b01323] (rid 244160) by apnadkarni on 2025-08-17 03:01:22:

    1. icomment:
      @sebres, thanks for the patch. Will wait on comments from others before merging.
      
    2. login: "apnadkarni"
    3. mimetype: "text/plain"
  8. Ticket change [3459b7a297] (rid 244161) by apnadkarni on 2025-08-17 03:06:49:

    1. icomment:
      @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 :-)
      
    2. login: "apnadkarni"
    3. mimetype: "text/plain"
  9. Ticket change [20d3dc4c52] (rid 244242) by dkf on 2025-08-17 17:07:29:

    1. icomment:
      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::abstract` and `oo::configurable` classes 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::callback` and `oo::define::initialise` in 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.
      
    2. login: "dkf"
    3. mimetype: "text/x-markdown"
  10. Ticket change [0e2680aeed] (rid 244256) by dkf on 2025-08-17 21:29:15:

    1. icomment:
      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.
      
    2. login: "dkf"
    3. mimetype: "text/x-markdown"
  11. Ticket change [cea79d3bb1] (rid 244258) by apnadkarni on 2025-08-18 05:50:56:

    1. icomment:
      @dkf, even if lazy loaded, it would be worthwhile to make the loading more efficient in any case with C implementation wherever possible.
      
    2. login: "apnadkarni"
    3. mimetype: "text/plain"
  12. Ticket change [5422936e86] (rid 244277) by dkf on 2025-08-18 08:59:30:

    1. icomment:
      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.
      
    2. login: "dkf"
    3. mimetype: "text/x-markdown"
  13. Ticket change [f20fcb1ab7] (rid 244285) by dkf on 2025-08-18 12:16:25:

    1. icomment:
      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.
      
    2. login: "dkf"
    3. mimetype: "text/x-markdown"
  14. Ticket change [105b462ccd] (rid 244299) by apnadkarni on 2025-08-18 13:27:18:

    1. icomment:
      Created a branch [apn-oo-lazy-init-sbmod-91](https://core.tcl-lang.org/tcl/timeline?r=apn-oo-lazy-init-sbmod-91) for 9.1. This includes trunk changes from dkf that fix the (bogus?) test failure reported in [8a13caa3](https://core.tcl-lang.org/tcl/info/8a13caa31dd8e3a5)
      
      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.
      
    2. login: "apnadkarni"
    3. mimetype: "text/x-markdown"
  15. Ticket change [4acba533b3] (rid 244335) by dkf on 2025-08-19 09:02:22:

    1. icomment:
      I found another cheap win; using full slot syntax in the init script instead of directing things via the slot's unknown method handler.
      
    2. login: "dkf"
    3. mimetype: "text/x-markdown"
  16. Ticket change [176ea31476] (rid 244360) by dkf on 2025-08-19 14:52:35:

    1. icomment:
      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.
      
    2. login: "dkf"
    3. mimetype: "text/x-markdown"
  17. Ticket change [fbc90b5d67] (rid 244512) by dkf on 2025-08-23 08:34:22:

    1. icomment:
      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.
      
    2. login: "dkf"
    3. mimetype: "text/x-markdown"
  18. Ticket change [40d3a39892] (rid 244513) by dkf on 2025-08-23 12:54:03:

    1. icomment:
      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.
      
    2. login: "dkf"
    3. mimetype: "text/x-markdown"
  19. Ticket change [8aafbb8aaf] (rid 244514) by apnadkarni on 2025-08-24 03:07:35:

    1. icomment:
      @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
      
    2. login: "apnadkarni"
    3. mimetype: "text/plain"
  20. Ticket change [96d206103f] (rid 244818) by dkf on 2025-08-31 10:55:07:

    1. icomment:
      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.
      
    2. login: "dkf"
    3. mimetype: "text/x-markdown"
  21. Ticket change [dae53c4dda] (rid 244834) by apnadkarni on 2025-09-01 03:01:28:

    1. icomment:
      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.
      
    2. login: "apnadkarni"
    3. mimetype: "text/x-markdown"
  22. Ticket change [c1bf1cf4ff] (rid 244867) by dkf on 2025-09-01 08:45:17:

    1. icomment:
      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!
      
    2. login: "dkf"
    3. mimetype: "text/x-markdown"
  23. Ticket change [149658fbaf] (rid 252002) by apnadkarni on 2026-03-11 05:59:44:

    1. closedate changed to: "2461110.7498241784"
    2. closer changed to: "apnadkarni"
    3. icomment:
      Closing as the additional gains with lazy init do not seem worthwhile after moving the oo init code to C.
      
    4. login: "apnadkarni"
    5. mimetype: "text/plain"
    6. resolution changed to: "Fixed"
    7. status changed to: "Closed"