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

Experimental Stackguard #1566

Merged
merged 15 commits into from Jul 2, 2023
Merged

Experimental Stackguard #1566

merged 15 commits into from Jul 2, 2023

Conversation

Xicy
Copy link
Contributor

@Xicy Xicy commented Jun 26, 2023

Extend Stack overflow #1159

Jint.Benchmark.SunSpiderBenchmark

Diff Method FileName Mean Error Allocated
Old Run 3d-cube 507.0 ms 0.50 ms 45002.7 KB
New 514.5 ms (+1%) 1.41 ms 45002.7 KB (0%)
Old Run 3d-morph 369.2 ms 0.89 ms 46270.99 KB
New 369.6 ms (0%) 6.06 ms 46270.99 KB (0%)
Old Run 3d-raytrace 393.1 ms 1.18 ms 86663.02 KB
New 416.6 ms (+6%) 1.55 ms 86663.95 KB (0%)
Old Run access-binary-trees 184.1 ms 0.47 ms 62246.95 KB
New 185.2 ms (+1%) 0.85 ms 62246.95 KB (0%)
Old Run access-fannkuch 1,143.4 ms 2.64 ms 140.84 KB
New 1,155.2 ms (+1%) 3.39 ms 140.84 KB (0%)
Old Run access-nbody 451.6 ms 0.85 ms 53295.24 KB
New 447.7 ms (-1%) 0.78 ms 53295.37 KB (0%)
Old Run access-nsieve 351.6 ms 1.11 ms 17154.28 KB
New 356.1 ms (+1%) 3.71 ms 17154.28 KB (0%)
Old Run bitop(...)-byte [24] 326.1 ms 0.55 ms 61949.2 KB
New 327.0 ms (0%) 1.40 ms 61951.05 KB (0%)
Old Run bitops-bits-in-byte 512.6 ms 0.63 ms 40557.47 KB
New 513.6 ms (0%) 1.46 ms 40556.67 KB (0%)
Old Run bitops-bitwise-and 316.0 ms 1.31 ms 55944.75 KB
New 314.5 ms (0%) 0.55 ms 55938.98 KB (0%)
Old Run bitops-nsieve-bits 508.8 ms 1.20 ms 53930.86 KB
New 511.9 ms (+1%) 4.17 ms 53929.99 KB (0%)
Old Run contr(...)rsive [21] 236.6 ms 0.57 ms 92773.68 KB
New 240.7 ms (+2%) 1.70 ms 92774.11 KB (0%)
Old Run crypto-aes 337.7 ms 0.56 ms 11040.45 KB
New 334.7 ms (-1%) 1.06 ms 11061.7 KB (0%)
Old Run crypto-md5 229.0 ms 0.78 ms 82159.74 KB
New 228.8 ms (0%) 0.79 ms 82159.88 KB (0%)
Old Run crypto-sha1 229.2 ms 0.97 ms 68861.36 KB
New 231.7 ms (+1%) 0.98 ms 68861.36 KB (0%)
Old Run date-format-tofte 244.0 ms 0.79 ms 49656.32 KB
New 245.0 ms (0%) 1.16 ms 49656.11 KB (0%)
Old Run date-format-xparb 125.7 ms 0.57 ms 26410.94 KB
New 127.3 ms (+1%) 0.59 ms 26410.94 KB (0%)
Old Run math-cordic 805.1 ms 1.68 ms 86858.78 KB
New 746.2 ms (-7%) 1.18 ms 86858.78 KB (0%)
Old Run math-partial-sums 280.0 ms 0.84 ms 49368.26 KB
New 279.6 ms (0%) 0.71 ms 49366.21 KB (0%)
Old Run math-spectral-norm 291.6 ms 5.46 ms 56616.28 KB
New 281.5 ms (-3%) 1.28 ms 56618.43 KB (0%)
Old Run regexp-dna 284.0 ms 2.02 ms 17660.88 KB
New 288.3 ms (+2%) 2.31 ms 17778.14 KB (+1%)
Old Run string-base64 178.7 ms 0.56 ms 9702.74 KB
New 182.2 ms (+2%) 0.43 ms 9701.99 KB (0%)
Old Run string-fasta 377.0 ms 1.28 ms 104772.56 KB
New 372.4 ms (-1%) 1.61 ms 104772.56 KB (0%)
Old Run string-tagcloud 159.3 ms 0.75 ms 41774.3 KB
New 160.8 ms (+1%) 0.74 ms 42457.06 KB (+2%)
Old Run string-unpack-code 166.1 ms 0.44 ms 77691 KB
New 166.5 ms (0%) 1.23 ms 80397.21 KB (+3%)
Old Run strin(...)input [21] 181.9 ms 0.77 ms 39533.89 KB
New 183.7 ms (+1%) 1.54 ms 39533.89 KB (0%)

