Giter Club home page Giter Club logo

haxe-delegates's Introduction

haxe-delegates

Small utility for Haxe that wraps function types as Delegate objects. Enforces type-strictness and can be faster on some platforms (~40% faster for non-inlined functions and ~200% faster for inlined on the hxcpp target). Very useful within performance-critical code where delegates/functions need to be called. Delegate building is much more expensive.

How to use it:

Import haxe.delegates.* and simply use the delegate type with a function type as your parameter, like so:

var delegate : Delegate<(Int, Int)->Int> = DelegateBuilder.from(myFunction);

public function myFunction(a : Int, b : Int) : Int {
    return a + b;
}

or,

var delegate : Delegate<(Int, Int)->Int> = DelegateBuilder.from((a : Int, b : Int) -> (a+b : Int)));

For inlined functions, it is important to use the cast notation (x : Type). This is not for casting the value, instead it is used by the builder to resolve the return type without the need to determine it implicitly.

What's actually going on?

These macros generate concrete types at compile time. As such, they're not designed to replace Haxe function types (which are light-weight and useful when used during start-up). The trade-off when using delegates for speed is a slightly bigger source-code.

Why?

Some Haxe targets are required to unbox value types from function types when called (which then need to be garbage collected). By encapsulating function calls, we can avoid this unboxing and even enforce type-strictness on function type parameters. The following example will fail:

var delegate : Delegate<Dynamic->Void> = DelegateBuilder.from(myFunction);

// Must have Dynamic as first argument...
public function myFunction(a : Int) : Void {
    trace(a);
}

Future work:

It may be possible to wrap delegates that get generated by their respective builders into singletons, or caching, to reduce any performace impacts during run-time if one wishes to create delegates frequently.

haxe-delegates's People

Contributors

paulsgcross avatar

Watchers

 avatar

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.