News for hackers. Stuff that matters.
Re:Go-Restructure: Sane regular expressions with struct fields by tptacek on Monday January 25, 2016 @08:02AM (#10966915)
This depends on reflect. What's the speed penalty?
Re:Go-Restructure: Sane regular expressions with struct fields by laumars on Monday January 25, 2016 @10:00AM (#10967484)
I came to the comments section to ask the same question. I've always liked the idea of reflection but avoided it because of performance scares (and usually I can solve the problem another way).

Since we're discussing the performance footprint of regular expression libraries, it's also worth mentioning how slow even just running the base regexp library can be. For example, the email example could also be written without regex:

    indices := strings.Split("[email protected]", "@")
    fmt.Println("Name:", indices[0])
    fmt.Println("Domain:", indices[1])
( https://play.golang.org/p/ZezcoBjc9v )

Obviously the above code doesn't do any sanity checks - which is where regular expressions can often make things easier. But the above would run a lot faster than a regexp pattern match.

Re:Go-Restructure: Sane regular expressions with struct fields by telotortium on Monday January 25, 2016 @01:29PM (#10968872)
I'm pretty sure you can't avoid it in the compilation phase, but it seems like you could remove reflection from the run phase by determining the offset of the struct field corresponding to each capture group and using unsafe operations to access the fields in order to write them.
Re:Go-Restructure: Sane regular expressions with struct fields by aikah on Monday January 25, 2016 @09:10AM (#10967224)
That's a very good question indeed, a benchmark should be added just for reference.
Re:Go-Restructure: Sane regular expressions with struct fields by aabbccddee on Monday January 25, 2016 @10:21AM (#10967616)
It's relatively slow in general. There is also likely a win by compiling the expression because reflect type is slower than reflect value and rt.Field(i) performs an allocation.