neugram / ng

scripting language integrated with Go
https://neugram.io
BSD 2-Clause "Simplified" License
917 stars 43 forks source link

Import regexp is slow #195

Open cflewis opened 6 years ago

cflewis commented 6 years ago

When I run

ng> import "regexp"

It takes a decent 3 to 4 seconds.

When I run

ng> import "strings"

it's as good as instant.

Even simple programs get hurt by this: If I wanted to write something that gets the path to a known parent directory (google3), I'd need to write:

#! /usr/bin/env ng

import "regexp"

pwd := $$ echo $PWD $$
import "regexp"
r := regexp.MustCompile("^.*google3")
s := r.FindString(pwd)
$$ echo $s $$

Is it simply because regexp has more imports than strings, or does strings have some special handling? Could regexp get that handling too?

P.S. why does fmt.Println(s) return

/foo/bar/google3
int(46)

?

sbinet commented 6 years ago

Hi Chris, I don't have any insights as to why importing 'regexp' is so much slower than other packages. I do suspect the same issue than you do though.

I can answer one thing though: 'fmt.Printf' returns the number of bytes written and an error. As the error is 'nil', it's elided from the printout. You do get the number of bytes though. Then you get the message being printed out.

Note there are built-in 'print' and 'printf' functions in ng that don't return '(int, error)'. You'd probably want to use those instead in scripts.