Skip to content

Commit

Permalink
Merge pull request #7 from DrBlury/main
Browse files Browse the repository at this point in the history
Improved performance of go with generics
  • Loading branch information
jinyus authored Sep 23, 2023
2 parents f626a9a + 19f297a commit dfcbb56
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 39 deletions.
4 changes: 2 additions & 2 deletions go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ module g.io/related

go 1.21.1

require github.com/emirpasic/gods v1.18.1
require github.com/goccy/go-json v0.10.2

require github.com/goccy/go-json v0.10.2 // indirect
require github.com/ugurcsen/gods-generic v0.10.4 // indirect
4 changes: 2 additions & 2 deletions go/go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
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=
60 changes: 25 additions & 35 deletions go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"os"
"time"

"github.com/emirpasic/gods/trees/binaryheap"
"github.com/goccy/go-json"
"github.com/ugurcsen/gods-generic/trees/binaryheap"
)

type Post struct {
Expand All @@ -16,26 +16,25 @@ type Post struct {
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"`
}

type PostWithSharedTags struct {
Post int
SharedTags int
}

func main() {

file, err := os.Open("../posts.json")
f, err := os.Open("../posts.json")
if err != nil {
log.Panicln(err)
}

var posts []Post
err = json.NewDecoder(file).Decode(&posts)
err = json.NewDecoder(f).Decode(&posts)

if err != nil {
log.Panicln(err)
Expand All @@ -52,49 +51,47 @@ func main() {
}

allRelatedPosts := make([]RelatedPosts, 0, len(posts))

taggedPostCount := make([]int, len(posts))
t5 := binaryheap.NewWith[PostWithSharedTags](PostComparator)

for i, post := range posts {
// luckily this is optimized to a memset
for i := range taggedPostCount {
taggedPostCount[i] = 0
for i := range posts {
for j := range taggedPostCount {
taggedPostCount[j] = 0
}

for _, tag := range post.Tags {
for _, tag := range posts[i].Tags {
for _, otherPostIdx := range tagMap[tag] {
if otherPostIdx != i {
taggedPostCount[otherPostIdx]++
}
}
}

t5 := binaryheap.NewWith(PostComparator)
t5.Clear() // Clear the heap for next post

for v, count := range taggedPostCount {
if t5.Size() < 5 {
t5.Push(PostWithSharedTags{Post: v, SharedTags: count})
} else {
if t, _ := t5.Peek(); t.(PostWithSharedTags).SharedTags < count {
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, _ := t5.Pop(); t != nil {
topPosts[i] = &posts[t.(PostWithSharedTags).Post]
if t, ok := t5.Pop(); ok {
topPosts[i] = &posts[t.Post]
}
}

allRelatedPosts = append(allRelatedPosts, RelatedPosts{
ID: post.ID,
Tags: post.Tags,
ID: posts[i].ID,
Tags: posts[i].Tags,
Related: topPosts,
})
}
Expand All @@ -103,7 +100,7 @@ func main() {

fmt.Println("Processing time (w/o IO)", end.Sub(start))

file, err = os.Create("../related_posts_go.json")
file, err := os.Create("../related_posts_go.json")

if err != nil {
log.Panicln(err)
Expand All @@ -114,21 +111,14 @@ func main() {
if err != nil {
log.Panicln(err)
}

}

func PostComparator(a, b interface{}) int {
aAsserted := a.(PostWithSharedTags)
bAsserted := b.(PostWithSharedTags)

if aAsserted.SharedTags > bAsserted.SharedTags {
func PostComparator(a, b PostWithSharedTags) int {
if a.SharedTags > b.SharedTags {
return 1

} else if aAsserted.SharedTags < bAsserted.SharedTags {
}
if a.SharedTags < b.SharedTags {
return -1

} else {
return 0
}

return 0
}

0 comments on commit dfcbb56

Please sign in to comment.