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

Reduced bits used by net.Read/WritePlayer based on maxplayers #2078

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

Conversation

mgetJane
Copy link
Contributor

it is impossible for maxplayers to be changed without a server restart

also, i removed all the extra stuff from net.ReadPlayer because net.ReadUInt will never return a falsy value

also, changing net.WritePlayer into a single line is probably a little unnecessary, but imo it's harmless and i'll change it back if it's unwanted

@robotboy655 robotboy655 added the Enhancement The pull request enhances current functionality. label Apr 24, 2024
else
net.WriteUInt( ply:EntIndex(), 8 )
end
local maxplayers_bits = math.ceil( math.log( 1 + game.MaxPlayers() ) / math.log( 2 ) )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

math.log's second parameter is base, division by $\ln 2$ is unnecessary.

Suggested change
local maxplayers_bits = math.ceil( math.log( 1 + game.MaxPlayers() ) / math.log( 2 ) )
local maxplayers_bits = math.ceil( math.log( 1 + game.MaxPlayers(), 2 ) )

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i remember an issue encountered by @2048khz-gachi-rmx where math.log(x, 2) gives an incorrect result while math.log(x) / math.log(2) gives the correct result

all their messages are gone from the gmod discord so i don't remember the exact details

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.ps BRANCH, ("%.17f"):format(math.log(4096, 2))
-- [Server]
"unknown"
"12.00000000000000000"
-- [Client]
"x86-64"
"11.99999999999999822"
.ps BRANCH, math.floor(math.log(4096, 2))
-- [Server]
"unknown"
12
-- [Client]
"x86-64"
11

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/LuaJIT/LuaJIT/commit/ad03eba715e5e0d0bd0f3c0ddef4b8f5bbb0c626 is the commit that caused this.
It seems that for values in $\left[ 1 .. 256 \right]$ it only causes issues when rounding down. Given that the function rounds up, it shouldn't be a problem.

> for p = 1, 8 do local log = math.log(2^p, 2) print(("%.17f / %i"):format(log, math.ceil(log))) end...
1.00000000000000000 / 1
2.00000000000000000 / 2
2.99999999999999956 / 3
4.00000000000000000 / 4
5.00000000000000000 / 5
5.99999999999999911 / 6
6.99999999999999911 / 7
8.00000000000000000 / 8

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you check the output of this? i'm on linux and the issue only happens on 64-bit windows

lua_run_cl for i=1,128 do if math.ceil(math.log(1+i,2))~=math.ceil(math.log(1+i)/math.log(2))then return print"bad"end end print"good"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

] version
Protocol version 24
Exe version 2023.06.28 (garrysmod)
Exe build: 06:17:43 Apr  1 2024 (9246) (4000)
GMod version 2024.04.24, branch: x86-64, multicore: 1
Windows 64bit
] lua_run_cl for i=1,128 do if math.ceil(math.log(1+i,2))~=math.ceil(math.log(1+i)/math.log(2))then return print"bad"end end print"good"
good

Copy link
Contributor Author

@mgetJane mgetJane Apr 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still feels a little iffy to use something that's kinda unreliable

ttt has the bitsRequired function which is 100% reliable (and actually faster according to @figardo)

that function could be a good addition to either the math or util table

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
5 participants