[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: No way to get uservalue count from API; intended?
- From: Sergey Zakharchenko <doublef.mobile@...>
- Date: Tue, 18 Aug 2020 22:34:24 +0400
Roberto Ierusalimschy <firstname.lastname@example.org>:
> > Is there a reason not to include the ability to read the number of
> > uservalues associated with a userdata
> > (lua_getiuservalue()/lua_setiuservalue()) into the API?
> No specific reason except the usual (to avoid bloating the API with
> functions seldom used.)
Speaking of which, it looks like luaL_checkudata/luaL_testudata are
quite often used, but also quite inefficient (if the rest of the
method runs fast), which seems to cause the somewhat strange benchmark
results I sent earlier. It looks up a metatable by name, then it gets
the actual one, compares them and pops both. Tuple-related benchmarks
look much better if I drop the metatable check.
A more efficient lower-level API could be, say, lua_testmetatable(L,
idx_tested, idx_of_metatable) which would return e.g. 1 if the value
at the idx_tested index has the metatable supplied at idx_of_metatable
(all done via pointer comparisons, no metatable pushing/popping), 0 if
it has no metatable at all, and -1 if it has a metatable but it's
different from the supplied one. The method would have the expected
metatable as an upvalue.