Skip to main content
gamedev.net
🔒 Locked

library design problem - coding twice

Started by RolandofGilead 2 replies 250+ views
Original Post
RolandofGilead
RolandofGilead
One part of the library is a script engine that can access all of the functions of the library. Perhaps if I were using function pointers this wouldn''t be a problem, but the script engine is using a switch statement(because everything I''ve learned so far leads me to believe the switch statement would be faster than calling a function). The problem, this would mean the library is now twice as large, the code that''s in the function definition and a copy of that code within the switch statement. This also means I have the unpleasant job of making sure that each of the versions of the code are the same. Besides that, since all that code is going to be in the switch statement, aren''t I in danger of making a function too big for the stack or one that takes too long to set up so that it would be faster just to use the function pointers? Would inlining the functions work for reducing the maintenance problem? Even if that works, is there a way to reduce the size and avoid stack problems?
Sand_Hawk
Sand_Hawk
What''s wrong with calling functions? The time needed to call a function shouldn''t be enormous. I don''t think it''s even worth it to cut & paste every function inside the switch. Just call the function, they were invented for this purpose.

Sand Hawk

----------------
-Earth is 98% full. Please delete anybody you can.


My Site
----------------(Inspired by Pouya)
Cyberdrek
Cyberdrek
quote:
Original post by RolandofGilead
One part of the library is a script engine that can access all of the functions of the library.

Perhaps if I were using function pointers this wouldn''t be a problem, but the script engine is using a switch statement(because everything I''ve learned so far leads me to believe the switch statement would be faster than calling a function).

The problem, this would mean the library is now twice as large, the code that''s in the function definition and a copy of that code within the switch statement. This also means I have the unpleasant job of making sure that each of the versions of the code are the same. Besides that, since all that code is going to be in the switch statement, aren''t I in danger of making a function too big for the stack or one that takes too long to set up so that it would be faster just to use the function pointers?

Would inlining the functions work for reducing the maintenance problem? Even if that works, is there a way to reduce the size and avoid stack problems?


Maybe I''m really dumb or something but how would using a switch replace using a function? Basicly, a switch is simply another way of doing "ifs"( sorta ). As for scripting engine, I''m just now completing mine and I can tell you that there aren''t any switches in it. Can''t use switch with std::string. I also don''t use any function pointers. The code looks weird to me but it works well. I might turn my engine into an article for this site once I''m done.



"DaHjajmajQa''jajHeghmeH!"

Cyberdrek
danielc@iquebec.com
Founder
Laval Linux

/(bb|[^b]{2})/ that is the Question -- ThinkGeek.com
Hash Bang Slash bin Slash Bash -- #!/bin/bash
[Cyberdrek | ]
RolandofGilead
RolandofGilead
quote:
Original post by Cyberdrek
...how would using a switch replace using a function?...


function version:
You make the script into bytecode, not interpreted(which is what I assume you''re doing since you''re using std::string) and use the opcode as an index into an array of function pointers.

switch:
Make the script bytecode and use the values of the opcode for the case values in a switch statement(the compiler then makes the switch into a jump table which removes the overhead of calling a function). Then put what would belong in a function from the above example into the case.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.