Skip to content

lib/fs: Optimize UnicodeLowercase - #6979

Merged
calmh merged 2 commits into
syncthing:mainfrom
greatroar:optimize-lowercase
Sep 11, 2020
Merged

calmh merged 2 commits into
syncthing:mainfrom
greatroar:optimize-lowercase

Conversation

@greatroar

@greatroar greatroar commented Sep 10, 2020 •

Copy link
Copy Markdown
Contributor

Most notably, it now detects all-lowercase files and returns these as-is. The tests have been expanded with two cases and are now used as a benchmark (admittedly a rather trivial one).

Benchmark results on Linux/amd64 (note that WalkCaseFakeFS100k creates all-lowercase filenames):

name                           old time/op         new time/op         delta
WalkCaseFakeFS100k/rawfs-8             388ms ± 1%          312ms ± 3%   -19.64%  (p=0.000 n=10+8)
WalkCaseFakeFS100k/casefs-8            718ms ± 1%          587ms ± 1%   -18.30%  (p=0.000 n=10+10)
UnicodeLowercaseMaybeChange-8         5.45µs ± 5%         4.57µs ± 1%   -16.20%  (p=0.000 n=10+10)
UnicodeLowercaseNoChange-8            5.33µs ± 4%         3.09µs ± 1%   -42.01%  (p=0.000 n=10+10)

name                           old B/entry         new B/entry         delta
WalkCaseFakeFS100k/rawfs-8               590 ± 0%            530 ± 0%   -10.17%  (p=0.000 n=10+10)
WalkCaseFakeFS100k/casefs-8              869 ± 0%            760 ± 0%   -12.54%  (p=0.000 n=10+10)

name                           old allocs/entry    new allocs/entry    delta
WalkCaseFakeFS100k/rawfs-8              15.7 ± 0%           12.1 ± 0%   -22.93%  (p=0.000 n=10+10)
WalkCaseFakeFS100k/casefs-8             26.1 ± 0%           20.1 ± 0%   -22.99%  (p=0.000 n=10+10)

name                           old ns/entry        new ns/entry        delta
WalkCaseFakeFS100k/rawfs-8             2.57k ± 1%          2.06k ± 3%   -19.65%  (p=0.000 n=10+8)
WalkCaseFakeFS100k/casefs-8            4.75k ± 1%          3.86k ± 1%   -18.63%  (p=0.000 n=10+10)

name                           old alloc/op        new alloc/op        delta
WalkCaseFakeFS100k/rawfs-8            89.2MB ± 0%         80.2MB ± 0%   -10.08%  (p=0.000 n=9+10)
WalkCaseFakeFS100k/casefs-8            131MB ± 0%          115MB ± 0%   -12.54%  (p=0.000 n=9+10)
UnicodeLowercaseMaybeChange-8           320B ± 0%           192B ± 0%   -40.00%  (p=0.000 n=10+10)
UnicodeLowercaseNoChange-8              320B ± 0%             0B       -100.00%  (p=0.000 n=10+10)

name                           old allocs/op       new allocs/op       delta
WalkCaseFakeFS100k/rawfs-8             2.38M ± 0%          1.82M ± 0%   -23.44%  (p=0.002 n=8+10)
WalkCaseFakeFS100k/casefs-8            3.95M ± 0%          3.04M ± 0%   -23.10%  (p=0.000 n=10+9)
UnicodeLowercaseMaybeChange-8           16.0 ± 0%           10.0 ± 0%   -37.50%  (p=0.000 n=10+10)
UnicodeLowercaseNoChange-8              16.0 ± 0%            0.0       -100.00%  (p=0.000 n=10+10)

@greatroar

Copy link
Copy Markdown
Contributor Author

I must confess that I don't know what's causing

UnicodeLowercaseMaybeChange-8           320B ± 0%           384B ± 0%   +20.00%  (p=0.000 n=10+10)

Overallocating a few bytes (say, 4 or 10) can decrease the alloc/op to 208B, but I don't understand why. The encoded output is never longer than the encoded input in the benchmark.

@greatroar

Copy link
Copy Markdown
Contributor Author

Found it, see updated benchmark results and comment in the code.

Most notably, it now detects all-lowercase files and returns these
as-is. The tests have been expanded with two cases and are now used
as a benchmark (admittedly a rather trivial one).

Benchmark results on Linux/amd64 (WalkCaseFakeFS100k creates
all-lowercase filenames):

name                           old time/op         new time/op         delta
WalkCaseFakeFS100k/rawfs-8             388ms ± 1%          312ms ± 3%   -19.64%  (p=0.000 n=10+8)
WalkCaseFakeFS100k/casefs-8            718ms ± 1%          587ms ± 1%   -18.30%  (p=0.000 n=10+10)
UnicodeLowercaseMaybeChange-8         5.45µs ± 5%         4.59µs ± 2%   -15.81%  (p=0.000 n=10+10)
UnicodeLowercaseNoChange-8            5.33µs ± 4%         3.25µs ± 1%   -39.02%  (p=0.000 n=10+10)

name                           old B/entry         new B/entry         delta
WalkCaseFakeFS100k/rawfs-8               590 ± 0%            530 ± 0%   -10.17%  (p=0.000 n=10+10)
WalkCaseFakeFS100k/casefs-8              869 ± 0%            760 ± 0%   -12.54%  (p=0.000 n=10+10)

name                           old allocs/entry    new allocs/entry    delta
WalkCaseFakeFS100k/rawfs-8              15.7 ± 0%           12.1 ± 0%   -22.93%  (p=0.000 n=10+10)
WalkCaseFakeFS100k/casefs-8             26.1 ± 0%           20.1 ± 0%   -22.99%  (p=0.000 n=10+10)

name                           old ns/entry        new ns/entry        delta
WalkCaseFakeFS100k/rawfs-8             2.57k ± 1%          2.06k ± 3%   -19.65%  (p=0.000 n=10+8)
WalkCaseFakeFS100k/casefs-8            4.75k ± 1%          3.86k ± 1%   -18.63%  (p=0.000 n=10+10)

name                           old alloc/op        new alloc/op        delta
WalkCaseFakeFS100k/rawfs-8            89.2MB ± 0%         80.2MB ± 0%   -10.08%  (p=0.000 n=9+10)
WalkCaseFakeFS100k/casefs-8            131MB ± 0%          115MB ± 0%   -12.54%  (p=0.000 n=9+10)
UnicodeLowercaseMaybeChange-8           320B ± 0%           192B ± 0%   -40.00%  (p=0.000 n=10+10)
UnicodeLowercaseNoChange-8              320B ± 0%             0B       -100.00%  (p=0.000 n=10+10)

name                           old allocs/op       new allocs/op       delta
WalkCaseFakeFS100k/rawfs-8             2.38M ± 0%          1.82M ± 0%   -23.44%  (p=0.002 n=8+10)
WalkCaseFakeFS100k/casefs-8            3.95M ± 0%          3.04M ± 0%   -23.10%  (p=0.000 n=10+9)
UnicodeLowercaseMaybeChange-8           16.0 ± 0%           10.0 ± 0%   -37.50%  (p=0.000 n=10+10)
UnicodeLowercaseNoChange-8              16.0 ± 0%            0.0       -100.00%  (p=0.000 n=10+10)
@calmh

calmh commented Sep 10, 2020

Copy link
Copy Markdown
Member

So this is neat, though I think the benchmarks aren't useful here. The improvement is equally large for the raw case that doesn't use casefs, since the underlying fakefs itself uses UnicodeLowercase a lot to implement its fake case insensitivity.

Comment thread lib/fs/folding.go Resolved
@greatroar

Copy link
Copy Markdown
Contributor Author

The improvement is equally large for the raw case that doesn't use casefs, since the underlying fakefs itself uses UnicodeLowercase a lot to implement its fake case insensitivity.

I'm not sure I follow completely. Should I remove the rawfs benchmark results from the commit message? The casefs ones? Both?

name                           old time/op    new time/op    delta
UnicodeLowercaseMaybeChange-8    4.59µs ± 2%    4.57µs ± 1%    ~     (p=0.197 n=10+10)
UnicodeLowercaseNoChange-8       3.26µs ± 1%    3.09µs ± 1%  -5.27%  (p=0.000 n=9+10)
Comment thread lib/fs/folding.go
// even if it doesn't need all that space. Overallocate now to prevent
// it from ever triggering a reallocation.
rs.Grow(utf8.UTFMax - 1 + len(s))
rs.WriteString(s[:i])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think this works in the general case. firstCaseChange returns the index of the first rune to have its case changed, while s[:i] is counted in bytes. This should break for strings with lowercase non-ASCII characters in the beginning, which may not be any of our current test cases

@calmh calmh Sep 11, 2020 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

My bad, this is fine. (Index in loop over string is in fact byte position, not rune number. TIL.)

@calmh
calmh merged commit 3e24d82 into syncthing:main Sep 11, 2020
@calmh calmh added this to the v1.10.0 milestone Sep 12, 2020
calmh added a commit to calmh/syncthing that referenced this pull request Sep 24, 2020
* main: (50 commits)
  cmd/ursrv: Properly sort versions (fixes syncthing#6991)
  cmd/ursrv: Recognize new-style dev versions
  gui, man, authors: Update docs, translations, and contributors
  build: Dockerfile that expects ready baked binaries
  lib/model: Consider case conflicts when checking to be deleted items (syncthing#6986)
  lib/fs: Optimize UnicodeLowercase (syncthing#6979)
  lib/db, lib/syncthing: Don't repair DB on upgrade, but on error (fixes syncthing#6917) (syncthing#6971)
  lib/model: Consider existing file when handling symlink on windows (syncthing#6977)
  lib/model: Handle symlink deletion on windows (fixes syncthing#6972) (syncthing#6976)
  lib/model: Fix case-only renames on pull (syncthing#6978)
  lib/fs: More efficient casefs cache (syncthing#6974)
  lib/model: Don't fail over case-conflict on tempfile (fixes syncthing#6973) (syncthing#6975)
  gui, man, authors: Update docs, translations, and contributors
  lib/fs: Use generic case resolver on windows (fixes syncthing#6968) (syncthing#6969)
  lib/model: Prevent duplicate need count on recalculating metadata (syncthing#6964)
  lib/db, lib/protocol: Never need empty-version entries (fixes syncthing#6961) (syncthing#6962)
  lib/model: Correct completion perc. when globalBytes is 0 (syncthing#6963)
  lib/db: Don't hang on double iterator release with Badger (syncthing#6960)
  test, lib/model: Various integration test updates & improvements (syncthing#6956)
  lib/db: Copy returned, old FileVersion (syncthing#6952)
  ...
@st-review st-review added the frozen-due-to-age Issues closed and untouched for a long time, together with being locked for discussion label Sep 12, 2021
@syncthing syncthing locked and limited conversation to collaborators Sep 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

frozen-due-to-age Issues closed and untouched for a long time, together with being locked for discussion

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants