--
You received this message because you are subscribed to the Google Groups "lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lua-l+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/lua-l/CALW7ahxHmpTS71amv50a%3DwcA-_nL4TR%3DufQn42FBvM5f%3DMJNJA%40mail.gmail.com.
--
You received this message because you are subscribed to the Google Groups "lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lua-l+un...@googlegroups.com.
Wouldn't table.move do exactly that right now?
--
You received this message because you are subscribed to the Google Groups "lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lua-l+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/lua-l/CALW7ahxHmpTS71amv50a%3DwcA-_nL4TR%3DufQn42FBvM5f%3DMJNJA%40mail.gmail.com.
It looked like you were referring to a 2nd table when you wrote "to the values in {...}"
- Robert
--
You received this message because you are subscribed to the Google Groups "lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lua-l+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/lua-l/4c4aeacc-84f0-44bf-ba61-b989ea7e5e3dn%40googlegroups.com.
Just write it yourself. I would suggest the following
implementation:
function table.push(t, ...) for i = 1, select("#", ...) do local v = select(i, ...) table.insert(t, v) end end
in a good Lua implementation (includes PUC and LuaJIT), this
should run in amortized linear time in the number of items you
push.
It is not linearithmic time (and even that would often be
acceptable). This is because a length hint is cached, and each
insertion only extends table length by 1;
when computing table length, both LuaJIT and PUC Lua look for a
boundary in the vicinity of the hint (say, K) so they can handle
this case in constant time AFAICT.
On LuaJIT, I would expect this implementation to be practically
efficient. On PUC, you might want to replace the table.insert with
t[#t + 1].
(You might also want to actually pack up the vararg in a table.
For some reason, in my testing a while ago, this makes iterating
varargs faster in PUC, whereas using select is much faster on JIT.
Try it and see.)
I would not do the premature optimization of caching a length
yourself and then adding a bunch of values based on that.
For one, it creates nasal demons with non-sequences unnecessarily.
Just behaving like table.insert would ("filling random holes") is
reasonable IMO.
Overwriting entries is not. (An error would also be reasonable,
however.)
I have not tested this, but judging by the code this might very
well turn out to be a pessimization in some cases:
If you add more values than K, you will trigger a new binary
search - which uses the last length hint as a lower bound, but the
"too large" array capacity as an upper bound.
I believe you might very well end up in linearithmic territory
theoretically, missing the fast path in length.
(Again, measure to see what this practically translates to. It's
very well possible that the multiple length operator applications
end up more expensive.
Vary the number of elements you push in one go; for PUC you'll
want to test numbers > 4, for LuaJIT > 2.)
To wrap it up, I would prefer such a feature not to be added to
Lua for a multitude of reasons:
It is not sufficiently orthogonal to table.insert and table.move.
If you want to add a single element, you use table.insert; if you
want to append multiple elements from another table, you use
table.move.
There are also nitty-gritty details to sort out. Do we prefer to
take the elements to append as a table, or as a vararg; if the
latter, what do we do with nils?
Do we allow overwriting values in a non-sequence, inconsistent
with what table.insert would do? Do we perhaps throw an error when
this happens?
Why should the language authors prescribe this when you can
produce a simple - and in a well-optimized implementation like
LuaJIT, hopefully sufficiently performant - implementation
yourself?
(If you're embedding Lua, this includes the possibility of a C
implementation. Though given LuaJIT, the assertion that a C
implementation will be significantly faster than a Lua
implementation need not be correct.)
To me this seems like it would violate Lua's goal of minimalism.
If you think that performance considerations necessitate this
feature, I would recommend benchmarking to demonstrate this,
preferably on both PUC and LuaJIT.
- Lars
p.s. "It has a rather odd API where the second value is an index
or value depending on the number of arguments. It would be nice to
have a clearer-named API for this."
I can second this. I think what happens when you do the seemingly
harmless 'table.insert(t, ("1"):gsub("a", "b"))' is quite a bit of
a Lua pitfall ;)
--
You received this message because you are subscribed to the Google Groups "lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lua-l+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/lua-l/CALW7ahxHmpTS71amv50a%3DwcA-_nL4TR%3DufQn42FBvM5f%3DMJNJA%40mail.gmail.com.
--
You received this message because you are subscribed to the Google Groups "lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lua-l+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/lua-l/CAB-PuDqycsTtRjheC-2yySNA8N2qQJKmYoWt7aXrKZdM60VeHA%40mail.gmail.com.
You received this message because you are subscribed to a topic in the Google Groups "lua-l" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/lua-l/lIJTvRYQ9xM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to lua-l+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/lua-l/0b37e9ad-eb86-4e36-b078-6cd388dc395d%40Spark.
--
You received this message because you are subscribed to a topic in the Google Groups "lua-l" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/lua-l/lIJTvRYQ9xM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to lua-l+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/lua-l/MG7eqc8Rws59hfneyFVRAofv9NhdjJmX4JYANz7E0ML-Mt59TDd2lj3I4ggfqCU7c0hHX8Q8YhxIbGVWg8I-9BrUiRQ-y21GW0ZHm9vk2kY%3D%40calamity.inc.
--
You received this message because you are subscribed to a topic in the Google Groups "lua-l" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/lua-l/lIJTvRYQ9xM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to lua-l+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/lua-l/20250824151546.GB805571%40arraial.inf.puc-rio.br.
You received this message because you are subscribed to the Google Groups "lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lua-l+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/lua-l/CALW7ahxDokg99XnmLokR94_5nShz9Tg2fhrF5aWPh72vK%2BezbA%40mail.gmail.com.
Just write it yourself. I would suggest the following implementation:
function table.push(t, ...) for i = 1, select("#", ...) do local v = select(i, ...) table.insert(t, v) end end
--
You received this message because you are subscribed to a topic in the Google Groups "lua-l" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/lua-l/lIJTvRYQ9xM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to lua-l+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/lua-l/CAGa7JC22F6mXe7zAV5MCKcVuR6PNa8pcRHKXw4LZ6V%2B0JBDMwg%40mail.gmail.com.
You received this message because you are subscribed to the Google Groups "lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lua-l+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/lua-l/CALW7ahzxQYZB8pp1qZdsAxsk44ehsOKmh-LJEw-%2BRsanycC1LQ%40mail.gmail.com.
On Friday, August 22, 2025 at 7:48:57AM UTC+10 Rett Berg wrote:
> > select("#", ...) can be deceptive. In Lua, 'select' is a regular
> > function. So, in each call, *all* vararg arguments are passed along.
> > If there are N vararg arguments, each of these calls to 'select'
> > has to pass these N arguments. If you loop that N times, the total cost
> > becomes N^2.
> I'm VERY surprised by this, thank you for enlightening me.