# ways to cache code

This topic is 4867 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am wondering if there a way to do something like this in c++. What I want to do works similar to how a display list in opengl works, but I would like to be able to do this for a general case.
BeginCachingCode();
a bunch of lines of code blah
...
...
...
...
EndCachingCode();


; Then later you could do this...
RunCachedCode();



##### Share on other sites
Sounds like a job for a function.

##### Share on other sites
Quote:
 Original post by SiCraneSounds like a job for a function.

What do you mean? I am trying to keep things in this case as general as possible without forcing users to be constrained to defining a specific callback function, if anyone is familiar with opengl display lists then this should make sense.

##### Share on other sites
I was thinking that BeginCaching could perhaps store the memory address of the next instruction, and that EndCaching could mark the memory address of the previous instruction as an endpoint to use when RunCachedCode is called. I am not sure if something like this is possible.

The method I describe here is not caching, but merely just stores and start and end addresses of the instructions in the code block.

##### Share on other sites
Quote:
Original post by Khaos Dragon
Quote:
 Original post by SiCraneSounds like a job for a function.

What do you mean? I am trying to keep things in this case as general as possible without forcing users to be constrained to defining a specific callback function

Quote:
 if anyone is familiar with opengl display lists then this should make sense.

Ask yourself - what is it that makes gl's display lists faster than the alternative? What compromises are made in order to acheive this speed up? Then ask yourself (and please tell me because I'm baffled) how you'd apply this as a C++ optimisation. Because as far as I can see it, all you've asked for is:

BeginCachingCode();// magical speed voodoo goes hereEndCachingCode();

Which doesn't help anyone.

##### Share on other sites
There is no language-specific feature that lets you do this in C++. There are higher-level constructs, like closures with bound parameters, that let you do this in various dynamic languages (i e LISP, LUA).

If you really want to do that (and sometimes, like in software rasterizers, you really do), then you can generate assembly code into a chunk of memory and jump to it. You have to make sure you allocate that memory in the way that the OS needs to mark that memory as executable, though, or you'll just get an exception.

JIT bytecode interpreters, and various pixel format converters and scanline rasterizers have been known to do this.

##### Share on other sites
Quote:
Original post by OrangyTang
Quote:
Original post by Khaos Dragon
Quote:
 Original post by SiCraneSounds like a job for a function.

What do you mean? I am trying to keep things in this case as general as possible without forcing users to be constrained to defining a specific callback function

Quote:
 if anyone is familiar with opengl display lists then this should make sense.

Ask yourself - what is it that makes gl's display lists faster than the alternative? What compromises are made in order to acheive this speed up? Then ask yourself (and please tell me because I'm baffled) how you'd apply this as a C++ optimisation. Because as far as I can see it, all you've asked for is:

BeginCachingCode();// magical speed voodoo goes hereEndCachingCode();

Which doesn't help anyone.

Hmm I suppose I should have been more general, my aim is not to necessarily cache code but mainly to be able to store it in some form. I am not doing this for speed, but for organizational reasons.

##### Share on other sites
Quote:
 Original post by hplus0603There is no language-specific feature that lets you do this in C++. There are higher-level constructs, like closures with bound parameters, that let you do this in various dynamic languages (i e LISP, LUA).If you really want to do that (and sometimes, like in software rasterizers, you really do), then you can generate assembly code into a chunk of memory and jump to it. You have to make sure you allocate that memory in the way that the OS needs to mark that memory as executable, though, or you'll just get an exception.JIT bytecode interpreters, and various pixel format converters and scanline rasterizers have been known to do this.

I have not used assembly much. However is it possible within assembly to get the address of the current instruction pointer?

##### Share on other sites
Quote:
 Original post by Khaos DragonHmm I suppose I should have been more general, my aim is not to necessarily cache code but mainly to be able to store it in some form. I am not doing this for speed, but for organizational reasons.

'Organisational reasons' is so horribly vauge that it could mean anything, but at a guess I'd suggest you look into function pointers and/or the listener pattern.

##### Share on other sites
I currently am using function pointers, but this requires me to force the user to define a function with specific class parameters where I like him to be able to pass whatever paramteres possible.

I suppose a function pointer with a void pointer as an argument would be good here?

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 14
• 23
• 12
• 12
• 11
• ### Forum Statistics

• Total Topics
634782
• Total Posts
3019266
×