Comments (10)
just strapping an #[inline] on the function will cause it to be inlined, thus making it functionally equivalent to the macro, but much more readable imo
macro's are useful but i don't think they offer any benefit here over just a macro
from twilight.
Thank you for this! It could be part of twilight-util
under another feature gate.
from twilight.
I think so too.
from twilight.
why would this need to be a macro and not just a regular function (that would probably be inlined anyways by the compiler)?
from twilight.
why would this need to be a macro and not just a regular function (that would probably be inlined anyways by the compiler)?
IMHO;
- Macros are evaluated at compile time, which means that the formatting code is processed before the program runs. In contrast, regular functions are executed at runtime. This compile-time evaluation can lead to performance benefits, especially when formatting is used extensively, as macros can potentially be inlined by the compiler, resulting in more efficient code.
- When formatting is performed through functions, there is an overhead associated with function calls. While modern compilers can inline small functions, macros ensure that there is no function call overhead at all, which can be important for frequently used formatting operations in Discord bot messages.
If there's any point where I'm mistaken, I would appreciate it if you could correct me. Thank you for reading.
from twilight.
Would you be able to use a dynamic strings and also inline it with string literals with the same macro though?
from twilight.
Would you be able to use a dynamic strings and also inline it with string literals with the same macro though?
There are ways to achieve similar functionality using Rust macros by using token manipulation and code generation. You can generate code or tokens based on certain input, but this is not the same as creating truly dynamic strings within the macro.
macro_rules! bold {
($content:expr) => {
format!("**{}**", $content)
};
}
from twilight.
But yes, I agree that macros tend to be less general-purpose, and functions can often serve as more versatile and maintainable alternatives. Functions are typically more suitable for common tasks and can be used in a wider range of scenarios. However, macros still have their place in Rust for code generation, metaprogramming, and other compile-time tasks where their capabilities shine. I still believe that macros are useful and can be used effectively.
from twilight.
why would this need to be a macro and not just a regular function (that would probably be inlined anyways by the compiler)?
IMHO;
* Macros are evaluated at compile time, which means that the formatting code is processed before the program runs. In contrast, regular functions are executed at runtime. This compile-time evaluation can lead to performance benefits, especially when formatting is used extensively, as macros can potentially be inlined by the compiler, resulting in more efficient code. * When formatting is performed through functions, there is an overhead associated with function calls. While modern compilers can inline small functions, macros ensure that there is no function call overhead at all, which can be important for frequently used formatting operations in Discord bot messages.
If there's any point where I'm mistaken, I would appreciate it if you could correct me. Thank you for reading.
You will get zero performance benefits for using format!
. The std::fmt
APIs are much more overhead than just prepending and appending the string. It also won't be evaluated at compile time since the APIs aren't const. What you probably want is to use concat!
.
from twilight.
Yes, I agree. Thank you for sharing your perspective. So functions are the best approach to these requirements, right?
from twilight.
Related Issues (20)
- Invalid UpdateRolePositions body
- Expose HeaderMap in addition to HeaderIter for twilight-http responses HOT 9
- Unable to deserialize gateway ready event due to missing fields HOT 15
- [RFC] Flatten `PartialX` Types with `X`
- Support Media Channels
- Deserialization errors for members with joined_at=null HOT 1
- `Message.referenced_message` field doesn't exist or is `null` ambiguous
- Creating a ban does not delete any messages
- Investigate static HTTP form boundary
- Editing a guild member (changing voice-channel) hangs indefinitely HOT 6
- Error message is unhelpful, and this doesn't require an error HOT 1
- Make `ListBody<T>` lazily deserializable HOT 4
- GuildCreate does not have guild_scheduled_event HOT 1
- Allow setting an initial resume_url on the Shard
- Seemingly unintentional limitations to update_guild_channel_positions HOT 1
- twilight-http: `Client::update_role_positions` uses wrong structure format
- Update lints used in Twilight
- Duplicate field error on Interaction when the `entitlements` field is present HOT 2
- Use Rustfmt Nightly HOT 6
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 twilight.