Re: Dynamic Typing Efficiency

Calum Grant <calumg@onetel.com>
13 May 2005 18:01:01 -0400

          From comp.compilers

Related articles
[4 earlier articles]
Re: Dynamic Typing Efficiency loic@fejoz.net (Yermat) (2005-05-09)
Re: Dynamic Typing Efficiency mailbox@dmitry-kazakov.de (Dmitry A. Kazakov) (2005-05-09)
Re: Dynamic Typing Efficiency eliotm@pacbell.net (Eliot Miranda) (2005-05-09)
Re: Dynamic Typing Efficiency jeffrey.kenton@comcast.net (Jeff Kenton) (2005-05-09)
Re: Dynamic Typing Efficiency clearm@comcast.net (2005-05-13)
Re: Dynamic Typing Efficiency alexc@TheWorld.com (Alex Colvin) (2005-05-13)
Re: Dynamic Typing Efficiency calumg@onetel.com (Calum Grant) (2005-05-13)
Re: Dynamic Typing Efficiency angray@beeb.net (Aaron Gray) (2005-05-16)
Re: Dynamic Typing Efficiency DrDiettrich@compuserve.de (Hans-Peter Diettrich) (2005-05-18)
Re: Dynamic Typing Efficiency jeffrey.kenton@comcast.net (Jeff Kenton) (2005-05-22)
| List of all articles for this month |

From: Calum Grant <calumg@onetel.com>
Newsgroups: comp.compilers
Date: 13 May 2005 18:01:01 -0400
Organization: ntl Cablemodem News Service
References: 05-05-041
Keywords: interpreter, types
Posted-Date: 13 May 2005 18:01:01 EDT

clearm@comcast.net wrote:
> I am writing a small, python-like scripting language that uses dynamic
> typing. I am also writing a stack-based virtual machine to go with
> it.
>
> I have read many papers and posts about improving the efficiency of
> VMs - for example by using threaded dispatch instead of switch
> statements.
>
> The problem I have is that dynamic typing seems to be extremely
> inefficient when you have a large number of types. For example, if I
> have integers, doubles, strings, booleans, and various compound types,
> then an ADD instruction would have to look like this:
>
> [snip]
>
> switch INSTRUCTION:
> {
>
> case ADD:
>
> if (operand1->type == INTEGER && operand2->type == INTEGER)
> {
> value.i = operand1->val.i + operand2->val.i;
> type = INTEGER;
> }
> else if (operand1->type == INTEGER && operand2->type == DOUBLE)
> {
> value.d = (double)operand1->val.i + operand2->val.d;
> type = DOUBLE;
> }
> else if (operand1->type == DOUBLE && operand2->type == INTEGER)
> {
> value.d = operand1->val.d + (double)operand2->val.i;
> type = DOUBLE;
> }
> else if (operand1->type == DOUBLE && operand2->type == DOUBLE)
> {
> value.d = operand1->val.d + operand2->val.i;
> type = DOUBLE;
> }
> else if... blah blah blah
>
> break;
>
> case SUB: ...
>
> ...
> }
>
> [snip]


I'm thinking that you could do something sneaky with C++ template
meta-programming. You could construct a template


template<int type1, int type2>
struct plus
{
          static variant eval(variant, variant) ...
};


and statically generate the appropriate function. Then you use
meta-programming again to generate an array of these things, and put
them into a large jump-table.


Disclaimer: I haven't tried this....


Calum




--
Fast object persistence in C++ http://lightwave2.com/persist
A fast malloc/new replacement http://lightwave2.com
Home http://visula.org/calum


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.