Skip to content

Commit

Permalink
Merge pull request #4 from haya14busa/performance
Browse files Browse the repository at this point in the history
Improve performance
  • Loading branch information
haya14busa authored Sep 14, 2016
2 parents 91d84c9 + 1dc4167 commit 33fd88f
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 103 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
4 changes: 4 additions & 0 deletions go/_test/special.vim
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ function! s:ExprTokenizer.token()
" skip ExprTokenizer.token
endfunction

function! s:StringReader.__init__()
" skip StringReader.__init__
endfunction

function! s:StringReader.getpos()
" skip StringReader.getpos
endfunction
Expand Down
4 changes: 3 additions & 1 deletion go/gocompiler.vim
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ function s:GoCompiler.compile_function(node)
let [_0, struct, name; _] = matchlist(left, '^\(.*\)\.\(.*\)$')
if name == 'new'
\ || (struct == 'ExprTokenizer' && name == 'token')
\ || (struct == 'StringReader' && name == 'getpos')
\ || (struct == 'StringReader' && (name == 'getpos' || name == '__init__'))
\ || (struct == 'VimLParser' && (name =~ '\(push\|pop\)_context\|__init__'))
\ || (struct == 'Compiler' && (
\ name == '__init__'
Expand Down 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
52 changes: 47 additions & 5 deletions go/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func NewLvalueParser(reader *StringReader) *LvalueParser {

type StringReader struct {
i int
pos [][]interface{} // (lnum, col int)
pos []pos
buf []string
}

Expand All @@ -177,10 +177,52 @@ func NewStringReader(lines []string) *StringReader {
return obj
}

func (self *StringReader) __init__(lines []string) {
size := 0
for _, l := range lines {
size += len(l) + 1 // +1 for EOL
}
self.buf = make([]string, 0, size)
self.pos = make([]pos, 0, size+1) // +1 for EOF
var lnum = 0
for lnum < len(lines) {
var col = 0
for _, r := range lines[lnum] {
c := string(r)
self.buf = append(self.buf, c)
self.pos = append(self.pos, pos{lnum: lnum + 1, col: col + 1})
col += len(c)
}
for lnum+1 < len(lines) && viml_eqregh(lines[lnum+1], "^\\s*\\\\") {
var skip = true
col = 0
for _, r := range lines[lnum+1] {
c := string(r)
if skip {
if c == "\\" {
skip = false
}
} else {
self.buf = append(self.buf, c)
self.pos = append(self.pos, pos{lnum: lnum + 2, col: col + 1})
}
col += len(c)
}
lnum += 1
}
self.buf = append(self.buf, "<EOL>")
self.pos = append(self.pos, pos{lnum: lnum + 1, col: col + 1})
lnum += 1
}
// for <EOF>
self.pos = append(self.pos, pos{lnum: lnum + 1, col: 0})
self.i = 0
}

func (self *StringReader) getpos() *pos {
var p = self.pos[self.i]
var lnum, col = p[0].(int), p[1].(int)
return &pos{i: self.i, lnum: lnum, col: col}
p := self.pos[self.i]
p.i = self.i
return &p
}

type Compiler struct {
Expand All @@ -200,7 +242,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
8 changes: 2 additions & 6 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 All @@ -143,7 +139,7 @@ func viml_range(start, end int) []int {

func viml_split(s string, sep string) []string {
if sep == `\zs` {
var ss []string
ss := make([]string, 0, len(s))
for _, r := range s {
ss = append(ss, string(r))
}
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
Loading

0 comments on commit 33fd88f

Please sign in to comment.