Skip to content

Implement File.Size#2131

Merged
psychonic merged 2 commits intoalliedmodders:masterfrom
PeakKS:filesize-methodmap
Apr 13, 2024
Merged

Implement File.Size#2131
psychonic merged 2 commits intoalliedmodders:masterfrom
PeakKS:filesize-methodmap

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
@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.

2 participants