Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache C interface #300

Merged
merged 1 commit into from
Oct 31, 2014
Merged

Cache C interface #300

merged 1 commit into from
Oct 31, 2014

Conversation

dpino
Copy link
Contributor

@dpino dpino commented Oct 29, 2014

It's a common idiom in Lua to cache the C interface:

local ffi = require('ffi')
local C = ffi.C

Caching the C namespace allows faster access to C functions. The codebase of Snabbswitch uses this idiom but there are a few places which don't, even when the C namespace was previously cached.

@dpino
Copy link
Contributor Author

dpino commented Oct 29, 2014

Performance notice: the JIT compiler specializes to the identity of namespace objects and to the strings used to index it. This effectively turns function cdata objects into constants. It's not useful and actually counter-productive to explicitly cache these function objects, e.g. local strlen = ffi.C.strlen. OTOH it is useful to cache the namespace itself, e.g. local C = ffi.C.

Source: http://luajit.org/ext_ffi_semantics.html

lukego added a commit that referenced this pull request Oct 31, 2014
@lukego lukego merged commit 76f14c9 into snabbco:master Oct 31, 2014
@dpino dpino deleted the cache-C branch November 17, 2014 16:16
dpino added a commit to dpino/snabb that referenced this pull request Apr 29, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants