登录
首页 >  Golang >  Go问答

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

来源:stackoverflow

时间:2024-04-16 13:54:32 434浏览 收藏

哈喽!大家好,很高兴又见面了,我是golang学习网的一名作者,今天由我给大家带来一篇《优化 Go 中正则表达式的内存消耗》,本文主要会讲到等等知识点,希望大家一起学习进步,也欢迎大家关注、点赞、收藏、转发! 下面就一起来看看吧!

问题内容

在 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 文件,检查每一列是否为空以及数据类型(最后一位是用正则表达式创建的),生成 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)
}

以上就是《优化 Go 中正则表达式的内存消耗》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>