-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
allow import
inside block
: makes N runnableExamples run N x faster, minimizes scope pollution
#9300
Comments
import
inside block: makes N runnableExamples run N x faster, minimizes scope pollutionimport
inside block
: makes N runnableExamples run N x faster, minimizes scope pollution
Terrible. Took D specialized scoping rules to recover from this feature. |
From my experience with that feature in scala, I think it is not a good feature. When you have this feature, people will optimize "scope pollution" as you said it. But there isn't a real benefit to it, it's just a waste of time. Initially I did optimize this "scope pollution" but after I started using intellij which handles global imports automatically and I never had problems with it, I could dismiss local imports easily and never look back. |
my proposal addresses the speed problem of running eg: |
well I guess it is easy to support an import statement in the |
that's already supported, and that was (IIRC) the reason why [1] eg of one type of problem that can happen if runnableExamples are run in 1 single file without this proposal: proc foo1()
runnableExamples:
import bar
foo1(barfoo)
...
proc foo2()
runnableExamples:
# oups, forgot to `import bar`
foo1(barfoo)
but if a user copy pastes runnableExamples from foo2 into an editor, it will fail because With this proposal, this would all work (and have the Nx speed benefit mentioned) |
We can also only split the runnable examples into multiple files if different |
Sounds like a good idea. But if it's done, It should be mentioned in the documentation of runnableExamples so that users have a chance to optimize the runnable examples to have identical imports. |
here's some benchmark: before 0862294gtime -v nim doc lib/pure/strutils.nim after:Command being timed: "./bin/nim doc lib/pure/strutils.nim" so that's a huge time saving, and one that'll only grow over time if runnableExamples becomes more common (which I hope). Note: the previous issues we had with running all runnableExamples in a single file (eg causing some conflicts eg symbol clashes with multiple imports) are avoided in that commit by 1 level of indirection, each runnableExample still only has its own imports and nothing more, eg:
so, thanks @Araq for the fix! |
proposal
allow
import
statements inside ablock
, as in following code:It currently gives:
Error: 'import' is only allowed at top level
.rationale
nim doc foo.nim
where foo.nim contains NrunnableExamples
blocks will generate N files and N compilationsI measured (on synthetic test cases; I can share code if needed) that running a single compilation+run on a file that concatenates N runnable tests runs N times faster than running the N runnable tests separately, since the overhead is almost always compilation step (+ overhead of starting a process etc). For N=50 (not unreasonable) that's a 50X speedup.
As we improve our docs with more runnableExamples, this problem is gonna become more and more pressing. I often run into timeout issues on travis already (#8707), and appveyor is still taking often > 8 hours to process a build (https://github.com/nim-lang/Nim/issues/8640)
allowing
import
inside ablock
can improve code health in a number of cases, avoiding symbol pollution and symbol clashes from too many top-level importsEDIT likewise with tests: this could be used for PR's like Merge tests into a larger file (part 1 of ∞) #9318 which refactors several tests into 1 file (again, for improving speed of running tests)
Note
even if
.. code-block:: :test:
were used instead ofrunnableExamples
, the exact same problem would be there (code is same underneath): the reason we're splitting runnableExamples into seperate files currently is to supportimport
inside runnableExamples, which is needed for various reasons (eg allowing importing other modules inside a runnableExamples block without polluting the original module amongst other use cases, and making each runnableExamples independent scope-wise).The text was updated successfully, but these errors were encountered: