Skip to content

[Refactor]: get rid of constant package in our parser and remove some useless code - #18123

Merged
mergify[bot] merged 8 commits into
matrixorigin:mainfrom
jensenojs:clean-parser
Aug 19, 2024
Merged

mergify[bot] merged 8 commits into
matrixorigin:mainfrom
jensenojs:clean-parser

Conversation

@jensenojs

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

issue #17733

What this PR does / why we need it:

constant.Value is heavy

@matrix-meow matrix-meow added the size/XXL Denotes a PR that changes 2000+ lines label Aug 14, 2024
@mergify mergify Bot added the kind/refactor Code refactor label Aug 14, 2024
@jensenojs

Copy link
Copy Markdown
Contributor Author

核心的修改是针对NumVal的构造函数, 被大规模使用的实际上是NewNumValWithType, 其他的构造函数几乎没有被使用. 使用. 这个pr修改了NumVal的结构, 移除掉了constant.Value的使用. 并且把一些实际上没有意义的代码给清理掉了

// before
func NewNumValWithType(value constant.Value, origString string, negative bool, typ P_TYPE) *NumVal {
 	numVal := &NumVal{
 		Value:      value,
 		origString: origString,
 		negative:   negative,
 		ValType:    typ,
 	}
 	return numVal
 }

// now
func NewNumVal[T bool | int64 | uint64 | float64 | string](val T, originString string, negative bool, typ P_TYPE) *NumVal {
 	nv := &NumVal{
 		ValType:    typ,
 		negative:   negative,
 		origString: originString,
 	}

 	switch v := any(val).(type) {
 	case bool:
 		if typ != P_bool {
 			logutil.Fatalf("unexpected type %T", v)
 		}
 		nv.resBool = v
 	case int64:
 		if typ != P_int64 {
 			logutil.Fatalf("unexpected type %T", v)
 		}
 		nv.resInt64 = v
 	case uint64:
 		if typ != P_uint64 {
 			logutil.Fatalf("unexpected type %T", v)
 		}
 		nv.resUint64 = v
 	case float64:
 		if typ != P_float64 {
 			logutil.Fatalf("unexpected type %T", v)
 		}
 		nv.resFloat64 = v
 	case string:
 		// do nothing as val already store in origString
 	default:
 		logutil.Fatalf("unexpected type %T", v)
 	}

 	return nv
 }

@jensenojs

jensenojs commented Aug 14, 2024 •

Copy link
Copy Markdown
Contributor Author

这个重构的对比可以在下面这个issue的case中体现出来, 跑的是sysbench的bulk_insert, 内存占用从6g降低到了5.2g

before(inuse-space):

image

job link : https://github.com/matrixorigin/mo-auto-test/actions/runs/10347957971/job/28639392466

now(inuse-space) :

image

job link : https://github.com/matrixorigin/mo-auto-test/actions/runs/10384921917/job/28753153611

heap文件

issue17733_heap.zip

结论

parser的小对象最多的是ast的叶子节点, NumVal(StrVal)就是例子, 这个pr其实就是用几个变量去省了一个接口(constant.Value) 后面要缓解gc问题的话, 如果parser是瓶颈那还是要看怎么复用这些小对象.

如果要用全局的reuse pool来复用这些小对象的话, 以前是从上到下地去做复用, 这样的工作量非常的大, 相关的issue已经给close掉了.

后面要缓解gc问题的话, 如果parser是瓶颈那还是要看怎么复用这里的小对象, 一个实现方案是 : 还是递归, 但是我们只复用几个叶子节点就算了, 中间的节点交给gc去处理. 就不像之前有一个递归的Free的操作.

等regression 跑的结果也没问题后再请大家review

https://github.com/matrixorigin/ci-test/actions/runs/10385841443

@jensenojs

Copy link
Copy Markdown
Contributor Author

后面如果用go.1.23新出的unique的话, 可能可以再把parser的内存占用给压低下来一些,
https://tip.golang.org/pkg/unique

@mergify
mergify Bot merged commit 7bc9289 into matrixorigin:main Aug 19, 2024
@jensenojs
jensenojs deleted the clean-parser branch August 19, 2024 06:27
@jensenojs
jensenojs restored the clean-parser branch August 19, 2024 08:01
@jensenojs
jensenojs deleted the clean-parser branch August 22, 2024 08:35
fengttt pushed a commit to fengttt/matrixone that referenced this pull request May 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/refactor Code refactor size/XXL Denotes a PR that changes 2000+ lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants