Tcl Source Code

View Ticket
Login
Ticket UUID: 02d5d65d70adab975fce0c87800140999ab80f3e
Title: File subsystem commands and operations suffer from unneeded (but expensive) path normalization
Type: Bug Created on: 2024-09-13 11:35:11
Submitter: sebres Assigned to: sebres
Subsystem: 37. File System Severity: Important
Priority: 5 Medium Last modified: 2024-12-02 14:39:42
Status: Closed Closed by: sebres
Resolution: Fixed Closed on: 2024-12-02 14:39:42
Version: any
Description:

It looks like more than 20 years (for instance since 2003 in [da7df285d16e6c30]), tcl's file related functions and commands suffer from unneeded path normalization and therefore unnecessarily slower than they may be.

This is almost invisible if one is testing with constant path (literals), because they hold then normalized path in internal representation, but very well bothering on variable path objects, what is mostly the normal case, if paths gets generated, read from somewhere, anyway dynamically constructed every time before their usage.
But the path normalization is very expensive, especially it is noticeable on hosts with rude corporate "protection" software like AV/DLP/Endpoint-Protection/Whatever-else-bothers-dev-to-work.

Just note that particularly by windows the function TclpObjNormalizePath doing GetFileAttributesExW + WinReadLinkDirectory + FindFirstFileW (basically glob)!

Below are 2 examples illustrating this behaviour on such a host. Note that _determ_fn always creates new object, and string trim is used only to force this, as well as the costs of such path generation is almost to neglect (0.01%), additionally the T:-drive used here is a RAM-disk:

  % proc _determ_fn {i} { string trimright "T:/TestCLU/a7/40vbp43/vu/3g/616q4bet2lf53lf88vo5q2.ent " }
  % timerate { set fn [_determ_fn 123] }; # costs of deterministic path generation
  0.457163 µs/# 2081613 # 2187404 #/sec 951.636 net-ms
  % set fn [_determ_fn 123]; timerate { close [open $fn] }; # 1 time normalization
  1411.53 µs/# 709 # 708.45 #/sec 1000.774 net-ms
  % timerate { set fn [_determ_fn 123]; close [open $fn] }; # every time normalization
  3060.91 µs/# 327 # 326.70 #/sec 1000.917 net-ms

The cost of such (unneeded here!) normalization overhead - 1650 µs, so more than 50%. The same is valid for another, much faster file commands like file exists, file attributes, file stat etc. However there the overhead may be up to 95% under normal circumstances or up to 1000% on hosts with rude corporate infrastructure or for UNC-paths over network (samba, cifs, nfs, etc):

  % set fn [_determ_fn 123]; timerate { file exists $fn }; # 1 time normalization
  4.277398 µs/# 116259 # 233786 #/sec 497.286 net-ms
  % timerate { set fn [_determ_fn 123]; file exists $fn }; # every time normalization
  1505.03 µs/# 665 # 664.44 #/sec 1000.845 net-ms

I would like to emphasise once again - the constant literal paths is rather an exception, because the normal case is usually a dynamic path object that is created or read from somewhere instantly before its usage: corresponds the second (red) case in both illustrations above.

I already fixed this and will try to provide a solution soon together with performance regression-test coverage.

User Comments:
sebres added on 2024-09-13 13:25:12:

Fixed in branch bug-02d5d65d70adab97 for 8.6. All tests pass (win/unix), there is almost no overhead visible anymore in performance regressions tests.

I'll merge it later if no objections follow.


dgp added on 2024-09-13 18:46:59:
If someone can get to it before I can, please check this revision
doesn't cause troubles with trofs.

https://math.nist.gov/~DPorter/tcltk/trofs/

sebres added on 2024-09-13 21:39:41:

Why should it, Don?
I hope the fix affects only absolute paths (in native FS) or relative paths if cwd is native, otherwise I guess tcl should already have caused troubles previously too. One case may be surely a bit incompatible or unexpected behaviour of functions in VFS-definition (e. g. filesystemPathTypeProc or pathInFilesystemProc), but then they can be adjusted to consider "new" handling.

However it is surely worth to check the branch against few VFS.


juliannoble2 added on 2024-09-14 01:05:24:
This issue has bugged me for a long time.
I assume we could close this too now as I think it's essentially the same problem?
https://core.tcl-lang.org/tcl/tktview/411f52ed87e313dd49e2

sebres added on 2024-09-14 09:47:42:

