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

Implement File.Size #2131

Merged
merged 2 commits into from
Apr 13, 2024
Merged

Conversation

PeakKS
Copy link
Contributor

@PeakKS PeakKS commented Apr 8, 2024

Addresses #1578

Use the engine's method for valve files, and a simple seek/tell for system files.

Test plugin:

#include <sourcemod>

#pragma semicolon 1
#pragma newdecls required

public Plugin myinfo =
{
	name = "File Size Test",
	author = "Peak",
	description = "meow",
	version = SOURCEMOD_VERSION,
	url = "http://www.sourcemod.net/"
};

public void OnPluginStart()
{
    RegServerCmd("sm_fst", Command_FileSizeTest);
}

public Action Command_FileSizeTest(int args)
{
    //Test existing
    int existing = FileSize("addons/sourcemod/plugins/fsztest.smx");
    int existingGame = FileSize("addons/sourcemod/plugins/fsztest.smx", true);

    PrintToServer("Existing Native: %d, Existing Game: %d", existing, existingGame);

    //Test native
    File nf = OpenFile("addons/sourcemod/plugins/fsztest.smx", "r");
    int nv = nf.Size();
    nf.Close();

    //Test game
    File gf = OpenFile("addons/sourcemod/plugins/fsztest.smx", "r", true);
    int game = gf.Size();
    gf.Close();

    PrintToServer("Native: %d, Game: %d", nv, game);
    return Plugin_Handled;
}

@PeakKS
Copy link
Contributor Author

PeakKS commented Apr 9, 2024

Actually just found out about fstat, that might be a better fit than seek/tell.

@PeakKS
Copy link
Contributor Author

PeakKS commented Apr 9, 2024

Set up some benchmarking to compare seek/tell with fstat and noticed that the current FileSize is extremely slow for game paths, had to knock it down to 1000 reps to avoid hanging up the server.

These results are with the seek/tell implementation.

sm_fst
Existing Native: 3482, 1000 reps in 0.001034 s
Existing Game: 3482, 1000 reps in 1.804669 s
Native: 3482, 1000 reps in 0.001555 s
Game: 3482, 1000 reps in 0.000020 s
sm_fst
Existing Native: 3482, 1000 reps in 0.001065 s
Existing Game: 3482, 1000 reps in 2.195328 s
Native: 3482, 1000 reps in 0.000967 s
Game: 3482, 1000 reps in 0.000018 s
sm_fst
Existing Native: 3482, 1000 reps in 0.001097 s
Existing Game: 3482, 1000 reps in 2.220535 s
Native: 3482, 1000 reps in 0.001062 s
Game: 3482, 1000 reps in 0.000023 s

These are the results with the fstat implementation

sm_fst
Existing Native: 3482, 1000 reps in 0.001857 s
Existing Game: 3482, 1000 reps in 1.366881 s
Native: 3482, 1000 reps in 0.000136 s
Game: 3482, 1000 reps in 0.000011 s
sm_fst
Existing Native: 3482, 1000 reps in 0.001016 s
Existing Game: 3482, 1000 reps in 1.365437 s
Native: 3482, 1000 reps in 0.000136 s
Game: 3482, 1000 reps in 0.000012 s
sm_fst
Existing Native: 3482, 1000 reps in 0.001010 s
Existing Game: 3482, 1000 reps in 1.369380 s
Native: 3482, 1000 reps in 0.000136 s
Game: 3482, 1000 reps in 0.000011 s

fstat seems a good amount faster so I will be updating to use that.

@psychonic psychonic merged commit 5c507cc into alliedmodders:master Apr 13, 2024
4 checks passed
@psychonic
Copy link
Member

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants