jacwright / RestServer

A PHP REST server for providing a very light-weight REST API.
MIT License
512 stars 194 forks source link

Lazy loading without generating a map #8

Open jk opened 12 years ago

jk commented 12 years ago

I knew that this day would come: Generating a map of the whole API and all its classes is quite cumbersome and isn't a good decision when it comes down to performance. When you also don't have any cache at hand it gets pretty sluggish on every request.

My intention by opening this issue is a refactoring on that topic, so that the class facilitates an autoloader which only loads the class which matches the requested URL. So the first advantage would be that you don't need so much memory by loading any possible class into memory. And on the other side, you have the advantage that you don't have to generate the map for all classes. Just for the requested one and its methods/actions.

Using an autoloader will change the behavior of the RestServer class, so that it loads every class it's able to load. I suggest that there will be a mechanism which prevents certain classes from being autoloaded. Perhaps a class comment like:

/**
 * @noUse
 */
class DoNotLoadThisClass {}

I consider this as a draft, any opinions on that proposal?

jacwright commented 12 years ago

When I created RestServer I knew there would be performance downsides at some point. I think an autoloader is a good idea. With slight modifications you could put off the parsing of the url mappings to happen only if the baseUrl is matched. $server->addClass('UserAPI', '/users') would be stored but the class wouldn't be loaded or the map generated unless the URL started with '/users'.

I'm happy to merge in any decent solutions you come up with.

jacwright commented 12 years ago

Also, not sure if this project fits your needs, but https://github.com/Luracast/Restler is a fork from RestServer and has moved on quite a bit. Development is more active than RestServer.