Skip to content
This repository was archived by the owner on Mar 25, 2018. It is now read-only.
This repository was archived by the owner on Mar 25, 2018. It is now read-only.

Native FFI #3

Description

@rauchg

Essentially supporting https://github.com/node-ffi/node-ffi directly
(require('ffi'))

Activity

  1. seppo0010 commented on Mar 2, 2015

    @seppo0010

    +1

  2. TooTallNate commented on Mar 2, 2015

    @TooTallNate

    Definitely would eliminate a lot of the pain in users having to compile native addons (especially on Windows).

    👍

  3. chrisdickinson commented on Mar 2, 2015

    @chrisdickinson

    +1 on this. Might be worthwhile to ask FFI maintainers from Python or Ruby if there's any pitfalls we should be aware of when bringing this into core?

  4. rauchg commented on Mar 2, 2015

    @rauchg
    Author

    @chrisdickinson agreed. We could CC them on this thread even :D

  5. Qard commented on Mar 2, 2015

    @Qard
    Member

    👍

  6. chrisdickinson commented on Mar 2, 2015

    @chrisdickinson

    cc @arigo, who maintains Python's cffi package, and @tduehr, who maintains Ruby's ffi package. Are there any pitfalls we should be aware of before incorporating an FFI package into io.js core?

  7. piscisaureus commented on Mar 2, 2015

    @piscisaureus

    (y) although can we put the feature behind a flag for some time?

  8. mikeal commented on Mar 2, 2015

    @mikeal
    Contributor

    is this something we should get in to V8?

    On Monday, March 2, 2015, Bert Belder notifications@github.com wrote:

    (y) although can we put the feature behind a flag for some time?

    —
    Reply to this email directly or view it on GitHub
    #3 (comment).

  9. Qard commented on Mar 2, 2015

    @Qard
    Member

    How would FFI interact with V8, which is heavily template-based? I've only ever seen C FFI before. Is it even possible, or would we need that C abstraction over V8 that has been suggested several times before?

  10. TooTallNate commented on Mar 2, 2015

    @TooTallNate

    How would FFI interact with V8

    The same way it interacts with libuv (through C code)?

  11. tduehr commented on Mar 3, 2015

    @tduehr

    @Qard you have it backward... V8 would be interacting with FFI.

    I'm not aware of any implementation pitfalls, mostly because i didn't create mri or JRuby's FFI implementations. I can help with runtime pitfalls somewhat... You're going to want to give users a way to make objects backed by C memory structures to act as much as possible like any other JS object for allocation/deallocation purposes. For all other intents and purposes, interacting directly with the C call should carry C semantics. It should be up the the ffi user to implement more idomatic wrappers for their chosen library not a FFI implementation concern.

  12. arigo commented on Mar 3, 2015

    @arigo

    Hi there. Do you have anything more concrete to point me to? All I see is a vague question "what are the pitfalls of incorporating a Foreign Function Interface into xxx"... and I don't know what xxx is.

  13. Fishrock123 commented on Jun 15, 2015

    @Fishrock123
  14. arigo commented on Jun 15, 2015

    @arigo

    Probably not a useful note, but since you asked me: as maintainer of Python CFFI, I feel that if the goal is to call C code, then the interface should strive for the same simplicity as C itself. Ideally, if a feature doesn't exist in C, it is not part of CFFI either (reading big- or little-endian numbers comes to mind).

    CFFI comes with a minimal API: the user writes ffi.cdef("some C declarations") to declare functions and types (using directly the C syntax), and then he uses ffi.new("foo_t *") or ffi.new("int[42]") to make a new C structure or array. You have a few operations on the "pointer" objects, like indexing and field access---and calls if they are pointers to functions. That's it for the core. This approach is completely different from Python's ctypes, even though it basically allows the same results.

    (Another difference is the "API mode", a separate mode which produces C source code that must be compiled into a Python extension module; this has the huge advantage that you're working with C code at the level of C, including stuff like macros and structures-with-at-least-a-field-called-x; not at the level of the ABI. But this might not sell so well in the context of JS, for all I know.)

  15. tduehr commented on Jun 15, 2015

    @tduehr

    I concur. I often get requests for features based on Ruby features which often cannot be duplicated for the general case of the FFI/C layer. Where ever I make an api decision, I default to C behavior. This is an interface to a lower level language, it should act like that lower level language. You're making it easier to use a C library and remove the need for writing the library hooks in C. For Ruby, this means the resulting gem is more portable between engine implementations and even versions of the same engine. This also means the user, the one implementing a C library wrapper using FFI, does not have to learn the internals of Ruby. The user has the information required to make their api align with language semantics so, C semantics should be expected by users.

  16. 4 remaining items

  17. trevnorris commented on Jun 25, 2015

    @trevnorris

    I can't see it being significantly slower than v8 bindings

    It is significantly slower than using a native module.

  18. rvagg commented on Jun 25, 2015

    @rvagg
    Member

    I think the hardware group attempted doing something with it for serialport (or similar) but bailed because it was uber-slow, at least I have a vague memory of a recent issue about it

  19. tduehr commented on Jun 25, 2015

    @tduehr

    It will be slower than using the native API.

    The big wins are:
    Easy module development
    Modules built on FFI are immune to internal API changes
    corollary - Modules built on FFI already work on other engines with FFI support

    FFI in Ruby is often used as a stepping stone. A gem will be built with FFI initially, if it proves popular, the author or someone else will write native versions for the main ruby engines. Usually, the author writes a native MRI version then someone else writes a Java version for JRuby.

  20. arigo commented on Jun 26, 2015

    @arigo

    With a good JIT, a FFI can be a lot faster than any alternative. This is the case about PyPy but I'm sure V8 and others are similar. The JIT can basically generate raw C calls; that's the best performance possible. By contrast, needing to go through some custom C API layer to access the objects is slower (because it needs a stable API that presents objects in a way that doesn't change all the time) and it cannot be optimized by the JIT (for example, if you make a temporary "integer" object, the JIT usually removes it; but if it escapes through the C API layer, it can't).

  21. kobalicek commented on Jun 26, 2015

    @kobalicek

    @arigo Without having FFI directly in V8 I think even JIT won't help you. You will probably not be able to extract data from V8 allocated objects without doing it in C++. I remember there was a discussion in V8 about this. It wasn't strictly about FFI, but it was about accessing DOM properties directly by V8.

  22. trevnorris commented on Jun 26, 2015

    @trevnorris

    A call into C++ from JS in recent V8 is a bit under 30 ns. Converting well formed arguments is in the single digit ns. So even a non trivial call to a native library going through the V8 API won't take more than 50 ns of overhead. The most expensive part will probably be the converting of return value(s) to JS objects. Which would exist in the FFI regardless.

    I've written tests to verify this, and even doing an operation as simple as summing values from a typed array with as few as 100 items is faster when passed to the native side.

  23. DemiMarie commented on Oct 1, 2015

    @DemiMarie

    @arigo makes an excellent point. The key point to remember is that this requires that the FFI be tightly integrated with the JIT compiler -- not be a separate module. The JIT needs special knowledge of the FFI built in to it. This is the case for PyPy and LuaJIT. That means that this feature request really belongs with the V8 project, since only they can provide a performant FFI.

    LuaJIT is a good example of how fast a good FFI can be. In LuaJIT, FFI data types are used routinely, because they are faster to access than Lua tables and take up less memory. (LuaJIT is also an example of how to write a fast VM in general, but that is beside the point).

  24. lygstate commented on Oct 28, 2015

    @lygstate

    This is a really important feature to makes nodejs to communicate with the OS and other frequently used APIs.

  25. YurySolovyov commented on Oct 17, 2016

    @YurySolovyov

    With the raise of frameworks like NW.js and Electron, also increased the need to call into OS APIs, because it is often enough what most desktop apps need to do anyway.

  26. PaulBGD commented on Dec 6, 2016

    @PaulBGD

    It has been a while, has there been any work on this?

  27. ofrobots commented on Dec 6, 2016

    @ofrobots

    @matthewloring and I have been working on prototyping a fast dynamic FFI on top of TurboFan JIT in V8. We're proving it out at the moment, and once we are confident it can work, we will share more details publicly.

  28. SuhairZain commented on Apr 3, 2017

    @SuhairZain

    Hi @ofrobots, any updates on the FFI you're working on?

  29. ebraminio commented on Apr 14, 2017

    @ebraminio

    This seems progressed internally here and then here. There are some related patches on the project which some are also merged but it seem the project is not finalized yet. I hope to see this soon.

  30. lygstate commented on Nov 29, 2017

    @lygstate

    So there is no progress still?

  31. DemiMarie commented on Nov 29, 2017

    @DemiMarie
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions