The need for speed (fluid.dataset~ + fluid.datasetquery~)

After finally getting to the bottom of things in the Example 11 verbose thread (thanks for your patiences @weefuzzy & @tremblap) I’ve managed to build a super stripped back version of my onset descriptors stuff.

I’ve created a batch analysis patch that analyzes the first 256 samples of my Metal Resonances sample library (3094 individual samples) and creates a 8d dataset. It’s almost identical to the process from example 11 except I only use MFCCs and loudness. I use 20 MFCCs instead of 13 as I got good results with that elsewhere. I still ignore the 0th coefficient. After that I do the same sanitization and PCA-ing that happens there to end up with an 8d space at the end.

For the real-time version, I analyze and post-process everything the same.

The results are pretty good. I haven’t A/B’d it with the vanilla entrymatcher~ version where I use only centroid/loudness/etc…, as I was mainly trying to get this up and running to check out the performance.

The good news is that for a super basic use case, it’s faster than I thought it would be! I got around 3ms average per query, with some spikes above that:

Still about twice as slow as my “full bells and whistles” version that does loads of desriptors, melbands, spectral/loudness compensation, etc… (around 1.5-2ms average) that’s built around entrymatcher~.

But this speed is with no extra funny business. As in, a pre-fit fluid.kdtree~, with no fluid.datasetquery~ on the real-time side.

If I put in a single fluid.datasetquery~ + re-fit-ing of the central fluid.kdtree~ and the numbers jump up. Way up:
Screenshot 2020-10-04 at 10.35.09 pm

This is around the range I was initially expecting.

This is also just a single fluid.datasetquery~. In a more real-world context, there may be a couple of forks, I may have some metadata (timeness) stuff I want to filter by, I have a load of melbands (for the spectral compensation) that I’d want to store somewhere and recall with each query. And that’s not even getting into actual biasing and skewing of an analysis which would require some further data transformation per query.

And on top of this, this is a very modest corpus. Only 3000 entries, with 8 dimensions each. I’ve loaded up corpora up near the 1million range, and my “bells and whistles” version has like 70+ dimensions (though not all are part of a distance measurement).

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

I know this will get faster when things get to the optimization stage (similar to what happened in TB1), with regards to the algorithms/functions/etc… That being said, I am dubious that copying everything in a fluid.dataset~ per-filter-step per-query, and then re-fit-ing a fluid.kdtree~ at the end of that process (again, per-query) will be able to get significantly faster than this.

Perhaps there is a way to cleverly form queries that retains the fit of a fluid.kdtree~ as @weefuzzy hints at here, but the bulk of the speed loss here is coming from fluid.datasetquery~ addcolumn-ing the dataset just once (the fit only adds a fraction of time, on this small dataset).

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

With that I’d like to re-bump the idea of having some kind of fluid.datasetfilter~ that is meant for filter (but not copying) data, that perhaps has a more friendly syntax/interface. And an easier way to get data in and out of fluid.dataset~s, that are quite monolithic at the moment. This would be for use cases where you have some data that corresponds with each entry (say a buffer~ name, duration, melbands/filters, etc…) that you want to be able to get in/out of a unified storage container, without needing to go through all sorts of intermediary container/types.

We would be too, if that was how it worked. Fortunately, it’s only on transform / transformJoin that any data gets copied. Anyway, I’m glad you realise things will get faster in future, and there’s reasons why they’re not yet.

What you’re asking for with the filtering is reference semantics, which aren’t terrible things in and of themselves, but are much harder to reason about both for library writers and creative coders, and aren’t necessarily the solution to all performance problems (IOW, there’s not much to be gained by guessing where bottlenecks are occurring) .

Meanwhile

  • I can’t remember what it is you’re doing that means you have to re-fit the tree on every query, but I’m pretty sure there’d be a way of reformulating things so that you didn’t.

  • As for getting stuff in, I think there’s a variety of ways this could improve. The dictionary input / output will be much smoother in the next release, because the Max wrapper is now taking care of a lot of the lumpy stuff.

1 Like

Isn’t that the central thing that fluid.datasetquery~ is doing? Or rather, in a fluid.datasetquery~ as entrymatcher~ replacement context where you have some fields that you want to filter by before doing an actual distance match. So anything you do with fluid.datasetquery~ would end up in a transform of some kind.

Fair enough. I’m just poking at bits and gleaning from the The Team’s posts on here to figure out what’s going on underneath. You know, with the shadows on the cave’s walls and all that.

In my current workflow, I tend to have steps that narrows down the pool of total available matches before any distance calculations take place.

Like in my test example above, I’ll have 3000 samples, but I’ll apply a filter like duration > 1000 to filter down only to long samples. In entrymatcher~-land, that’s just a step in the process and things carry on. In fluid.datasetquery~-land, if I understand correctly, I would have to transform everything that has duration > 1000 into a new fluid.dataset~ and then fit it from there.

I would also have several of these steps along the way. onsets == 1, duration > 1000, etc…

To use a more @tremblap-esque example, it would be forking off based on analyzed pitch confidence. If the confidence is shit, transform a version of the dataset into one that has no pitch-related columns, and then fit from there.

So in short, there are things I’d want to filter by, and things I’d want to match via distance, and sometimes these will overlap (e.g. pitch > 200 → distance matching on pitch in the remaining entries).

I still need to wrap my head around this, and what would make the most sense. In my entrymatcher~ workflow, I use it as a generic data container too, so holding metadata, melbands-for-filters, etc… along side the descriptors. If dicts are the intermediary format/container, it may very well be keeping all of that kind of shit in dicts and using datasets for distance match-y stuff. That does get sticky where some things are both.

But yes, dict-ing would be super welcome!

Yes, but I read you as being under the impression that every call to addcolumn, filter etc were also copying. They’re not. And, I stress, copying isn’t necessarily a big deal.

I don’t know what order entry matcher does stuff in internally (although I could look). I can tell you that based on eyeballing the fluid.datasetquery code, that it could well be faster to distance match a bunch of candidates first and then thin those out with a query. Whether there’s graceful workflow for that is another matter. (Again, to stress, the fact of making smallish datasets on the fly here shouldn’t necessarily be a concern).

I don’t know in any great detail what the code for that does, as it’s still relatively fresh out of the oven and we’re still bashing away at the interface. I will say though, that even when you do have the code (and perhaps wrote it) that the sources of performance problems can be surprising.

Sounds like some of this could be reordered, or helped by having prefitted trees that already have e.g. no pitch in them. But I don’t see where the the dataset wrangling comes in for real time analysis: are you building new datasets on the fly?

Right. No, no, I get that those are the same. I just meant in chaining together subsequent queries, requiring new copying (if you need a filter to happen before moving on, to further filter that area).

In a performance/fixed context, something like this would make sense. For example, in Kaizo Snare, throughout the performance I go between filtering by duration (duration > 500, or no filtering on duration) and filtering by onsets (onsets == 1, or no filter by onsets), so I could easy pre-“render” four datasets/kdtrees and select what I need, but this couldn’t cover all the bases.

I’m totally down for rethinking the problem and flipping it on its head, but I don’t see how some of the problems could be avoided.

A use case that I’ve been meaning to implement for a while now is mixing descriptor types to widen the navigable descriptor area. For example, taking a slow envelope follower and using that to filter by a timeness descriptor such that the louder/busier I play, the shorter the samples that are being pulled from are. So rather than having discreet datasets/kdtrees, it would be a continuous query ala duration > $1 where $1 = [scaledOutputOfEnvelopeFollower].

What I proposed there though was to make pre-sieving - I would have 2 patch structures/searches/databases for pitch and no pitch for instance.

That’s smart.

I presume there may be other things you’d want to filter for though, which would still require querying/copying along the way.

So I’ve revisited this based on the last geekout session.

From what @a.harker and I believe @weefuzzy have commented on elsewhere, the way the data is structured in fluid.dataset~ stuff isn’t “ideal” for small datasets, but should theoretically scale up better, particularly once a fluid.kdtree~ has been fit.

I also wanted to do a more apples-to-apples comparison than the numbers I had above, as I was comparing separate patches with different functionalities and setups etc…

Here are my findings, with some test code below.

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

