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

Fix data races in sorting and Reverse #1296

Closed
wants to merge 1 commit into from
Closed

Conversation

bep
Copy link
Member

@bep bep commented Jul 21, 2015

And add caching to the result.

It is, not surprisingly, also faster:

benchmark                           old ns/op     new ns/op     delta
BenchmarkSortByWeightAndReverse     14104         465           -96.70%

benchmark                           old allocs     new allocs     delta
BenchmarkSortByWeightAndReverse     1              0              -100.00%

benchmark                           old bytes     new bytes     delta
BenchmarkSortByWeightAndReverse     32            0             -100.00%

Fixes #1293

@bep bep force-pushed the reverse-race branch 4 times, most recently from 3393773 to b36a4fe Compare July 21, 2015 19:25
@bep
Copy link
Member Author

bep commented Jul 21, 2015

Of course, just making copies on every sort and reverse will also fix the main issue here, but at a slight cost:

benchmark                           old ns/op     new ns/op     delta
BenchmarkSortByWeightAndReverse     14304         19858         +38.83%

benchmark                           old allocs     new allocs     delta
BenchmarkSortByWeightAndReverse     1              3              +200.00%

benchmark                           old bytes     new bytes     delta
BenchmarkSortByWeightAndReverse     32            5152          +16000.00%

@tatsushid @spf13 ... any thoughts?

@spf13
Copy link
Contributor

spf13 commented Jul 22, 2015

wow. Thanks for tracking this down. I think caching is a big win, especially for large sites. I'm not sure I fully understand the root issue. Is it that the sort / reverse isn't safe for concurrency and consequently the order isn't right/ items get lost?

It seems the best approach would be to make it concurrently safe (mutexes or channels would work well). Then cache the results.

Is this what you were looking for?

@bep
Copy link
Member Author

bep commented Jul 22, 2015

There are several issues in play here:

  • The symptom in this case is "lost pages" caused by a data race where Reverse modified the slice while ByDateread it.
  • There some methods that operate on Pages that does restructuring of the slice.
  • Most use cases use this modified slice only once (in the index template)
  • In this case, the single template had a list of recent posts in the side-bar, so the Data.Pages.ByDate.Reverse were executed 300 times in parallell => data race.
  • Then there are, of course, variants of the above, people trying do multiple sorting of the same data etc.

My fix is this:

  • A copy is made of the slice before doing any structural changes and all writes are locked. This fixes the concurrency issues / data races etc.
  • In addition to the above, the result of the restructuring is cached (with a read/write mutex to protect the cache). In normal cases (small blogs), there are very minute savings from caching this result, but it just sounds ineffective to sort and reverse the slice 300 times with the same result.

I have thought more on this, and will polish up my fix, as it makes sense to me.

@bep bep force-pushed the reverse-race branch 8 times, most recently from 5b93578 to 658c504 Compare July 23, 2015 09:33
@bep bep changed the title Work in progress: Fix data races in sorting and Reverse Fix data races in sorting and Reverse Jul 23, 2015
@bep bep force-pushed the reverse-race branch 3 times, most recently from c8a3dcb to f729d95 Compare July 23, 2015 10:29
The custom sort functions used from the templates had some subtle data race- and related issues,
especially when used in the single page template.

This commit fixes this by making copies and protect the read and writes with a RWMutex.

The results are cached (it will typically be invoked *number of pages* times with exactly the same data).

This is, not surprisingly, also faster:

```
benchmark                           old ns/op     new ns/op     delta
BenchmarkSortByWeightAndReverse     14228         491           -96.55%

benchmark                           old allocs     new allocs     delta
BenchmarkSortByWeightAndReverse     1              0              -100.00%

benchmark                           old bytes     new bytes     delta
BenchmarkSortByWeightAndReverse     32            0             -100.00%
```

Fixes gohugoio#1293
@bep
Copy link
Member Author

bep commented Jul 23, 2015

Merged in a9c5133

@bep bep closed this Jul 23, 2015
@bep bep deleted the reverse-race branch April 18, 2017 09:19
bep added a commit that referenced this pull request Jan 20, 2021
ef9c4913c Clean up and removal of outdated examples
46122c9aa add godot tutorials to showcase
06d1d1ea2 Update scss-sass.md
1fc63c100 Spelling fix in 0.79.1 release notes
ad2f50e3d Update plainwords description (#1296)
33021d451 Update substr examples (#1304)
6b1cc59bb Release 0.80.0
521db8c6d Merge branch 'tempv0.80.0'
58626c2b3 releaser: Add release notes to /docs for release of 0.80.0
f81d118af dartsass: Dart Sass only supports `expanded` and `compressed`
7da6f54be Add Dart Sass support
b1f2661bb Replace jsconfig.js with jsconfig.json
38de0c1a4 Update index.md
223ceae80 Update index.md
f7ac0e59d Release v0.79.1
2d4583d43 Merge branch 'temp791-2'
1d34e609b releaser: Add release notes to /docs for release of 0.79.1
e26769988 Merge branch 'temp791'
75694d904 Fix Resource.ResourceType so it always returns MIME's main type
0f65d7783 Typo s/adds/add (#1298)
0b896b2c0 images: Add images.Overlay filter
0d4257dcd Clarify documentation on shimming
fcf601ddf Update index.html
6bf9bc1c1 Update index.html
1ce76bf3a Update index.html
e7d976eec Update index.html
db2996e64 Update index.html
245e5bfc9 news: Add post about Apple M1
3ad4115ed tpl: Add title parameter to YouTube shortcode
76ed976f8 Added two useful extensions to the list (#1243)
e5a30dd11 Update related.md
25cf8f48b Improve substr examples
e16e57e9a Update path.Split.md
2749b88fd Update path.Split.md
d76cad3ff Release 0.79.0
f5ccfbe98 releaser: Add release notes to /docs for release of 0.79.0
ebf1b87b0 Merge commit '9f1265fde4b9ef186148337c99f08601633b6056'
1f1e8f39c Allow setting the delimiter used for setting config via OS env, e.g. HUGO_
e9b1414dd deps: Update to github.com/evanw/esbuild 0.8.11 to 0.8.14
0f76cf66c docs: Regen docshelper
1ada5d47e Add menu params
1c120aef0 Revert "docs: Regenerate docshelper"
7b60b5624 docs: Regenerate docshelper

git-subtree-dir: docs
git-subtree-split: ef9c4913cdcf95d62ec12d872f412f97e55a55ad
@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 14, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Issue with pages with duplicate date not being generated on multicore PC
2 participants