@Xicy Xicy marked this pull request as ready for review June 28, 2023 08:34
@Xicy
Copy link
Contributor Author

Xicy commented Jun 28, 2023

Hi @lahma, I hope you are well. I'm not sure if the improvement I made will work, but it can prevent stack overflow. Can you review it when you are available? Maybe you can suggest something to increase performance and decrease memory usage.

Copy link
Collaborator

@lahma lahma left a comment

Choose a reason for hiding this comment

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

Cool stuff indeed, I wasn't aware of this StackGuard until you introduced it, thanks! I've added some preliminary comments. I don't object this idea. Probably need to run some performance tests of my own too - the code paths aren't utilized with Jint anyway so it's more like for bigger recursion. Maybe separate test case proving that 10 000 can be achieved?

@@ -9,21 +9,12 @@ public class EngineLimitTests
[Fact]
public void ShouldAllowReasonableCallStackDepth()
{
if (OperatingSystem.IsMacOS())
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think for this test case we might want to disable the stack guard (value -1?) to ensure we don't start writing silly code - this is also a performance test when it comes to stack handling. So we would keep this test as-is expect for configuring engine with disabled stack guard.

Can of course give the script generation improvements (x starts with 1 and result checked).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Disable options (-1) make sense. I add disable option and also kept the actual test case as-is.

new Engine(options => options.Constraints.MaxExecutionStackCount = 1000)
.SetValue("assert", new Action<bool>(Assert.True))
.SetValue("log", new Action<object>(Console.WriteLine))
.Evaluate(@"
Copy link
Collaborator

Choose a reason for hiding this comment

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

maybe no console write lines, they just make test output bad - maybe just return needed data from Evaluate

Copy link
Contributor Author

Choose a reason for hiding this comment

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

log removed.
I used it for testing locally and did not expect to effect test output :)

@Xicy
Copy link
Contributor Author

Xicy commented Jun 28, 2023

Maybe separate test case proving that 10 000 can be achieved?

I updated the test to 10 000 but it takes more time for unit test in my local machine takes 33sec. It is may not ideal for github actions

@lahma
Copy link
Collaborator

lahma commented Jul 1, 2023

Maybe separate test case proving that 10 000 can be achieved?

I updated the test to 10 000 but it takes more time for unit test in my local machine takes 33sec. It is may not ideal for github actions

OK I might have been overly ambitious, maybe test case should have FunctionNestingCount * 2 to prove that current max change be remedied by using the new switch?

/// <summary>
/// Maximum recursion stack count, defaults to -1 (as-is dotnet stacktrace).
/// </summary>
public int MaxExecutionStackCount { get; set; } = StackGuard.Disabled;
Copy link
Collaborator

@lahma lahma Jul 1, 2023

Choose a reason for hiding this comment

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

I think the old comment had valuable info 14000 which comes from Chrome and V8 based engines (ClearScript) that can handle 13955. So might be good to indicate that option will allow more nested calls with slight performance / stack trace readability drawback. (after hitting the engine's own limit), The old info gave reasonable limit for expectations without people resorting to int.MaxValue.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I updated it as remarks. If you have another suggestion, please share it with me. It will be a pleasure for me to improve it.

@lahma
Copy link
Collaborator

lahma commented Jul 1, 2023

Looking good, I gave a few more comments for your consideration, sorry for the delay.

Copy link
Collaborator

@lahma lahma left a comment

Choose a reason for hiding this comment

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

Great work, thanks for iterating the solution!

@lahma lahma merged commit db73cad into sebastienros:main Jul 2, 2023
3 checks passed
@Xicy Xicy deleted the feature/stackgruad branch July 2, 2023 08:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants