Soni "They/Them" L. <fakedme@gmail.com> 于2019年5月5日周日 上午11:05写道: > > I often write code like: > > local foo = t.foo > if not foo then foo = whatever t.foo = foo end > -- use foo here > > but ideally I'd like to write it like: > > if not t.foo then t.foo = whatever end > local foo = t.foo > > and still have the same performance (or better). > > does lua do any optimizations related to this? > I have had tried to do this optimization many years ago (2012), See the attachment for the patch, it's based on lua 5.2.1 . I cached the last index of table hash part in Proto struct , so that it can use it directly on next access ( for OP_GETTABUP, OP_GETTABLE, OP_SELF) . But I haven't seen the better performance. I guess lua's hash table is good enough for short string keys, > 70% short string keys are on the main index according to my experience. -- http://blog.codingnow.com
Attachment:
patch
Description: Binary data