At first I wanted to test pure matching speed at different sizes, with a single nearest neighbor (trying to be as consistent between the approaches as possible). I’m generating a 10k point fluid.dataset~ with 8 dimensions, along with a corresponding entrymatcher 10000 8. Both are filled with the same exact data.

I chose 10k as a starting off point since it’s bigger than the 3k above in which entrymatcher was faster, so I wanted to “scale things up” by a factor of three to get the ball rolling.

I’m also comparing the mean of the output and testing hundreds of random points to get a decent average over time.

This is what I get for that (10k points, 8d):
10000_8

This is in line with the numbers in my previous posts with regards to raw matching. My “bells and whistles” patch also does extra analysis, lookups, etc… so this is point-for-point the raw matching speed on the same exact dataset with the same exact test point.

Scaled up to 32k I get this:
32000_8

It’s starting to converge slightly, but still significantly slower.

Big jump up to 100k:
100000_8

Again, an improvement, but still slow. There is convergence though, so that gives me hope. I think around here I’m also at a decent “big corpus” real-world number, and 15ms is definitely on the slow side (for real-time percussion stuff).

Things above this point get interesting. I initially tried jumping right up to 500k but that patch kept hanging. After a bit of troubleshooting I discovered that I could not fit a fluid.kdtree~ with that many entries in it. I’ll make a separate bug thread for this with some hang/crash reports.

So I dialled things back to 200k:
200000_8 - wild peaks

This is the best result yet, in terms of raw matching speed difference. The fit did take quite a while here (more on this below), but I suspect if I kept going this way and started getting into @spluta sized 1mil+ fluid.dataset~s, that a fluid.kdtree~ might start to be as fast as or perhaps at some point even faster than entrymatcher.

I should also note that even though the average is 25ms, the actual time was jumping around like crazy.

I was curious where this “changeover” would start to happen, but I’m unable to fit a fluid.kdtree~ big enough to find out. The trajectory is such that based on the decreasing difference between the two approaches, it would presumably happen.

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Now, I wanted to compare apples-to-apples. What the above tests/comparisons leave out is the fact that entrymatcher can vary the querying/weights/etc… per query. So I went back and tested things with the fluid.dataset~ + fluid.kdtree~ process also incorporating fluid.datasetquery~, which means that fluid.kdtree~ needs to get fit, per query. With the patch from my previous post, with a smaller corpus (3k points), this was considerably slower, but I wanted to see if/where this would start to balance out and converge based on the above tests.

For the sake of consistency I tested the same size datasets (10k, 32k, 100k, 200k) and only added a fluid.datasetquery~ + re-fit per query.

10k points:
10000_transform

Right out of the gate we’re orders of magnitude slower. I ran a ton of points here just to make sure that it wasn’t some spike or other thing going on.

32k points:
32000_transform

At this point I was manually banging individual queries, since it was too slow to use a metro consistently.

Big jump up to 100k points:
100000_transform

Over 5s, and at this point I start getting a pinwheel per query.

Finally up to 200k points:
200000_transform

…a little bit slower.

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

So from my tests here, I can’t scale a fluid.dataset~ + fluid.kdtree~ big enough to mitigate the difference in raw matching speed with entrymatcher using the same data at each step of the way.

It also seems that fit-ing increases exponentially (?) the bigger the fluid.dataset~ gets, so where a fluid.kdtree~ might start getting faster, having to fit per-query bogs that down massively.

I was initially worried that the ‘copying’ step of fluid.datasetquery~ would be too slow for real-time use, I didn’t anticipate that even a pre-fit fluid.kdtree~ was still significantly slower (in an apples-to-apples comparison (while ignoring the variable queries possible with entrymatcher)).

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Here is my test code. For the sake of ease I have multiple versions of the files with the different numbers plugged in, so I’ll just post the 10k versions of both and you can just tweak the numbers as needed.

Here’s the patch for vanilla matching:


----------begin_max5_patcher----------
5045.3oc6cs0iiiic94t+UHXj4spc3cRkmxNYSlEHyr6fLSvffAAEjsYUslV
VxQRt5tmEa+aO7hjrjsjMkMkc0XbUncqRRTjmOdtwy4H5+9aeyr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jTHSWU7ZyBxNP1+xd8Vzp.NDvs
4Zhv.gUk3NG5UdmxnMmpRtaEgquOpLK360qBcl25+hR4SaSRJOYBV1uXNETJ
zFLWFlisE8NSIccrDdoeWW3lak0rn.cDDZpyRuRTillPBtv5mAtgSFiBwgBO
rT8aHdz7svzqj2WfOpilymGetl.ZzxrpSbEHoDtqxMwQJ6x+bbd4mC92eN6T
ADFFFBrdWp3iIUoLmA.XX2B4cPqgNVc7cAVL67.V6qNmICdZhsXS0TnYi+7s
+i29+CLEF0mK
-----------end_max5_patcher-----------

And here’s the patch with fluid.datasetquery~ and re-fit-ing:


----------begin_max5_patcher----------
5109.3oc68r1iiajbed2eEDBweaVk9c2LeHHmyk3.b1WLhcfQfQv.JINyRuT
jJjTytqOb6u8zOnnHkZJ1TpozrvZFfYnHEYWU006p5l+s29lYKx+Tb4rf+of
eM3Mu4u8127F8oTm3M0e9MyVG8okoQk5u1rrsqWDWL6AykjeJIKMtReMX8I2
DUs78IYO+XQ7xJyylQ.yAODfYp+R0GiPyAA+u6eN4aq18fP0m0bppOuI17Tl
M6gfYKhxddVyMlrRCU4K9s2A4yZ.fhn0wUwEOFmEsHUe2.0k96u8sp+7fq3Z
7GkO3cO0p3OowlYo4QqzPwHIBPTngJDJzjAw.jAXejgdIAnY9CKqBVDrXrnH
k.mK.DQHSfgTHhPgRTjnm1wnycZ2PrO0Te34f3qiKKidN9HLuHtLt5gfmRRk
rPAff+4.4zl7GIpDsZUgDHhkmUXk1fN0zOWSAHXyDOH7bm+sSCDdbx+ozsIq
luJpJRRJ9RPYx5MowzxsKjebzr8XgFS4T3bHHb2uP.l+P.jMjx.7PbEOIkGq
TGXmrPt0xDPjPy+ynXMYfLYhAryAUWrspJOazHEDqwFLgqQGxt+5McYPruUm
KYTj.0XwzP.XNt8Obod.FGpPXAeRrjQYmD0qu1S4Eqiz.I6pQLXPwb7g52oJ
xPqevD3zQZnuVIMV4StljFL+qJtFFiMghPX3zSLVludcbV0QVIjmq3yqUjAo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N+88hXxGLwj8KpV64V6rTpxUhgcg9vbi.BI0sRPXHVvqOhKb
ntTrF6jzFcz6D17GdsN5SrlWAqNfSGPfa6kmE9cQ2fh30x6mYgF7FB2504jC
oEgfvTTXWikJM.dednyFgyj4ppno38GezIzZyEpdL6AqGcE8BvQc8dhqpqzg
ayhsegp5ZR2Z0SedOE.lcbpuMca7tWxpNG8OFxqKZ+dkQgdWETM.Vj+wrQCg
WGkjFH7e8yQiG.AnPpomB3bLFYj1PPo6b9G.+th33y.B0TulbpAYJ+a7Ov8C
RMaYUQiF7DD.iYx1GCHANMojEBCo9GF+uhWMZ3i230KrInJPsECeCe+Owoo4
eb7jPD0zRLb0F9rF.oRtR5P8SEMD.DSABf9ZDAzku36xSOCVjlDmPkdBQMcm
TaifdA.2rsXSZ7nbNk.L90fn.VnlGFInLjvQOz3MsWHARkpMz51TZO7pnYQ7
x3jWNck6rwyzDa1dfCztaE8CvYBQQwVLBJ+fLDVC2hH0CFVa+fW220RgfPuZ
IoLNaU4qMKH6Ix9W1yZSqB3P.2TqIBCDV2h6bnW4cph1LTmb2JCWeeTUdv2q
hBcl2F+xp3m1llVMXAVNrYNETJzjLWFliMM8NSJccpBdoVqKb8Wk0DTfJCBM
8YoWQpQiSHAWX7y.2vIiQg3PgGBU+FROZd2G9JY8B7QU1b973q0DPQszQchq
IRRg65ZSbh1t7OmTT84f+smyGJgvvvPfw6RIeLotj4L..C61Hu8ZMzwtiuKg
EyNOBqYoyoqfmBYK2TOEp2pce6e+s++PokcYY
-----------end_max5_patcher-----------

A little addendum here.

When making what I thought was a bug report thread I discovered that fluid.kdtree~ just take a really really long time to fit larger fluid.dataset~s.

I managed to fit a 1mil point fluid.kdtree~ and the converging is still happening. The closest matching so far:
Screenshot 2020-10-09 at 8.53.07 pm

As I noticed in the 200k version above, the duration is super erratic for the kdtree. I guess this is a function of how that tree-based distance matching works in that some may just be closer to stuff (?) so it takes less time, where other things may take longer to match.

The bad news is that it’s still really slow.

This fit took over 20min of pinwheeling to compute, so the corresponding fluid.datasetquery~ version of this would be super unpleasant to test, but coming in at 20min+ per-fit, we’re looking at around 36000 times slower than entrymatcher if you need to filter or touch the data in any way.

Thanks. To be clear: it’s really not surprising in the least that you can’t yet the same (or even remotely so) performance from our kdtree as you can with entry matcher. We’re still in alpha, and still concentrating on questions of interface and architecture, so have paid no attention at all to making things fast, and we won’t be for a while.

I’m not sure what the takeaway should be from the above: KD-Trees are what they are: pretty scalable and performant for up-to-medium-dimensioned data, and not too slow to build. Ours could be (and will eventually be) more efficient. If you’re coming away from this thinking that KDTrees are somehow inherently slower than whatever entrymatcher does, then don’t, because there hasn’t been remotely enough testing to demonstrate that. Also, I should point out that the tests above have very little at all to do with how a dataset itself is structured: the point of a KDTree (and query-able trees in general) is to provide an alternative index on to data.

Re-fitting the tree on every query seems apples-to-apples from a UX perspective, but absolutely isn’t from an algorithm point of view. A KD Tree purposely frontloads much of its computational cost on to building the index, so that queries have good and predictable on average performance. What you want is something like entrymatcher that allows you to combined tailored range and nearest neighbour queries in the same index, and we don’t have that yet. We might (or might not) one day look at something like R-Trees which are (purportedly) good for combing such things (albeit with different trade offs elsewhere)

So, looking at the figures below, I’m not much interested in the particular numbers, or the comparison with entrymatcher, but the trends are interesting. It looks like something in the fitting is O(N2), but the queries alone look like they’re sub-linear as N increases, which is what one would expect.

1 Like

Totally. I get a lot of that.

This is just me poking at the code, my use cases, and reporting back my findings in a… Rod-ish fashion.

Aside from raw speed comparisons (which will obviously improve from alpha code), most of my nudging in this thread (and general line of inquiry) has to do with…

because

This may change with some things as you’re suggesting, but if any variability of distance matching, biasing, forking, etc… requires copying and re-fit-ing a tree, I think there may be a big gap in terms of what is possible in a real-time context.

I’m imagining (hoping) that for vanilla distance matching, that a pre-fit kdtree would be fast as shit, and be a super go-to where you only want to find the nearest in a set, with fixed data all around (like for the time-travel/prediction stuff). I could be unique in that sense, but for me this covers only a small part of what I’d want to do with a corpus. So a lot of this is just gesturing towards those possibilities, which circles back to…

Thanks. To be clear: it’s really not surprising in the least that you can’t yet the same (or even remotely so) performance from our kdtree as you can with entry matcher. We’re still in alpha, and still concentrating on questions of interface and architecture, so have paid no attention at all to making things fast, and we won’t be for a while.

Just a few of comments/questions about this:

  • I expect (as I already communicated to Rod) that the implementation may outperform entrymatcher on larger numbers of dimensions, given how close the results are in terms of orders of magnitude for search only - it would be useful to know if that is where it starts winning.

  • I don’t agree that it isn’t at all surprising - I am suprised. The code in entrymatcher as Rod is calling it should be a brute force distance calculation that has to run every time on all items of data in the whole database/set and might have a sort to do at the end (I forget if that is the case). The only optimisations here are to do with the linear ordering of the data in memory, and for this case I am surprised to see that the KD Tree doesn’t overtake with smaller data sets to get the nearest due to the algorithmic complexity being preferable.

  • In terms of this being “alpha” - assuming that you are compiling with compiler optimisations on with a vanilla implementation then in my experience throwing optimisation time at it isn’t likely to improve the speed by factors of 4 etc. unless there is something fundamental issue (algorithmic/memory copying or layout etc.) that is slowing it down. Given that the purpose of the tools is (at least in significant part) real-time usage I think some indication of whether there is at least a potential optimisation that can be identified here would be useful here, both to predict what improvements might come later, and how this might fit a real-time workflow - I suppose whilst you can say there isn’t enough evidence that KDTrees in these kind of cases will be slower, there’s also no evidence in this thread that they aren’t - algorithmic complexity is all well and good but in there realworld the constants matter. At the moment I’m a little unsure of the claim, as sometimes (in other contexts) I find that people assume that code can just be made faster by allowing time to that problem (or moving to different lanagues etc,) often in cases where that simply isn’t the case.

I’m not particularly interested here either in the fact that the code under comparison is mine, nor in asking for a specific outcome, but I think understanding the pros/cons of the tools against available ones is pretty relevant, and my strong expectation is that KDTrees should have the benefit of being fast, at least for the nearest neighbour part. Any light shed on why this isn’t seemingly the case here would be useful.

1 Like

Indeed, I get the desirability of marrying nearest neighbour and range queries like you want to. TBH, I can’t remember the chain of decisions that ended us up with different objects; AFAIK, datasetquery is only brute force at the moment, but there’s certainly plenty of low hanging scope for that to get better once we settle. Another thing that I’m keen on (and which Gerard reminded me today of my keenness for) is trees that support insertion because, like you, I want to do everything on the stage, during the stage show, live, as it’s performed :

1 Like

There may well have been an implicit ‘to me’ in the expression of surprise :wink: The reasons it currently huffs a bit at scale are partly to do with implementation rather than the algorithm itself. Because there’s a lot of recursion, small inefficiencies can pile up quite quickly. The happy news is, of course, that it’s easy to tame those. However, I think the first priority has been on making expressive code that works.

In fact, the performance of KD-Trees begins to fall apart at higher dimensions (because this governs the depth of the tree and, I guess, there’ll be more backing up and so forth), and it starts to approach brute force. Where I would expect KDTree to scale better than brute force is in the number of data points. Were it not for the hitch Rod just reported in fitting a large set (which turned out to be a pass by value in a recursion problem), it should have been possible to see that.

This paper is reasonably up front about KD Tree’s strengths limits (although perhaps overly pessimistic) without being too comp-sci

I’m not entirely sure what you’re after from me in the last point. I’m pretty sure you know I agree that RT usage is important, and that you know I’m not the person who can speak with much authority on the how / when of performance tuning the objects in general. I do know that there are some specific inefficiencies that can be easily addressed (because I just fixed one), and I’ll send a PR to G suggesting some of these. But, as it stands, I don’t think it’s at all unusable in a RT context (as long as one isn’t re-indexing all the time). If you’re hitting a particular performance / ergonomic wall with it, then we should certainly look at that: given that you have a gig to try and do with this stuff, n’all.

Things like adding new tree algorithms are, AFAIK, definitely on a far, possibly conjectural, horizon. Certainly not before the gig.

1 Like

I did a quick test here, upping the dimensions to 20.

Here are the comparisons.

10k + 8d:
10000_8

10k + 20d:
Screenshot 2020-10-10 at 12.14.22 am

100k + 8d:
100000_8

100k + 20d:
Screenshot 2020-10-10 at 12.15.43 am

So it actually looks like fluid.kdtree~ does worse with higher dimensions, everything else being equal.

In terms of the last point I’m asking if there are places where potential inefficiencies might seem obvious, rather than a general “it’s the implementation/we could make it faster” - because I tend to be wary of such claims without specifics.

