-
Notifications
You must be signed in to change notification settings - Fork 228
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
Make take and drop more symmetric #1183
Conversation
Allow `take` from the end of bytes or indexed (as `drop` does). Allow `drop` from fibers (as `take` does).
Return table for `take` of dictionary types. Allow `drop` of dictionary types.
Increase efficiency for `take` and `drop` with slices. Check indexed types before bytes types.
I tried the code out. Below are some preliminary remarks. FWIW, I did something like: (def ind (range 200))
## take
(def take-results @[])
(for i 0 10
(def start (os/clock))
(repeat 10_000_000 (take 100 ind))
(array/push take-results
(- (os/clock) start))) So I could see individual batch run results. For However, for If I throw out outlier-ish things In any case, I presume the main point of this PR is symmetry (though of course it's nice to get a boost for some cases and/or maintain a similar level of efficiency for others). A rather minor thing -- there was a change that got lost in one of zevv's recent PRs and it looks like the same lines are touched here so I wonder if we might make a change: 472ec73#diff-e120880268b4f0f04177470180f50ee0d2c7ac13cb83bb778b6d81efda1cbbccR4171-R4172 If you don't mind, may be we can go for one of the suggested options. No big deal though, just noticed a chance to tweak :) |
If we're going the route of handling dictionaries [1] for Not sure though, just bringing it up. If so, I guess it would make sense to do similarly for If those changes were made then [1] I'm in favor of leaving the functionality undocumented initially :) |
I appreciate the feedback!
For profiling, I use something similar to this: timeit.janet (defmacro timeit
``Similar to `loop`, but outputs performance statistics after completion.``
[head & body]
(with-syms [clk cnt elp run]
~(do
(var ,cnt 0)
(def ,clk (os/clock))
(loop ,head (++ ,cnt) ,;body)
(def ,elp (- (os/clock) ,clk))
(def ,run (/ ,elp ,cnt))
(cond
(< ,run 1e-3) (printf "elapsed %fs, %.4gµs/body" ,elp (* ,run 1_000_000))
(< ,run 1) (printf "elapsed %fs, %.4gms/body" ,elp (* ,run 1_000))
(printf "elapsed %fs, %.4gs/body" ,elp ,run)))))
(def ind (range 200))
(timeit [:repeat 10_000_000]
(drop 100 ind))
I remembered to run my changes through Done. |
As suggested by @sogaiu @zevv forget to push this change in a recent PR (janet-lang#1175 (comment)). Incidentally, the affected lines were already reformatted in the current PR, via fmt/format-file.
All other functions that accept a predicate ( |
Related issue: #1178
Allow
take
from the end of bytes or indexed (asdrop
does).Allow
drop
from fibers (astake
does).Return table for
take
from dictionary types.Allow
drop
from dictionary types (astake
does).Increase efficiency for
take
anddrop
with slices.Check indexed types before bytes.
It isn't clear to me that there is a common use case for taking and dropping from dictionary types. However, the implementation is relatively straight-forward, particularly for take, so I don't know that it should be explicitly forbidden either.
take
for indexed types isn't slowed any by this change, anddrop
has become significantly faster.take
has become slightly slower forbytes
objects due to the type check reordering. I believe that indexed types are a more common case, and in particular when efficiency is a concern.