Giter Site home page Giter Site logo

fast-string-format's Introduction

Fast String Format

Build and Test NuGet

This library is designed for a future version of DarkRift Networking to provide very fast custom formatted log lines.

Background

The Idea

DarkRift will soon allow users to configure the output of their logs. For example, one could create a custom log format of the time, log type, and message using something like:

{Timestamp:yyyy-MM-dd hh:mm:ss} | {LogType}: {Message}

However if DarkRift allows customisation of log lines there will be a serious performance impact when writing logs unless such customisation is done in compiled code. Therefore, this library has been built to facilitate fast string formatting capable of formatting logs lines at speed.

There are of course already string formatting options in C#. string.Format is great but it's slow and not easy to understand if given only the format string with no parameters. String interpolation is wonderful to read and performs well but it's unfortunately limited to compile time configuration. Fast String Format is designed to give similar readability and performance as String Interpolation but with a similar runtime flexibility as string.Format.

The Implementation

Fast String Format works by using Expressions to compile the format string into a Func<string, T>. When that Func is run, the entire string operation happens using calls to the very fast string.Concat method.

Current Benchmarks

Fast String Format is currently benchmarked against a number of different string formatting methods using BenchmarkNet.

|              Method |       Mean |     Error |     StdDev |     Median |
|-------------------- |-----------:|----------:|-----------:|-----------:|
|              Concat |   963.6 ns |  96.16 ns |   246.5 ns |   900.0 ns |
|            Addition | 1,002.5 ns | 172.08 ns |   450.3 ns |   900.0 ns |
|       StringBuilder | 2,031.6 ns | 510.40 ns | 1,464.4 ns | 1,200.0 ns |
|        StringFormat | 2,184.0 ns | 468.28 ns | 1,380.7 ns | 1,400.0 ns |
| StringInterpolation | 1,550.5 ns | 381.32 ns | 1,081.7 ns | 1,000.0 ns |
|    FastStringFormat | 1,823.7 ns | 471.92 ns | 1,369.1 ns | 1,100.0 ns |

Usage

FastStringFormat is available as a NuGet package!

A simple usage would look like:

var formatter = new FastStringFormatCompiler().Compile<DataObject>("{Forename} {Surname} was born {DOB::yyyy-MM-dd}. It is {LikesCats} that he liked cats.");

var data = new DataObject(Forename = "Steve", Surname = "Irwin", DOB = new DateTime(1962, 9, 22), LikesCats = true);

Console.WriteLine(formatter(data));

This would print out:

Steve Irwin was born 1962-09-22. It is True that he liked cats.

Of course, this example is trivial. Since the parsing is entirely within the Compile method, the real power of Fast String Format is found when a single format string needs to be applied to thousands of different data objects.

Using Custom Formatters

When creating the FastStringFormatCompiler object, one can pass in a custom IFormatStringParser which allows custom parsing of format strings. By default the DefaultFormatParser is used which currently provides similar functionality to String Interpolation.

FastStringFormatCompiler compiler = new FastStringFormatCompiler(myCustomFormatStringParser);

fast-string-format's People

Contributors

jamjar00 avatar

Stargazers

Vassil Kovatchev avatar Man Sun avatar Panupun  avatar Endar avatar Enrico Speranza avatar

Watchers

James Cloos avatar  avatar

fast-string-format's Issues

Create documentation

Documentation should be created using docfx or imilar (GitHub pages? Wiki?) containing:

  • API reference
  • Quickstart
  • Creating a parser
    at minimum.

Allow object heirachy traversal

Given a format string such as {Time.Year} or {User.Names.Forename} these should traverse the object graph and pull out the correct values.

Code for this exists in the fast-object-filter library which will be released soon.

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.