Indeed, [411f52ed87e313dd49e2] looks very similar. However I'd like to see what would happen with 9.0 once this branch gets merged, since it seems to have a bit different timing (suspecting something around tilde-expansion changes).


oehhar added on 2024-09-14 17:10:58:

Sergey, great work !

Julian refers to ticket [411f52ed87e313dd49e2].


jan.nijtmans added on 2024-09-14 21:32:04:

At least the Github ACTIONS build doesn't show any errors.

Since 8.6.15 is close to release, I suggest to wait until after this release for the merge. After that: no objection from me.


jan.nijtmans added on 2024-09-14 21:33:39:

Hm. Better point to the Github ACTIONS build on Windows. They pass too.


sebres added on 2024-09-15 11:16:50:

Just a note for me: the check for tilde expansion (marked with TODO in the branch) is indeed not really necessary for 9.0, because file pathtype ~/test is "relative" since merge of TIP#602, where still "absolute" for 8.x.

However, it'd be better to have a common function that'll be able to tell whether normalization is needed. Or maybe to move some logic into VFS-definition for Tcl_FSGetNativePath (where it basically belongs to).


sebres added on 2024-09-17 15:57:41:

8.6.15 is released, thus the fix is merged now in all branches >= 8.6.
For 8.7+ there was additional unneeded normalization in zipfs, which is solved in [ed87833280], and merged as whole thing in 9.0 in [265dc4708a3bf925] + small amend hereafter (removing tilde expansion check for windows).

I hope the major issues of [411f52ed87e313dd49e2] are fixed too now (at least the performance regression of 8.7+).
However there was also mentioned a glob command (which is notoriously slow under windows)... I don't think there is good a way to speed-up it together with file normalize, excepting possibly a new option like file normalize -fast ... what could do normalization similar translated path + sym-links, but without FindFirstFileW and co (so no unique path generation).


jan.nijtmans added on 2024-09-18 08:21:24:

All --enable-symbols=mem builds for 8.7 and 9.0 are failing now on all platforms. There appears to be a memory leak. Therefore, re-opening.

@sebres, can you have a look at this? I suspect the changes in the zipfs, since 8.6 doesn't have that part, and the leak is visible on all platforms not only Windows.


sebres added on 2024-09-18 10:40:47:

Hmm... Indeed. It is because Tcl_FSGetTranslatedPath in opposite to Tcl_FSGetNormalizedPath always returns an incremented object reference (why basically? like normPathPtr, translatedCwdPtr is also stored in the internal representation and it'd be no needs to increase it at all). Anyway will fix it asap.

By the way, I noticed your comment only accidental - because there are no mails from tcl-repo in the mailing list anymore since 27th Aug (the mails [Tk] arriving, but not [Tcl])... And it is surely not my system, because in SF mailing list archive also nothing since 27th Aug.


sebres added on 2024-09-18 10:56:21:

Mem-leak is fixed in [536390e4258fbef0], thus close.


dgp added on 2024-12-02 13:00:27:
The memory leak fix needs to be backported to the 8.6 branch, right?

I see leaks in the 8.6.16 release candidate.

dgp added on 2024-12-02 13:08:40:
No.  That memleak fix is in zipfs code, which isn't in Tcl 8.6.

This major system rewrite in a patch release of a stable branch
has instead introduced some other memleaks.

Searching....

dgp added on 2024-12-02 13:35:23:
The commit [a6a5870235ce08b2] modifies the routine
TclFSCwdIsNative(), adding a call to Tcl_FSGetCwd().

That call (eventually) allocates a Tcl_Obj in line 735:

        tsdPtr->cwdPathPtr = Tcl_NewStringObj(str, len);

and that allocation leaks.

Not yet clear to me why the existing refcount housekeeping
isn't able to handle this.

dgp added on 2024-12-02 13:58:52:
Maybe this is easy.  Documentation for Tcl_FSGetCwd() says

"...The  result already has its reference count incremented for the caller.
       When it is no longer needed, that  reference  count  should  be  decre‐
       mented."

This new call doesn't seem to be following the instructions.  Will try
that as a fix.

sebres added on 2024-12-02 14:39:42:

Good catch, Don!..

I added small amend [e4feb601a833fa18] to avoid possible theoretical segfault.

However it'd be nice to have some routine updating cwdPath (tsdPtr->cwdPathPtr) and epoch without to return incremented reference (so one could use it instead of Tcl_FSGetCwd for the init purposes).