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

refactor: use for loop replace spread to fix stack overflow #15

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

zzuu666
Copy link

@zzuu666 zzuu666 commented Dec 9, 2024

Initial checklist

  • I read the support docs
  • I read the contributing guide
  • I agree to follow the code of conduct
  • I searched issues and discussions and couldn’t find anything or linked relevant results below
  • I made sure the docs are up to date
  • I included tests (or that’s not needed)

Description of changes

Main change is use for replace w/ spread opreator [...slice]

When I try to parse a large markdown file, about 1.6 Mb, I meet an issue RangeError: Maximum call stack size exceeded

image

I debugged the source code and found that the error came from the slice line. I suspected that the spread operator caused the stack overflow.

image

After, change it to for, the issue has fixed.

@github-actions github-actions bot added 👋 phase/new Post is being triaged automatically 🤞 phase/open Post is being triaged manually and removed 👋 phase/new Post is being triaged automatically labels Dec 9, 2024
Copy link

codecov bot commented Dec 9, 2024

Welcome to Codecov 🎉

Once you merge this PR into your default branch, you're all set! Codecov will compare coverage reports and display results in all future pull requests.

Thanks for integrating Codecov - We've got you covered ☂️

events.length = 0

let slice = vecs.pop()

while (slice) {
events.push(...slice)
// NOTE: do not use spread operator here, it will cause a stack overflow.
for (let i = 0; i < slice.length; i++) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a loop could could significant negative performance impact.
Could concat be used instead?

See:
micromark/micromark#187
micromark/micromark@acc135e

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I try to use Array.contact to replace for loop, but the event is a parameter, I just can modify it in-place, do you have any recommend, thx.

events.length = 0

let slice = vecs.pop()

while (slice) {
events.push(...slice)
// NOTE: do not use spread operator here, it will cause a stack overflow.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better than a comment, would be a test that fails with the current logic and passes after

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I add a large markdown file to test/fixtures, it will fail when run the original codes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, could we remove this comment now, since the test will cover it?

@zzuu666 zzuu666 force-pushed the fix_stack_overflow branch 2 times, most recently from 82a2173 to 13a0754 Compare December 10, 2024 06:13
events.length = 0

let slice = vecs.pop()

while (slice) {
events.push(...slice)
// NOTE: do not use spread operator here, it will cause a stack overflow.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, could we remove this comment now, since the test will cover it?

@zzuu666
Copy link
Author

zzuu666 commented Dec 15, 2024

sure, I have removed unnecessary comment, thank you for the review and your time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤞 phase/open Post is being triaged manually
Development

Successfully merging this pull request may close these issues.

2 participants