forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PR JuliaLang#51635 introduced function `check_src_module_wrap` which checks whether a module declaration is missing or whether a `using`/`import` is declared before a module. The original implementation, based on manual checks via regexes, did not catch all possible cases. In particular, it did not consider multi-line comments, e.g., ``` using Foo =# module Bar end ``` threw errors even though it should be valid. Single-line comments, multi-line comments, and docstrings make it quite difficult to reliably check that a module is declared and not preceded by any using/import. Some examples are ``` module B end =# using A =# module B end using A =# module B end "docs" module B end ``` Because of that, this commit avoids all manually-written, specialized conditions and employs `Meta.parse` to parse the code into `Expr`s. It then becomes very simple to check the condition of interest. The downside is increased runtime. [WITHOUT the shortcut; see bellow] I measured the time it takes to check three files of different sizes (the first measurements is the current version, the second is master): ``` # 63979 lines of code @Btime Base.check_src_module_wrap(Base.PkgId("dummy"), "LibSCIP.jl") 102.267 ms (535826 allocations: 36.19 MiB) 5.630 μs (8 allocations: 528 bytes) # 1051 lines of code @Btime Base.check_src_module_wrap(Base.PkgId("dummy"), "Primes.jl") 3.525 ms (21503 allocations: 1.08 MiB) 5.946 μs (9 allocations: 656 bytes) # 79 lines of code @Btime Base.check_src_module_wrap(Base.PkgId("dummy"), "BenchmarkTools.jl") 106.170 μs (578 allocations: 46.02 KiB) 3.240 μs (8 allocations: 560 bytes) ``` The slowdown is *dramatic* (most of the time is spent reading the source file into a String). I added a condition that checks if the file content starts with "module ", in which case the function returns immediatelly. This shortcut helps: LibSCIP.jl takes only 5.865 μs (instead of 102.265 ms). Of course, the shortcut is taken only in some cases... I am submitting this PR anyway for discussion. Fixes JuliaLang#55792
- Loading branch information
Showing
2 changed files
with
99 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters