From e3e7a420deacdf7e0d88d21d1a7b7d5431b33bf1 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Wed, 15 Jun 2011 20:07:10 +0900 Subject: Cache and reuse RpcMethod objects. --- src/RpcMethodFactory.cc | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) (limited to 'src/RpcMethodFactory.cc') diff --git a/src/RpcMethodFactory.cc b/src/RpcMethodFactory.cc index f2e48862..088f2ba9 100644 --- a/src/RpcMethodFactory.cc +++ b/src/RpcMethodFactory.cc @@ -41,8 +41,17 @@ namespace aria2 { namespace rpc { +namespace { +SharedHandle getNoSuchMethod() +{ + static SharedHandle m(new NoSuchMethodRpcMethod()); + return m; +} +} // namespace + +namespace { SharedHandle -RpcMethodFactory::create(const std::string& methodName) +createMethod(const std::string& methodName) { if(methodName == AddUriRpcMethod::getMethodName()) { return SharedHandle(new AddUriRpcMethod()); @@ -118,7 +127,28 @@ RpcMethodFactory::create(const std::string& methodName) } else if(methodName == SystemMulticallRpcMethod::getMethodName()) { return SharedHandle(new SystemMulticallRpcMethod()); } else { - return SharedHandle(new NoSuchMethodRpcMethod()); + return SharedHandle(); + } +} +} // namespace + +std::map > RpcMethodFactory::cache_; + +SharedHandle +RpcMethodFactory::create(const std::string& methodName) +{ + std::map >::const_iterator itr = + cache_.find(methodName); + if(itr == cache_.end()) { + SharedHandle m = createMethod(methodName); + if(m) { + cache_.insert(std::make_pair(methodName, m)); + return m; + } else { + return getNoSuchMethod(); + } + } else { + return (*itr).second; } } -- cgit