🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.tcl
Article #134

Re: Speed and Memory with upvar

#134
From: Ralf Fassel
Date: Fri, 20 Nov 2026 10:59
66 lines
3347 bytes
* meshparts <alexandru.dadalau@meshparts.de>
| I actually did a mistake in my original post: My experience is that
| the *second* procedure is faster than the first one, although is using
| upvar as an additional command. But I cannot explain this behaviour.

Most probably your procs modify the list, in which case (as Rich and
Harald already pointed out) a copy of the list will be created in
myproc1 (pass-by-shared-obj), whereas myproc2 (pass-by-name-reference)
operates directly on the list in the callers context (no copy required).

| So I suppose the memory consumption will be also identical in both
| cases.

Not if the list is modified in the proc:

- myproc1 needs to create a copy of the passed list to modifiy it
  (shared Tcl_Obj*).  A copy of the list requires creating [llength $l]
  new Tcl_Obj*, copy the pointers and IncrRef for each pointer.  That
  naturally scales with the number of elements in the list.

- myproc2 operates directly on the list in the callers context,
  non-shared Tcl_Obj*, no need to copy.

  # two lists, short and long
  % set l3 [list 1 2 3]
  % set l1m [lrepeat 1000000 1]

  # modify the list in the proc
  % proc myproc1 {l} { lset l 2 y }
  % proc myproc2 {lname} { upvar $lname l; lset l 2 x }

  # proc1 short list
    % time {myproc1 $l3} 100
    1.23 microseconds per iteration
    % time {myproc1 $l3} 100
    1.22 microseconds per iteration
    % time {myproc1 $l3} 100
    1.23 microseconds per iteration

  # proc1 long list
    % time {myproc1 $l1m} 100
    3670.52 microseconds per iteration
    % time {myproc1 $l1m} 100
    3217.89 microseconds per iteration
    % time {myproc1 $l1m} 100
    3418.59 microseconds per iteration

  # proc2 short list
    % time {myproc2 l3} 100
    1.4 microseconds per iteration
    % time {myproc2 l3} 100
    1.35 microseconds per iteration
    % time {myproc2 l3} 100
    1.35 microseconds per iteration

  # proc2 long list
    % time {myproc2 l1m} 100
    1.34 microseconds per iteration
    % time {myproc2 l1m} 100
    1.35 microseconds per iteration
    % time {myproc2 l1m} 100
    1.36 microseconds per iteration


HTH
R'
Technical Details
Message-ID: <ygay0an4vkg.fsf@akutech.de>
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
References: <11dn5cd$4ibt$1@tota-refugium.de> <yga33sw6avh.fsf@akutech.de> <11dn9te$4mfp$1@tota-refugium.de>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: Ralf Fassel <ralfixx@gmx.de>
Newsgroups: comp.lang.tcl
Subject: Re: Speed and Memory with upvar
Date: Fri, 20 Nov 2026 11:59:59 +0100
Organization: A noiseless patient Spider
Lines: 66
Message-ID: <ygay0an4vkg.fsf@akutech.de>
References: <11dn5cd$4ibt$1@tota-refugium.de> <yga33sw6avh.fsf@akutech.de>
	<11dn9te$4mfp$1@tota-refugium.de>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Date: Fri, 20 Nov 2026 11:00:00 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="3244414"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19sLPn7e8YT843jI4Wfm54s"; posting-host="0dd4c21b3b45f1e759a1702a0e2e533f"
User-Agent: Gnus/5.13 (Gnus v5.13)
Cancel-Lock: sha1:j380w6HVgngykLm6sGtTSI4XjPc=
	sha1:jyjLywzl4ttBlX8tn6xCzyedFOk= sha256:e+IoxtREBGrf/YexXgjthwGISNXsPwNmEI7rCrGaUP8=
	sha1:8/x6N92yccl6yJtiU1K8krfLfqo= sha256:ycey/b30Nwm5QUgVVYLCkiYijoSeJ6Bq9+QV2nhpCG4=