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

Improve SortedPairs even more #2032

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

Be1zebub
Copy link
Contributor

@Be1zebub Be1zebub commented Nov 30, 2023

PR #2015 is good, but we can make it much better.

Improvements:

  • table.insert( tbl, val ) > tbl[ #tbl + 1 ] = val
  • next( keys, i ) > keys [ i ]

benchmarks:

  • brandonsturgeon's version is 11.23% better than default
  • my version is 36% better than default
  • or 27.92% better than brandonsturgeon's

bench src: https://gist.github.com/Be1zebub/4b90791fc7ab2d89a18c7a93da143095

bench results:

benchmarks have been tested on a default branch, in p2p server.

pr Facepunch#2015 is good, but we can make it much better.

improvments:
table.insert( tbl, val ) > tbl[ #tbl + 1 ] = val
next( keys, i ) > keys [ i ]

benchmarks:
brandonsturgeon's version is 10.57% better then default.
my version is 35.87% better then default
or 28.29% better then brandonsturgeon's.

bench src: https://gist.github.com/Be1zebub/4b90791fc7ab2d89a18c7a93da143095
@robotboy655 robotboy655 added the Enhancement The pull request enhances current functionality. label Nov 30, 2023
@Kefta
Copy link
Contributor

Kefta commented Nov 30, 2023

Does caching #keys and incrementing a local improve perf for large tables?

@Be1zebub
Copy link
Contributor Author

Be1zebub commented Nov 30, 2023

@Kefta do you mean replace

local keys = {}

for k in pairs( tbl ) do
	keys[ #keys + 1 ] = k
end

with

local keys, i = {}, 0

for k in pairs( tbl ) do
	i = i + 1
	keys[ i ] = k
end

# doesnt actually calculate the amount, lua just get the length from the cache.

at least jit store len in cache, idk how it works in lua puc - but I has read about how jit store and process tables

but because #tbl is C-call, its 30% slower than manual calculated number usage.
we can get 2-5% more perf with this microoptimization.
Its not significant as next & table.insert replacements - because next & table.insert make some work inside.

bench results:
tested with this table:

now its 37% faster than default.

not very significant, but why not
+ 2-5% more perf (compared to the improved version)
@Heyter
Copy link

Heyter commented Mar 14, 2024

:)

-- SortedPairs:
	-- sum = 0.168
	-- avg = 0.00168
	-- median = 0.0010000000000012
-- SortedPairs new:
	-- sum = 0.082000000000001
	-- avg = 0.00082000000000001
	-- median = 0.00099999999999945
do
	local pairs_aux = pairs({})
	local index, keys = 0, {}
	local sort = table.sort
	local function fn1(a, b) return a > b end
	local function fn2(a, b) return a < b end
	function SortedPairs(tbl, desc)
		keys, index = {}, 0

		for k in pairs_aux, tbl do
			index = index + 1
			keys[index] = k
		end

		sort(keys, desc and fn1 or fn2)
		return pairs_aux, tbl
	end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement The pull request enhances current functionality.
Projects
None yet
4 participants