Git Product home page Git Product logo

fast_fmt's Introduction

Fast fmt

Faster, more flexible and more correct alternative to core::fmt (AKA std::fmt)

Warning

This is WIP. Some APIs may change, some may lack documentation, others may be broken. Information in this README (especially benchmarks) may be misleading. Contributions are highly appreciated!

I don't promise to work on this much for a while!

Why is this faster?

  • Lack of trait objects allows compiler to optimize better.
  • Use of size_hint allows writers to e.g. pre-allocate large enough buffer.
  • Use of never type for errors coming from Write allows to optimize-out error checks.

Why more flexible?

Instead of multiple traits like Display, Debug, ... this crate defines a single Fmt<S> which allows you to implement multiple different strategies, even your own. One possible use case is to implement Fmt<Localizer> to enable localization of your application.

Why more correct?

Instead of returning Err(()) on failed writes it returns appropriate types. It can even be Void to represent writers that can never fail (e.g. std::string::String).

How fast is it in practice?

The crate provides a very simple benchmark:

test bench::bench_core_fmt ... bench:         122 ns/iter (+/- 24)
test bench::bench_fast_fmt ... bench:          26 ns/iter (+/- 1)

It's consistently more than four times faster!

What to improve?

Roughly sorted by priority.

  • Documentation
  • Macros - ideally provide the same experience as core does.
  • More strategies
  • More impls (especially Fmt for primitives)
  • Bridge with core::fmt
  • Bridge with T: Iterator<char> + Clone?
  • Integrate with genio and provide encoders for different encodings.
  • Support for trait objects if someone wants them
  • Transformers (e.g. char escaping)
  • Asynchronous formatting maybe?
  • PR against core
  • Deprecate core::fmt

Last two are jokes.

fast_fmt's People

Contributors

japaric avatar kixunil avatar seeekr avatar sollyucko avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

fast_fmt's Issues

Remove the `void` dependency

As discussed elsewhere the void dependency has caused issues with no_std before. It is also (in my subjective opinion) just kind of gross, there should be no need to have crates for language features.

Fortunately there is a ! type now which does the same thing.

Needs benches

This crate features no benchmarks, yet claims to be "fast". Microbenchmarks may not be optimal, but it would be a start. They should also be set up to compare against core: after all, there's a race to win. :^)

Thoughts on macros

I've found this crate quite nice, and I think it could be even nicer with macro support. Thanks to macros 1.1, it's possible to actually implement this now.

My current thoughts on design would be a fmt_merge!() macro analogous to core::fmt's format_args!(). It would produce a single value of some type which can be easily formatted. For the type, I would imagine sticking together a ton of Chains with another ChangeFormat<F, T> struct wrapping T which implements Fmt<Display> when T: Fmt<F>. Initially I would just think to support {}/{0} for Display, and add support for named arguments and other kinds of formatting later.

The other idea I've had for this would be to support deriving Fmt with a similar pattern. If the merging macro is implemented using syn and the proc-macro infrastructure, it could easily be reused to build something like the following:

#[derive(Fmt)]
#[fmt(
    // format for a specific formatting type
    "fast_fmt::Display",
    // exact same syntax as `fmt_merge!()`:
    "Hello, {}! It's a {} day.",
    self.world,
    if self.good { "good" } else { "bad" }
)]
struct DayDescription {
    world: &'static str,
    good: bool,
}

I don't necessarily have the time to build this now, but I wanted to throw some design ideas out there. If I were to work on this in the future, do you think it would be a good addition to the fast_fmt crate? If there isn't already an in-the-works macro implementation, that is.

The library is pretty nice without these, but they could make it even easier to use.

Thanks!

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.