Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to impove performance for a amount of constant literal in a javascript file #566

Closed
pkujhd opened this issue Apr 15, 2024 · 4 comments
Closed

Comments

@pkujhd
Copy link

pkujhd commented Apr 15, 2024

I use goja as a runtime for execute javascript file. but when require text-encoding modules into goja's runtime. It's incredibly slow.
I profile it, found most cost of cpu was in Goja (* Program). defineLiteralValue.
the modules text-encoding has a amount of constant literal. Is there a way to improve performance?

thx~

@pkujhd
Copy link
Author

pkujhd commented Apr 16, 2024

I disable cache lookup with Program.values, the performance is same as node.

Could you give an option for goja.Compile to disable the cache lookup?

@dop251
Copy link
Owner

dop251 commented Apr 16, 2024

Hi. Could you provide more details about the code you're running and how you're running it, and the change you made?

@pkujhd
Copy link
Author

pkujhd commented Apr 17, 2024

Hi. Could you provide more details about the code you're running and how you're running it, and the change you made?

@dop251
I execute javascript string "require(text-encoding)"
text-encoding is a npm package (https://www.npmjs.com/package/text-encoding)

when I change

func (p *Program) defineLiteralValue(val Value) uint32 {
	for idx, v := range p.values {
		if v.SameAs(val) {
			return uint32(idx)
		}
	}
	idx := uint32(len(p.values))
	p.values = append(p.values, val)
	return idx
}

to

func (p *Program) defineLiteralValue(val Value) uint32 {
	// for idx, v := range p.values {
	// 	if v.SameAs(val) {
	// 		return uint32(idx)
	// 	}
	// }
	idx := uint32(len(p.values))
	p.values = append(p.values, val)
	return idx
}

the performance is close to node. this change disable cache lookup for literal values in Program

becasue text-encoding/lib/encoding-indexes.js have a amount of literal, every literal traverses search Program.values with many items, every item's compare it with SameAs function( it call reflect to cast type). it is a slow mechanism in this case

@pkujhd
Copy link
Author

pkujhd commented May 17, 2024

thanks, it's working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants