Comments (3)
Hi
Sorry for the delay but it took some time before I could address this issue.
The problem comes from inference of the type of your sieve
. Because a @resumable function
is a macro it can not include the runtime type of an object. You can remedy this problem by specifying it manually:
@resumable function segmentedsieve(n::Int64)
segmentstart::Int64 = 0
segmentend::Int64 = Int(ceil(sqrt(n)))
initialsieve = basicsieve(segmentend)::BitArray{1}
segment = copy(initialsieve)
lastprime::Int64 = 2
p::Int64 = 3
while p <= n
while p <= segmentend
if segment[p-segmentstart]
@yield lastprime
lastprime = p
end
p+=1
end
segmentstart += length(segment)
segmentend += length(segment)
segmentend = min(segmentend,n)
segment .= true
sievesegment(initialsieve,segment,segmentstart)
end
lastprime
end
This gives on my computer for n=10000
the following benchmark results:
julia> @btime bench1()
130.895 μs (7 allocations: 400 bytes)
julia> @btime bench2()
7.433 ms (3707 allocations: 61.94 KiB)
julia> @btime bench3()
121.794 μs (7 allocations: 4.41 KiB)
If you do a @macroexpand
of the @resumable function
you see what the resulting code is and you can find out what the types are.
Can you keep me informed whether this solves your problem? I think I will include a kind of best practice section in the documentation with your segmented sieve as an example (if you allow me).
Kind regards
Ben
from resumablefunctions.jl.
Ah right, adding type annotations did indeed eliminate the allocations. I'm impressed by how well it performs.
Interestingly enough, the allocations seemed to be coming exclusively from the function argument n being noninferrable. Once I specified the type of n, all allocations disappeared. Maybe it could be made to work for more general function type parameters with generated function and a parametrically typed struct?
Feel free to add the segmented sieve as an example.
Kind regards,
Olof
from resumablefunctions.jl.
Thanks!
Parametric @resumable functions
are possible. I hope that one day the Julia compiler is good enough to avoid these type annotations.
I will close the issue.
Kind regards
Ben
from resumablefunctions.jl.
Related Issues (20)
- Recursive parametric functions troubles?
- Resumable function has stopped HOT 2
- `continue` results in infinite loops HOT 3
- macro clashing with ProgressLogging.jl HOT 1
- Error `@yield macro outside a @resumable function!` when using @yield inside a macro HOT 3
- is this package being maintained? HOT 5
- The CI/CD build is broken HOT 1
- Broken on Julia 1.10 (fix included) HOT 17
- Release v1.0.0? HOT 3
- CI, TagBot, Documenter fixes HOT 1
- a resumable function as a struct field var causes an error "struct expression not at top level"
- How to build fast iterators? HOT 4
- `@resumable` breaks blocks scope HOT 2
- let blocks without variable assignement like `let i; f(i); end` are not supported HOT 1
- significant regression in benchmarks HOT 4
- Functionals that refer to themselves are not supported (because they are not saved to the finite state machine)
- ResumableFunctions v0.6.6 StackOverflow with julia-1.10 HOT 8
- tests for downstream breakage HOT 1
- 0.6.7 breaks resumables with multiple fors over same variable HOT 6
- enable inference in self-referencing resumable functions
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from resumablefunctions.jl.