[fpc-devel] Optimization theory

J. Gareth Moreton gareth at moreton-family.com
Mon Jun 25 05:23:00 CEST 2018

 Actually you're right - that is the fastest method (it's written O(1), by
the way, if it doesn't depend on n).  The drawback is that the large
majority of instructions don't have such an optimisation procedure, so the
table  would be extremely sparse.  There are 1,109 individual
instructions on x86 that FPC currently supports.  Come to think of it,
even that is not too bad as far as memory goes - you're looking at 8,872
bytes on i386 (4 bytes for the instruction and 4 bytes for a pointer) and
17,744 bytes on x86-64 (8 bytes for each field), both are potentially
cacheable on the CPU hence won't be too costly in regards to looking it
up.  Not sure why I overlooked that one.  Thanks Martin.

 Gareth aka. Kit

 On Mon 25/06/18 00:18 , Martin fpc at mfriebe.de sent:
 On 24/06/2018 23:28, J. Gareth Moreton wrote: 
 > On paper, a binary search is the fastest method to locate a data 
 > pointer associated with a key (in this case, an opcode). 

 Forgive me for stepping in, given that I hardly looked at your work, nor 
 the particular part of fpc. 
 But the fastest way should be a hash look up (O(n) = 1) 

 I might have missed something, so maybe I am completely out of line.... 

 You have an enum. This could be used as a hash key (no need to 
 calculate, just take the raw value, and you have a perfect hash). 

 All you need to do is once build the hash table. 
   lookup =  Array[tasmop] of CodePointer; 
 in unit initialization set the codepointer for the known opcodes. 

 Then you do not need to search. just 
    codepointer := lookup[taicpu(p).opcode] 
 fpc-devel maillist - fpc-devel at lists.freepascal.org [1] 


[1] mailto:fpc-devel at lists.freepascal.org
[2] http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20180625/09e1f9b9/attachment.html>

More information about the fpc-devel mailing list