I thought it would be easiest for me to go and look at the code and see if there is anything obvious there or differences with entrymatcher. I’m unclear on the probable speed implications of std::priority_queue in this context (it might be overly complex when k is small, compared to an insertion sort approach), but the other thing is that I remembered that entrymatcher doesn’t calculate the correct distance fully until output, preferring square distance, as it saves the root until we know that we are interested in the exact value. It looks like changing two lines of code in KDTree.hpp would do that (possibly with some renaming to retain expressivity, as things called distances, should not contain square distances etc.). Eigen support .squaredNorm() so it’s easy to do. The other area is the effect of the tensor view on speed, but all of this is conjecture. All I can say is that changing to a squared distance until output is incredibly unlikely to lose you speed, but how much performance gain you’ll get is unclear without testing.

Happy to make other suggestions, but if you would be willing to humour us with a quick test on a branch that would be interesting - feel free to say no, of course.

1 Like

In other news Rod’s timings are not being done correctly, so the comparison is not correct - he’s starting both timers before either side calculates and stopping them after each one returns, which means it’s impossible for the one that goes second to measure faster.

I have made him aware of this, so expect updates from him…

You’re incorrigible :heart:* Because this isn’t my code, and because G finds it unaccountably unsettling if I just go about changing shit, I’ll pass suggestions his way in the form of a PR. I’ve been tweaking based on what I see in instruments since Rod’s post earlier, and things are already much improved. Good tip about the norm(), I’ll give it a shot, but will limit myself only to low hanging fruit with simple changes that G can make quick verdicts on, because he’s busy, and suffers enough of my nonsense as it is.

*If you’re in the mood for looking at code, I know of two very lovingly crafted PRs currently feeling folorn and abandoned on a Github repo. And a forgotten branch… :wink:

1 Like

I guess because threading is a mystery of the universe, the results of fixing the timing method are actually the same.

10k/8d:

(the overall speed is faster cuz I’m on my laptop now, but the ratio is the same)

and updated (but messy) code nugget:


----------begin_max5_patcher----------
5200.3oc6cs0iaijc9Y6eEDBYdqs15dQlmxNYSlEHyrYQlIXPvffFTRkZywT
jJjTssmEq+sm5BEEoDuTjpnZ2yptgkYSJxpNe04dcph+s29lEqR+jHeg2+r2
u38l272d6adi9TpS7lx+9MK1E9o0wg45u1hDwGSW8qKdvboBwmJzm9cdvimK
8PQrnn3y6Elm6hnD424+s7p6CKV+9njmdLSrtv7EfH.coOf3Gv7wPJDQnvG7
fXxRDz+zuD9CdnfkT4mnkfpGXxgcQIxFT28PkmLZitWI6ouCgVb5aZ5a5uJT
cx+9aeq5iGtNhuvKxaUuj+CdKVEl7zDfg.5RDkb5WdvCdLZ+n.rET.2NJfbG
JrWH9vW7JD4E6S0zbG3w13zvovPffnkvZ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oPSZzYXN1r9GX
RQRlaAUCQULoIMx1P0cAdf749FGavUhNXT.NvoZOKpdQl7Ux5E3ipr474Qv3
+mhxJ9r2+1SoKFtNRsrhyaOumvf.fw+RISIobRyY..F1rXoaO0a.ESfNFWb4
XuTOR4DXzsWJX1z.VyRmqiWZ1u8u+1+e.Qcg0TA
-----------end_max5_patcher-----------

What can I say- I like looking at code. I was suggesting trying things on a branch - not changing it without checking. Also it looks like the distance calculation is allocating memory because Eigen and actually that is where I’d look first. I see how Eigen makes this easy to write expressively, and that in theory it might even optimise the calculation with SIMD etc. but the flip side is it likes to allocate all over the place, leaving no obvious trace in the code that that is probably happening.

I’ve confirmed that even with the changes entrymatcher is returning a lot faster than the kdtree, but I’d think some targeted changes might reduce that difference, if not eliminate it altogether.

I guess the key interesting thing for me is where the algorithms with better complexity actually get faster, assuming the implementation isn’t getting in the way - for a lot of us a couple of (or few) thousand items is already a lot of stuff to be sifting, so optimisations that make sense for millions of points aren’t necessarily that interesting.

1 Like