regex - 在Go中优化正则表达式的内存消耗

标签 regex go optimization memory

在Go中使用正则表达式时,我遇到了巨大的内存问题:

Showing top 20 nodes out of 34
      flat  flat%   sum%        cum   cum%
    1.53GB 25.03% 25.03%     1.53GB 25.03%  regexp/syntax.(*compiler).inst
    1.43GB 23.29% 48.31%     1.43GB 23.29%  regexp/syntax.(*parser).newRegexp
    1.10GB 17.99% 66.31%     1.10GB 17.99%  regexp.onePassCopy
    0.53GB  8.67% 74.97%     0.53GB  8.67%  regexp/syntax.(*Regexp).Simplify
    0.39GB  6.44% 81.41%     0.90GB 14.74%  regexp.makeOnePass
    0.29GB  4.76% 86.17%     0.29GB  4.76%  regexp.newQueue
    0.19GB  3.13% 89.30%     0.22GB  3.54%  regexp.makeOnePass.func1
    0.17GB  2.79% 92.09%     6.10GB 99.54%  regexp.compile
    0.14GB  2.22% 94.31%     0.25GB  4.09%  regexp/syntax.(*parser).collapse
    0.14GB  2.21% 96.52%     0.14GB  2.21%  regexp/syntax.(*parser).push
    0.10GB  1.66% 98.17%     1.80GB 29.37%  regexp/syntax.Parse
    0.04GB  0.69% 98.86%     0.10GB  1.59%  regexp/syntax.(*compiler).init
         0     0% 98.86%     6.13GB   100%  bicctopostgres/app.Test_prepareRecordsToInsert
         0     0% 98.86%     6.10GB 99.54%  bicctopostgres/app.TypeIs
         0     0% 98.86%     6.10GB 99.54%  bicctopostgres/app.evalColumn
         0     0% 98.86%     6.11GB 99.74%  bicctopostgres/app.getColumnsHeaderData
         0     0% 98.86%     1.42GB 23.12%  bicctopostgres/app.isDate
         0     0% 98.86%     0.61GB 10.00%  bicctopostgres/app.isInteger
         0     0% 98.86%     1.09GB 17.85%  bicctopostgres/app.isReal
         0     0% 98.86%     2.98GB 48.58%  bicctopostgres/app.isTimestamp

我正在构建一个程序来解析csv文件,检查每列是否为空和数据类型(最后一位由regex组成),生成Postgres表并加载记录。创建数据库表时,将忽略无数据的列。

但是我有一个巨大的瓶颈,仅凭2000条记录,我就只能将6GB的内存用于正则表达式相关的东西。

这是用于检查数据是否为时间戳记的函数示例:
//isTimestamp returns true if the string is exacly: 9999-99-99 99:99:99.999999 .
func isTimestamp(s *string) bool {
    e := `(?i)^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{6}$`
    var isDesired = regexp.MustCompile(e)
    return isDesired.MatchString(*s)
}

然后检查类型的主要功能是:
//TypeIs returns the underlying data type of the string, if it's a string also returns the string length, otherwise is zero.
func TypeIs(s *string) (dataType string, stringLength int) {
    if isInteger(s) {
        return "INTEGER", 0
    }
    if isReal(s) {
        return "REAL", 0
    }
    if isDate(s) {
        return "DATE", 0
    }
    if isTimestamp(s) {
        return "TIMESTAMP", 0
    }
    return "TEXT", len(*s)
}

也许我的正则表达式语句太糟糕了?它们起作用,但是也许它太强力了。

有小费吗?谢谢!

最佳答案

您可以在函数外部编译正则表达式吗?

var isDesired = regexp.MustCompile(`(?i)^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{6}$`)

func isTimestamp(s *string) bool {
    return isDesired.MatchString(*s)
}

关于regex - 在Go中优化正则表达式的内存消耗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61192610/

相关文章:

php - 无法从 .htaccess 生成的 url 获取变量

regex - R grepl找到一个纯数字

search - 包含 slice 的结构集

R优化(): unexpected behavior when working with parent environments

c# - 为 asp.net asp :RegularExpressionValidator 形成正则表达式

sockets - Go:从套接字读取时出现意外的 EOF

go - 如何在 Go 包和文件夹中构建代码?

c++ - 优化C++中的函数指针和虚函数

ruby - 为什么在 Ruby 中使用 String#count 比使用 String#chars 计算字母更快?

javascript - 带连字符的 YouTube 网址测试器