Giter Site home page Giter Site logo

Formatter macros about twilight HOT 10 OPEN

 avatar commented on September 22, 2024 1
Formatter macros

from twilight.

Comments (10)

AEnterprise avatar AEnterprise commented on September 22, 2024 1

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.

laralove143 avatar laralove143 commented on September 22, 2024

Thank you for this! It could be part of twilight-util under another feature gate.

from twilight.

 avatar commented on September 22, 2024

I think so too.

from twilight.

AEnterprise avatar AEnterprise commented on September 22, 2024

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.

 avatar commented on September 22, 2024

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.

laralove143 avatar laralove143 commented on September 22, 2024

Would you be able to use a dynamic strings and also inline it with string literals with the same macro though?

from twilight.

 avatar commented on September 22, 2024

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.

 avatar commented on September 22, 2024

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.

Gelbpunkt avatar Gelbpunkt commented on September 22, 2024

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.

 avatar commented on September 22, 2024

Yes, I agree. Thank you for sharing your perspective. So functions are the best approach to these requirements, right?

from twilight.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.