-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from DrBlury/main
Added go concurrency alternative
- Loading branch information
Showing
6 changed files
with
188 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
rust/target | ||
rust_rayon/target | ||
go/related | ||
go_con/related_concurrent | ||
odin/main | ||
related*.json | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
go 1.21.1 | ||
|
||
use ( | ||
./go | ||
./go_con | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module g.io/related_concurrent | ||
|
||
go 1.21.1 | ||
|
||
require github.com/goccy/go-json v0.10.2 | ||
|
||
require github.com/ugurcsen/gods-generic v0.10.4 // indirect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= | ||
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= | ||
github.com/ugurcsen/gods-generic v0.10.4 h1:OomH3R2MdzZxpnEPijaD/ncLzV6rpDXd5ruEkWsw0vo= | ||
github.com/ugurcsen/gods-generic v0.10.4/go.mod h1:mGYOa88Y5sbw+ADXLpScxjJ7s5iHoWya/YHyeQ4f6c4= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
"runtime" | ||
"time" | ||
|
||
"github.com/goccy/go-json" | ||
"github.com/ugurcsen/gods-generic/trees/binaryheap" | ||
) | ||
|
||
var concurrency = runtime.NumCPU() | ||
|
||
type Post struct { | ||
ID string `json:"_id"` | ||
Title string `json:"title"` | ||
Tags []string `json:"tags"` | ||
} | ||
|
||
type PostWithSharedTags struct { | ||
Post int | ||
SharedTags int | ||
} | ||
|
||
type RelatedPosts struct { | ||
ID string `json:"_id"` | ||
Tags []string `json:"tags"` | ||
Related []*Post `json:"related"` | ||
} | ||
|
||
// Result struct to hold results from goroutines | ||
type Result struct { | ||
Index int | ||
RelatedPost RelatedPosts | ||
} | ||
|
||
func main() { | ||
f, err := os.Open("../posts.json") | ||
if err != nil { | ||
log.Panicln(err) | ||
} | ||
|
||
var posts []Post | ||
err = json.NewDecoder(f).Decode(&posts) | ||
if err != nil { | ||
log.Panicln(err) | ||
} | ||
|
||
start := time.Now() | ||
|
||
tagMap := make(map[string][]int, 100) | ||
for i, post := range posts { | ||
for _, tag := range post.Tags { | ||
tagMap[tag] = append(tagMap[tag], i) | ||
} | ||
} | ||
|
||
resultsChan := make(chan Result, len(posts)) | ||
doneChan := make(chan bool, concurrency) | ||
|
||
for w := 0; w < concurrency; w++ { | ||
go func(workerID int) { | ||
defer func() { doneChan <- true }() | ||
for i := workerID; i < len(posts); i += concurrency { | ||
relatedPost := computeRelatedPost(i, posts, tagMap) | ||
resultsChan <- Result{Index: i, RelatedPost: relatedPost} | ||
} | ||
}(w) | ||
} | ||
|
||
for i := 0; i < concurrency; i++ { | ||
<-doneChan | ||
} | ||
close(resultsChan) | ||
close(doneChan) | ||
|
||
allRelatedPosts := make([]RelatedPosts, len(posts)) | ||
for r := range resultsChan { | ||
allRelatedPosts[r.Index] = r.RelatedPost | ||
} | ||
|
||
end := time.Now() | ||
|
||
fmt.Println("Processing time (w/o IO)", end.Sub(start)) | ||
|
||
file, err := os.Create("../related_posts_go_con.json") | ||
if err != nil { | ||
log.Panicln(err) | ||
} | ||
|
||
err = json.NewEncoder(file).Encode(allRelatedPosts) | ||
if err != nil { | ||
log.Panicln(err) | ||
} | ||
} | ||
|
||
func computeRelatedPost(i int, posts []Post, tagMap map[string][]int) RelatedPosts { | ||
taggedPostCount := make([]int, len(posts)) | ||
t5 := binaryheap.NewWith[PostWithSharedTags](PostComparator) | ||
|
||
for _, tag := range posts[i].Tags { | ||
for _, otherPostIdx := range tagMap[tag] { | ||
if otherPostIdx != i { | ||
taggedPostCount[otherPostIdx]++ | ||
} | ||
} | ||
} | ||
|
||
for v, count := range taggedPostCount { | ||
if t5.Size() < 5 { | ||
t5.Push(PostWithSharedTags{Post: v, SharedTags: count}) | ||
} else { | ||
if t, _ := t5.Peek(); t.SharedTags < count { | ||
t5.Pop() | ||
t5.Push(PostWithSharedTags{Post: v, SharedTags: count}) | ||
} | ||
} | ||
} | ||
|
||
num := min(5, t5.Size()) | ||
topPosts := make([]*Post, num) | ||
|
||
for i := 0; i < num; i++ { | ||
if t, ok := t5.Pop(); ok { | ||
topPosts[i] = &posts[t.Post] | ||
} | ||
} | ||
|
||
return RelatedPosts{ | ||
ID: posts[i].ID, | ||
Tags: posts[i].Tags, | ||
Related: topPosts, | ||
} | ||
} | ||
|
||
func PostComparator(a, b PostWithSharedTags) int { | ||
if a.SharedTags > b.SharedTags { | ||
return 1 | ||
} | ||
if a.SharedTags < b.SharedTags { | ||
return -1 | ||
} | ||
return 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters