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

Improved performance a little with generics #7

Merged
merged 2 commits into from
Sep 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}