This repository has been archived by the owner on Jan 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Heavily optimize cpu time and allocations.
- Loading branch information
Showing
6 changed files
with
187 additions
and
33 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
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,26 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/finnbear/moderation" | ||
"log" | ||
"os" | ||
"runtime/pprof" | ||
) | ||
|
||
func main() { | ||
f, err := os.Create("moderation.prof") | ||
if err != nil { | ||
log.Fatal("could not create CPU profile: ", err) | ||
} | ||
defer f.Close() | ||
if err := pprof.StartCPUProfile(f); err != nil { | ||
log.Fatal("could not start CPU profile: ", err) | ||
} | ||
defer pprof.StopCPUProfile() | ||
|
||
for i := 0; i < 100000; i++ { | ||
moderation.Analyze("hello") | ||
moderation.Analyze("sh1t") | ||
moderation.Analyze("Hello John Doe, I hope you're feeling well, as I come today bearing shitty news regarding your favorite chocolate chip cookie brand") | ||
} | ||
} |
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,26 @@ | ||
package radix | ||
|
||
type Buffer struct { | ||
Storage [longestWord * 2]*Node // * 2 because some characters turn into 2 matches | ||
index int | ||
} | ||
|
||
func (buffer *Buffer) Append(node *Node) { | ||
buffer.Storage[buffer.index] = node | ||
buffer.index++ | ||
if buffer.index >= len(buffer.Storage) { | ||
buffer.index = 0 | ||
} | ||
} | ||
|
||
func (buffer *Buffer) Clear() { | ||
buffer.index = 0 | ||
} | ||
|
||
func (buffer *Buffer) Get(index int) *Node { | ||
return buffer.Storage[index] | ||
} | ||
|
||
func (buffer *Buffer) Len() int { | ||
return buffer.index | ||
} |
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
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
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,94 @@ | ||
package moderation | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func IsProfane(phrase string) bool { | ||
return Analyze(phrase).IsInappropriate() | ||
} | ||
|
||
// from https://github.com/TwinProduction/go-away/blob/master/goaway_bench_test.go | ||
func BenchmarkIsProfaneWhenShortStringHasNoProfanity(b *testing.B) { | ||
for n := 0; n < b.N; n++ { | ||
IsProfane("aaaaaaaaaaaaaa") | ||
} | ||
b.ReportAllocs() | ||
} | ||
|
||
func BenchmarkIsProfaneWhenShortStringHasProfanityAtTheStart(b *testing.B) { | ||
for n := 0; n < b.N; n++ { | ||
IsProfane("fuckaaaaaaaaaa") | ||
} | ||
b.ReportAllocs() | ||
} | ||
|
||
func BenchmarkIsProfaneWhenShortStringHasProfanityInTheMiddle(b *testing.B) { | ||
for n := 0; n < b.N; n++ { | ||
IsProfane("aaaaafuckaaaaa") | ||
} | ||
b.ReportAllocs() | ||
} | ||
|
||
func BenchmarkIsProfaneWhenShortStringHasProfanityAtTheEnd(b *testing.B) { | ||
for n := 0; n < b.N; n++ { | ||
IsProfane("aaaaaaaaaafuck") | ||
} | ||
b.ReportAllocs() | ||
} | ||
|
||
func BenchmarkIsProfaneWhenMediumStringHasNoProfanity(b *testing.B) { | ||
for n := 0; n < b.N; n++ { | ||
IsProfane("How are you doing today?") | ||
} | ||
b.ReportAllocs() | ||
} | ||
|
||
func BenchmarkIsProfaneWhenMediumStringHasProfanityAtTheStart(b *testing.B) { | ||
for n := 0; n < b.N; n++ { | ||
IsProfane("Shit, you're cute today.") | ||
} | ||
b.ReportAllocs() | ||
} | ||
|
||
func BenchmarkIsProfaneWhenMediumStringHasProfanityInTheMiddle(b *testing.B) { | ||
for n := 0; n < b.N; n++ { | ||
IsProfane("How are you fu ck doing?") | ||
} | ||
b.ReportAllocs() | ||
} | ||
|
||
func BenchmarkIsProfaneWhenMediumStringHasProfanityAtTheEnd(b *testing.B) { | ||
for n := 0; n < b.N; n++ { | ||
IsProfane("you're cute today. Fuck.") | ||
} | ||
b.ReportAllocs() | ||
} | ||
|
||
func BenchmarkIsProfaneWhenLongStringHasNoProfanity(b *testing.B) { | ||
for n := 0; n < b.N; n++ { | ||
IsProfane("Hello John Doe, I hope you're feeling well, as I come today bearing terrible news regarding your favorite chocolate chip cookie brand") | ||
} | ||
b.ReportAllocs() | ||
} | ||
|
||
func BenchmarkIsProfaneWhenLongStringHasProfanityAtTheStart(b *testing.B) { | ||
for n := 0; n < b.N; n++ { | ||
IsProfane("Fuck John Doe, I hope you're feeling well, as I come today bearing terrible news regarding your favorite chocolate chip cookie brand") | ||
} | ||
b.ReportAllocs() | ||
} | ||
|
||
func BenchmarkIsProfaneWhenLongStringHasProfanityInTheMiddle(b *testing.B) { | ||
for n := 0; n < b.N; n++ { | ||
IsProfane("Hello John Doe, I hope you're feeling well, as I come today bearing shitty news regarding your favorite chocolate chip cookie brand") | ||
} | ||
b.ReportAllocs() | ||
} | ||
|
||
func BenchmarkIsProfaneWhenLongStringHasProfanityAtTheEnd(b *testing.B) { | ||
for n := 0; n < b.N; n++ { | ||
IsProfane("Hello John Doe, I hope you're feeling well, as I come today bearing terrible news regarding your favorite chocolate chip cookie bitch") | ||
} | ||
b.ReportAllocs() | ||
} |