Re: Dynamic Typing Efficiency

glen herrmannsfeldt <gah@ugcs.caltech.edu>
8 May 2005 22:57:45 -0400

          From comp.compilers

Related articles
Dynamic Typing Efficiency clearm@comcast.net (2005-05-08)
Re: Dynamic Typing Efficiency bobduff@shell01.TheWorld.com (Robert A Duff) (2005-05-08)
Re: Dynamic Typing Efficiency luke@iogopro.co.uk (Luke McCarthy) (2005-05-08)
Re: Dynamic Typing Efficiency gah@ugcs.caltech.edu (glen herrmannsfeldt) (2005-05-08)
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)
[4 later articles]
| List of all articles for this month |
From: glen herrmannsfeldt <gah@ugcs.caltech.edu>
Newsgroups: comp.compilers
Date: 8 May 2005 22:57:45 -0400
Organization: Compilers Central
References: 05-05-041
Keywords: types, interpreter
Posted-Date: 08 May 2005 22:57:45 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 of switch statement with lots of ifs inside for all pairs of
combinations of data types.)


> In other words I have to have several if statements for each
> combination of types (or perhaps a switch-case). All of these
> conditional brances seems rather inefficient (expecially on a 31-stage
> pipelined Prescott) but I can't think of another way to do it.


I believe most processors have an efficient implementation of switch
that doesn't use an if tree. The one that I am used to seeing is an
indexed branch to a branch instruction, or indexed load of an address
from a table and branch. This is, of course, bad for branch
prediction but once the branch is made it should be fast. An IF tree
could have many branch prediction failures.


For your case, I would say that you could instead use a switch inside
each case based on the two data types. You could also make one gigantic
switch based on both operation and data type. If you have 16 data
types, maybe something like:


      switch(INSTRUCTION*256 + operand1->type*16 + operand2->type)


There would then only be one branch prediction failure.


More usual for most machines and languages is to have type conversion
operations and arithmetic operations on a single data type. One switch
could convert the operands as appropriate, and a second to operate on
the converted operands.


-- glen


Post a followup to this message

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