Tcl Source Code

View Ticket
Login
Ticket UUID: 3302370
Title: Performance gap between [global] and [::]
Type: Bug Created on: 2011-05-15 10:00:16
Submitter: ferrieux Assigned to: msofer
Subsystem: 07. Variables Severity:
Priority: 5 Medium Last modified: 2011-09-24 05:38:49
Status: Open Closed by:
Resolution: None Closed on:
Version: None
Description:
Using [global] instead of [::] in a proc gives a 7x speedup.

% proc f {} {global zzz;for {set i 0} {$i<10000000} {incr i} {incr zzz}}
% proc g {} {for {set i 0} {$i<10000000} {incr i} {incr ::zzz}}
% time f
871320 microseconds per iteration
% time g

This can be traced to the different bytecode ops used: in the tight loop, the [global] case uses a compiled local (upvarred to the global), while the FQN uses loadScalarStk repeatedly.

I would have assumed that the intrep cacheing of the resolved varname in that literal value "::zzz" would save most of the work, but something else must be increasing the cost...
6123508 microseconds per iteration
User Comments:
ferrieux added on 2011-05-15 17:05:00:
Hmm, sorry for the bad layout. Copypasta glitch. Summary of timings:
 
 global ->  871320 microseconds
 ::         -> 6123508 microseconds

dkf added on 2011-05-25 05:28:58:
I vaguely remember there being a whole bunch of experimentation with variable reference caching in... was it 8.4? I think after 8.4.2 things started to be turned off again because of odd bugs in corner cases, but that's quite a long time ago now so memory might be off.

What follows is "thinking with my mouth open" (except its typed ;-))

It should be possible to cache a Var* in a fully-qualified name, but there's a possibility that it could cause problems with [unset] semantics. Plus the name needs to be interpreter-bound anyway. Ick!

msofer added on 2011-05-25 05:36:27:
This CAN be solved easily enough: when the compiler detects a FQN, it can create a same-named (with qualifiers!) variable in the LVT that is auto-upvared to the global on proc entry. The body then always refers to the one in the LVT.

The same thing could actually be done for [eval] and [uplevel] bodies, even for non qualified names. But in that case it probably requires other changes elsewhere: the whole core assumes that only proc (and lambda, and oo) bodies have an LVT, this assumption could be hidden somewhere unexpected.

Another solution would be to code a fast-path for FQN variables in TEBC, reading the Tcl_Obj intrep. Currently only LVT variables have a fast inlined path, the others call the functions in tclVar.c

ferrieux added on 2011-05-25 14:09:41:
The first solution, even if not generalized to uplevel+eval bodies for now, sounds like an optimum to me... FWIW
(Wow ! an FQN-named compiled local -- brilliant)

ferrieux added on 2011-09-24 05:38:49:
Miguel, is this doable ?