{"id":21071603,"url":"https://github.com/esrrhs/olua","last_synced_at":"2025-05-16T05:31:40.462Z","repository":{"id":87695676,"uuid":"600309931","full_name":"esrrhs/oLua","owner":"esrrhs","description":"Optimize lua code","archived":false,"fork":false,"pushed_at":"2023-03-11T10:07:16.000Z","size":74,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-03T20:51:14.189Z","etag":null,"topics":["lua","optimization"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/esrrhs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-02-11T04:48:20.000Z","updated_at":"2023-08-19T03:15:49.000Z","dependencies_parsed_at":null,"dependency_job_id":"5ac13cfe-2a5f-455f-b297-d3249a137caa","html_url":"https://github.com/esrrhs/oLua","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/esrrhs%2FoLua","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/esrrhs%2FoLua/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/esrrhs%2FoLua/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/esrrhs%2FoLua/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/esrrhs","download_url":"https://codeload.github.com/esrrhs/oLua/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254474273,"owners_count":22077248,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["lua","optimization"],"created_at":"2024-11-19T18:53:42.926Z","updated_at":"2025-05-16T05:31:40.456Z","avatar_url":"https://github.com/esrrhs.png","language":"Go","readme":"# oLua\n[\u003cimg src=\"https://img.shields.io/github/license/esrrhs/oLua\"\u003e](https://github.com/esrrhs/oLua)\n[\u003cimg src=\"https://img.shields.io/github/languages/top/esrrhs/oLua\"\u003e](https://github.com/esrrhs/oLua)\n[\u003cimg src=\"https://img.shields.io/github/actions/workflow/status/esrrhs/oLua/go.yml?branch=master\"\u003e](https://github.com/esrrhs/oLua/actions)\n\n一个聊胜于无的Lua优化工具。\n\n## 优化点\n- [x] 优化Lua的table访问\n- [x] 优化Lua的table构造\n\n## 优化Lua的table访问\n例如如下代码：\n```lua\na.b = {}\nif a.c then\n    a.b.data1 = \"1\"\n    a.b.data2 = \"2\"\n    a.b.data3 = \"3\"\nend\n```\na.b是一个table，每次访问a.b都会触发一次table的访问，这样会影响性能，所以可以优化为：\n```lua\na.b = {}\nlocal a_b = a.b\nif a.c then\n    a_b.data1 = \"1\"\n    a_b.data2 = \"2\"\n    a_b.data3 = \"3\"\nend\n```\n**注意：这里做了一个假设推断，当对一个a.b赋值构造的table后，就不会再更改a.b为其他table或者其他类型。只针对符合这种假设的推断的代码才能优化。**\n\n## 优化Lua的table构造\n例如如下代码：\n```lua\nlocal a = { a = 1, 2 }\na.b = 1\na[\"c\"] = 2\na[3] = 3\na.d = { e = 4 }\na.d.f = 5\n```\n每次往a中添加元素可能会触发table的扩容，所以可以优化为：\n```lua\nlocal a = {['a']=1, 2, ['b']=1, ['c']=2, [3]=3, ['d']={['e']=4,['f']=5}}\n```\n\n## 使用\n编译：\n```bash\ngo mod tidy\ngo build\n```\n运行，优化单个文件的table访问：\n```bash\n./oLua -input input/table_access.lua -output output/table_access.lua -opt_table_access\n```\n运行，优化单个文件的table构造：\n```bash\n./oLua -input input/table_construct.lua -output output/table_construct.lua -opt_table_construct\n```\n也可以优化目录下的所有文件，原地替换：\n```bash\n./oLua -inputpath input_dir -opt_table_access -opt_table_construct\n```\n\n## 效果\n使用Lua执行input、output目录下的lua文件，看运行所需的时间。\n\n|   | 优化前   | 优化后  |  \n|---|-------|------|\n| table访问 | 19.07 | 11.0 |  \n| table构造 | 3.3   | 1.9  |  \n\n## 其他\n[lua全家桶](https://github.com/esrrhs/lua-family-bucket)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fesrrhs%2Folua","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fesrrhs%2Folua","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fesrrhs%2Folua/lists"}