Comments (1)
Quick update, this modified code is slightly faster than the above code. 1. Returns a reference to std::function and reads a reference of std::function
from std::any
. 2. Use a std::string_view
as key instead of std::string
. But this feature requires C++20
struct BackendBase
{
std::map<std::string, std::any> funcs_;
template<typename Class, typename Return, typename ...Args>
void expose_method(const std::string& name, Return(Class::*func)(Args...))
{
funcs_[name] = std::function<Return(Args...)>([this, func](Args ... args) {
(static_cast<Class*>(this)->*func)(args...);
});
}
template <typename Func>
const std::function<Func>& get_method(const std::string_view name)
{
return std::any_cast<const std::function<Func>&>(funcs_.at(name));
}
};
This is not the final design tho. I still want to remove the use of std::function and use a custom, more specific class. Hopefully the method wrapper can ensure no allocation what so ever.
Update: The current lambda needs exactly 24 bytes of storage space on 64bit systems. Which is exactly how much space libc++'s std::function
have for small functions. No heap allocations. We are good in that regards :)
from etaler.
Related Issues (20)
- Iteration over tensor HOT 3
- Modulizating Etaler (C++20 modules)
- Indexing with negative indices failed HOT 1
- Rework of the view/indexing system
- Fix toHost<bool>() HOT 1
- Granularize error/pre/post-condition checking machenisim HOT 1
- Fix typos in variable names and comments HOT 1
- Move the visualizer to a seprate repository
- New serealization system
- Replace std::decay with std::remove_cvref when C++20 is avaliable
- Build fail on latest MSVC 2019 caused by et_check (possibe MSVC bug) HOT 1
- Setting up CI
- Tests failing when running on OpenCL backend, POCL w/ CPU
- Support 0D tensors
- AMD GPU support over Mesa + Clover HOT 1
- SmallVector::back() emits error under testing in a Debug build
- OpenCL program cache HOT 1
- Works on HaikuOS
- GridCellEncoder problems HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from etaler.