]> git.lizzy.rs Git - micro.git/commitdiff
Make determining whether a code point represents a combining mark faster (#1719)
authorPhilipp Emanuel Weidmann <pew@worldwidemann.com>
Fri, 12 Jun 2020 04:10:00 +0000 (09:40 +0530)
committerGitHub <noreply@github.com>
Fri, 12 Jun 2020 04:10:00 +0000 (00:10 -0400)
Makefile
internal/util/unicode.go
pkg/highlight/unicode.go

index f3c0ca0d68b365ca163515bcb365639ea4ac0a4d..a0a1417a6612538f5688b0b2ff422db60fa78a6c 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -79,7 +79,7 @@ bench-compare:
        for i in 1 2 3; do \
                go test -bench=. ./internal/...; \
        done > benchmark_results
-       benchstat benchmark_results_baseline benchmark_results
+       benchstat -alpha 0.15 benchmark_results_baseline benchmark_results
 
 clean:
        rm -f micro
index f1bb989626604d3021eb8ef45b6fd919e7d55d67..14243e68bee096e67b5e4e3b1b3a498936996ad8 100644 (file)
@@ -16,6 +16,16 @@ import (
 // For rendering, micro will display the combining characters. It's not perfect
 // but it's pretty good.
 
+var minMark = rune(unicode.Mark.R16[0].Lo)
+
+func isMark(r rune) bool {
+       // Fast path
+       if r < minMark {
+               return false
+       }
+       return unicode.In(r, unicode.Mark)
+}
+
 // DecodeCharacter returns the next character from an array of bytes
 // A character is a rune along with any accompanying combining runes
 func DecodeCharacter(b []byte) (rune, []rune, int) {
@@ -24,7 +34,7 @@ func DecodeCharacter(b []byte) (rune, []rune, int) {
        c, s := utf8.DecodeRune(b)
 
        var combc []rune
-       for unicode.In(c, unicode.Mark) {
+       for isMark(c) {
                combc = append(combc, c)
                size += s
 
@@ -43,7 +53,7 @@ func DecodeCharacterInString(str string) (rune, []rune, int) {
        c, s := utf8.DecodeRuneInString(str)
 
        var combc []rune
-       for unicode.In(c, unicode.Mark) {
+       for isMark(c) {
                combc = append(combc, c)
                size += s
 
@@ -61,7 +71,7 @@ func CharacterCount(b []byte) int {
 
        for len(b) > 0 {
                r, size := utf8.DecodeRune(b)
-               if !unicode.In(r, unicode.Mark) {
+               if !isMark(r) {
                        s++
                }
 
@@ -77,7 +87,7 @@ func CharacterCountInString(str string) int {
        s := 0
 
        for _, r := range str {
-               if !unicode.In(r, unicode.Mark) {
+               if !isMark(r) {
                        s++
                }
        }
index 646c2eecd5855e1eff4f57acbe36c1541f1e52f3..a18118a6ca2fde0b3e43c9d426c35bb3e334ba52 100644 (file)
@@ -5,6 +5,16 @@ import (
        "unicode/utf8"
 )
 
+var minMark = rune(unicode.Mark.R16[0].Lo)
+
+func isMark(r rune) bool {
+       // Fast path
+       if r < minMark {
+               return false
+       }
+       return unicode.In(r, unicode.Mark)
+}
+
 // DecodeCharacter returns the next character from an array of bytes
 // A character is a rune along with any accompanying combining runes
 func DecodeCharacter(b []byte) (rune, []rune, int) {
@@ -13,7 +23,7 @@ func DecodeCharacter(b []byte) (rune, []rune, int) {
        c, s := utf8.DecodeRune(b)
 
        var combc []rune
-       for unicode.In(c, unicode.Mark) {
+       for isMark(c) {
                combc = append(combc, c)
                size += s
 
@@ -32,7 +42,7 @@ func DecodeCharacterInString(str string) (rune, []rune, int) {
        c, s := utf8.DecodeRuneInString(str)
 
        var combc []rune
-       for unicode.In(c, unicode.Mark) {
+       for isMark(c) {
                combc = append(combc, c)
                size += s
 
@@ -50,7 +60,7 @@ func CharacterCount(b []byte) int {
 
        for len(b) > 0 {
                r, size := utf8.DecodeRune(b)
-               if !unicode.In(r, unicode.Mark) {
+               if !isMark(r) {
                        s++
                }
 
@@ -66,7 +76,7 @@ func CharacterCountInString(str string) int {
        s := 0
 
        for _, r := range str {
-               if !unicode.In(r, unicode.Mark) {
+               if !isMark(r) {
                        s++
                }
        }