Re: Performance implications of large FFI constants

  • From: Mike Pall <mike-1209@xxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Wed, 26 Sep 2012 12:05:58 +0200

William Adams wrote:
> I want to be able to access the thing easily
> I want the access to be as fast as possible
> I want to avoid collisions with constants that might be defined elsewhere.

  local ffi = require("ffi")
  ffi.cdef[[
  struct whatever {
    static const int FOO = 42;
    static const int BAR = 99;
  };
  ]]
  local W = ffi.new("struct whatever")
  print(W.FOO, W.BAR)

Yes, these are compiled to actual constants, just like enum
constants (and unlike Lua table fields). Except that enum constants
are global in C, even if defined inside a struct. Note that it's
slightly more efficient to use an instance of the dummy struct (W)
as a namespace than using the ctype.

In many actual use cases you already have a struct to deal with.
Then it's quite natural to define the related constants there, too.

--Mike

Other related posts: