eval $foo - does $foo get bytecompiled? 
Author Message
 eval $foo - does $foo get bytecompiled?

If I do something like:

set foo {
set i 0
while { $i < 10000 } {
        puts $i
        incr i

Quote:
}
}

eval $foo

Does the internal representation of foo get twiddled into bytecode?
Or is it reparsing the string every time?  Is there a way to avoid
that?

--
David N. Welton
Free Software: http://www.*-*-*.com/ ~davidw/
   Apache Tcl: http://www.*-*-*.com/
     Personal: http://www.*-*-*.com/ ~davidw/



Sun, 08 Feb 2004 01:41:06 GMT  
 eval $foo - does $foo get bytecompiled?



Quote:

> If I do something like:

> set foo {
> set i 0
> while { $i < 10000 } {
>         puts $i
>         incr i
> }
> }

> eval $foo

> Does the internal representation of foo get twiddled into bytecode?

IIRC no.

Quote:
> Or is it reparsing the string every time?  Is there a way to avoid
> that?

Place the code into a [proc].

--




Sun, 08 Feb 2004 02:30:17 GMT  
 eval $foo - does $foo get bytecompiled?

Quote:
> eval $foo

> Does the internal representation of foo get twiddled into bytecode?
> Or is it reparsing the string every time?  Is there a way to avoid
> that?

No, the arguments of an [eval] are never converted into bytecode.
The usual behavior of [eval] is to [concat] all arguments as strings,
then parse the resulting string as a script and evaluate it.

There is an optimization when $foo is a Tcl_ListObj without a
valid string representation.  In that case, the generation and
re-parsing of the string is skipped, because it must give back
the same Tcl_ListObj.  That list (command) is evaluated directly
using Tcl_EvalObjEx() -- no generation of bytecode at all.

So, construction of $foo as a list can improve performance, but
that has nothing to do with bytecode.

--
| Don Porter          Mathematical and Computational Sciences Division |

| http://math.nist.gov/~DPorter/                                  NIST |
|______________________________________________________________________|



Sun, 08 Feb 2004 03:00:47 GMT  
 eval $foo - does $foo get bytecompiled?
        ...

Quote:
> eval $foo

> Does the internal representation of foo get twiddled into bytecode?
> Or is it reparsing the string every time?  Is there a way to avoid
> that?

It has flipped back and forth, but currently (8.3.3 and 8.4a3) it
doesn't get byte-compiled.  As Andreas noted, if you place it in a
proc, the proc will still get byte-compiled.

--
  Jeff Hobbs                     The Tcl Guy
  Senior Developer               http://www.ActiveState.com/
        Tcl Support and Productivity Solutions



Sun, 08 Feb 2004 03:09:02 GMT  
 eval $foo - does $foo get bytecompiled?

Quote:
>> eval $foo

>> Does the internal representation of foo get twiddled into bytecode?
>> Or is it reparsing the string every time?  Is there a way to avoid
>> that?

> No, the arguments of an [eval] are never converted into bytecode.

Please forget I said that.  In checking my work after posting,
I got a lot of things wrong.

Quote:
> That list (command) is evaluated directly
> using Tcl_EvalObjEx() -- no generation of bytecode at all.

...but if you take a look, Tcl_EvalObjEx does generate bytecode.

Sorry to post first and ask questions later.  Should leave things
like this to Donal, the one who set up the optimization.

--
| Don Porter          Mathematical and Computational Sciences Division |

| http://math.nist.gov/~DPorter/                                  NIST |
|______________________________________________________________________|



Sun, 08 Feb 2004 03:12:48 GMT  
 eval $foo - does $foo get bytecompiled?

Quote:

> If I do something like:

> set foo {
> set i 0
> while { $i < 10000 } {
>         puts $i
>         incr i
> }
> }

> eval $foo

> Does the internal representation of foo get twiddled into bytecode?
> Or is it reparsing the string every time?  Is there a way to avoid
> that?

The right answer is that it is "somewhat bytecompiled"! In the
particular example, what happens when you invoke
      eval $foo
is more or less:

0) call Tcl_EvalObjEx with flags TCL_EVAL_DIRECT
1) parse the value of $foo into words
2) interpret "set i 0" (ie, call Tcl_SetObjCmd)
3) interpret  "while x y" (call Tcl_WhileObjCmd)
        where x is the string {$i < 10000}
                    y is the body of the while, as string
4) interpret the expression x  (call Tcl_ExprObjCmd); if 0 stop
5) parse and bytecompile the string/script y: called by (Tcl_EvalObjEx,
flags 0) only on the first pass
6) execute the bytecodes of y
7) goto 4

The next time you [eval $foo], it starts again from scratch even if $foo
is unchanged.

As said before, avoid all the circus by defining a proc - bytecompiled
once and for ever (more or less too ...)

Miguel



Sun, 08 Feb 2004 05:26:58 GMT  
 eval $foo - does $foo get bytecompiled?

Quote:

> The next time you [eval $foo], it starts again from scratch even if $foo
> is unchanged.

Aha, this is what I wanted to know.  Thanks!

--
David N. Welton
Free Software: http://people.debian.org/~davidw/
   Apache Tcl: http://tcl.apache.org/
     Personal: http://www.efn.org/~davidw/



Sun, 08 Feb 2004 17:51:24 GMT  
 eval $foo - does $foo get bytecompiled?

Quote:

> The next time you [eval $foo], it starts again from scratch even if $foo
> is unchanged.

Theoretically, it is possible to notice the second (or nth) time an
uncompiled unmodified object gets passed into Tcl_EvalObjEx() and
compile it then, but we don't do that at the moment (as you know,
Miguel.)  However, I don't know what proportion of Tcl code would
benefit from such a tactic...

Experiments back in 8.0 days indicated that aggressive compilation
was not the way to go; it slowed much of the core down because of
the quantity of script-fragments that are only executed once (in
callbacks, and particularly Tk binding scripts.)

Donal.
--

-- Actually, come to think of it, I don't think your opponent, your audience,
   or the metropolitan Tokyo area would be in much better shape.



Sun, 08 Feb 2004 19:16:21 GMT  
 eval $foo - does $foo get bytecompiled?

: Experiments back in 8.0 days indicated that aggressive compilation
: was not the way to go; it slowed much of the core down because of
: the quantity of script-fragments that are only executed once (in
: callbacks, and particularly Tk binding scripts.)

Just a side remark - SQL databases like Oracle seems to cope
with bind variables somehow while compiling and optimizing SQL
statements. May be there are some concepts to borrow, which
would allow %-substitution without recompiling?
--
I've run DOOM more in the last few days than I have the last few
months.  I just love debugging ;-)
        -- Linus Torvalds



Tue, 10 Feb 2004 01:43:54 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. A use for "foo = foo()"

2. if foo.__class__ == Foo: #in C?

3. foo.py vs. foo.pyc

4. Difference between $foo and [set foo]

5. $foo == "" or [llength $foo] == 0

6. difference between ::foo::woof and foo::woof?

7. Date format from [file atime/mtime foo.foo]

8. 'foo' = #foo -> true or false?

9. %w(foo) v.s. ['foo']

10. "import foo" and "from foo import bar"

11. is (member # :test 'foo) -> :test #'foo optimization allowed?

12. Shouldn't "expr foo == foo" work in tcl8?

 

 
Powered by phpBB® Forum Software