Skip to content

Commit

Permalink
Issue #1288: In this next round of SFTP transfer optimizations, we ca…
Browse files Browse the repository at this point in the history
…che the initial results of the `dir_check()` function at the start of the transfer. (#1805)

According to flamegraphs, there is a fair amount of CPU time spent in `dir_check()` and related functions, almost all of which is redundant.  Those access checks apply to conditions which do not change over the course of the data transfer.
  • Loading branch information
Castaglia committed May 14, 2024
1 parent c377ff5 commit fb6bd09
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions contrib/mod_sftp/fxp.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,12 @@ struct fxp_handle {
/* For indicating whether the file existed prior to being opened/created. */
int fh_existed;

/* For caching the initial dir_check() results for subsequent READ/WRITE
* requests.
*/
int fh_cached_read_dir_check, fh_have_cached_read_dir_check;
int fh_cached_write_dir_check, fh_have_cached_write_dir_check;

/* For referencing information about the opened file; NOTE THAT THIS MAY
* BE STALE.
*/
Expand Down Expand Up @@ -10468,7 +10474,13 @@ static int fxp_handle_read(struct fxp_packet *fxp) {
cmd2 = fxp_cmd_alloc(fxp->pool, C_RETR, file);
cmd2->cmd_class = CL_READ;

if (!dir_check(fxp->pool, cmd2, G_READ, fxh->fh->fh_path, NULL)) {
if (fxh->fh_have_cached_read_dir_check == FALSE) {
fxh->fh_cached_read_dir_check = dir_check(fxp->pool, cmd2, G_READ,
fxh->fh->fh_path, NULL);
fxh->fh_have_cached_read_dir_check = TRUE;
}

if (!(fxh->fh_cached_read_dir_check)) {
uint32_t status_code = SSH2_FX_PERMISSION_DENIED;

(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
Expand Down Expand Up @@ -13516,7 +13528,13 @@ static int fxp_handle_write(struct fxp_packet *fxp) {
cmd2 = fxp_cmd_alloc(fxp->pool, C_STOR, file);
cmd2->cmd_class = CL_WRITE;

if (!dir_check(fxp->pool, cmd2, G_WRITE, fxh->fh->fh_path, NULL)) {
if (fxh->fh_have_cached_write_dir_check == FALSE) {
fxh->fh_cached_write_dir_check = dir_check(fxp->pool, cmd2, G_WRITE,
fxh->fh->fh_path, NULL);
fxh->fh_have_cached_write_dir_check = TRUE;
}

if (!(fxh->fh_cached_write_dir_check)) {
status_code = SSH2_FX_PERMISSION_DENIED;

(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
Expand Down

0 comments on commit fb6bd09

Please sign in to comment.