Skip to content

Commit

Permalink
use builtin len instead of viml_len to avoid allocations
Browse files Browse the repository at this point in the history
benchmark                            old ns/op     new ns/op     delta
BenchmarkVimLParser_VimLParser-4     522557385     384606362     -26.40%
  • Loading branch information
haya14busa committed Sep 14, 2016
1 parent 36af432 commit ce9df41
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 66 deletions.
6 changes: 3 additions & 3 deletions go/_test/let.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@ if x {
func F() {
var cmd *Cmd = nil
for _, x := range builtin_commands {
if viml_stridx(x.name, name) == 0 && viml_len(name) >= x.minlen {
if viml_stridx(x.name, name) == 0 && len(name) >= x.minlen {
cmd = x
break
}
}
if self.neovim {
for _, x := range neovim_additional_commands {
if viml_stridx(x.name, name) == 0 && viml_len(name) >= x.minlen {
if viml_stridx(x.name, name) == 0 && len(name) >= x.minlen {
cmd = x
break
}
}
for _, x := range neovim_removed_commands {
if viml_stridx(x.name, name) == 0 && viml_len(name) >= x.minlen {
if viml_stridx(x.name, name) == 0 && len(name) >= x.minlen {
cmd = nil
break
}
Expand Down
2 changes: 2 additions & 0 deletions go/gocompiler.vim
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,8 @@ function s:GoCompiler.compile_call(node)
if index(s:viml_builtin_functions, left) != -1
if left == 'add'
let left = 'append'
elseif left == 'len'
let left = 'len'
else
let left = printf('viml_%s', left)
endif
Expand Down
2 changes: 1 addition & 1 deletion go/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func (self *Compiler) __init__() {
}

func (self *Compiler) out(f string, args ...interface{}) {
if viml_len(args) == 0 {
if len(args) == 0 {
if string(f[0]) == ")" {
self.lines[len(self.lines)-1] += f
} else {
Expand Down
6 changes: 1 addition & 5 deletions go/vimlfunc.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func init() {
type vimlList interface{}

func viml_empty(obj interface{}) bool {
return viml_len(obj) == 0
return reflect.ValueOf(obj).Len() == 0
}

func viml_equalci(a, b string) bool {
Expand Down Expand Up @@ -125,10 +125,6 @@ func viml_join(lst vimlList, sep string) string {
return strings.Join(ss, sep)
}

func viml_len(obj interface{}) int {
return reflect.ValueOf(obj).Len()
}

func viml_printf(f string, args ...interface{}) string {
return fmt.Sprintf(f, args...)
}
Expand Down
20 changes: 0 additions & 20 deletions go/vimlfunc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,6 @@ import (
"testing"
)

func TestViml_len(t *testing.T) {
tests := []struct {
in interface{}
want int
}{
{in: "hoge", want: 4},
{in: "あいうえお", want: 15},
{in: "", want: 0},
{in: []string{"hoge", "foo"}, want: 2},
{in: []int{1, 2, 3}, want: 3},
{in: []interface{}{1, "2", float64(3)}, want: 3},
}
for _, tt := range tests {
got := viml_len(tt.in)
if got != tt.want {
t.Errorf("viml_len(%v) = %v, want %v", tt.in, got, tt.want)
}
}
}

func TestViml_eqreg(t *testing.T) {
tests := []struct {
in string
Expand Down
74 changes: 37 additions & 37 deletions go/vimlparser.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ce9df41

Please sign in to comment.