diffz icon indicating copy to clipboard operation
diffz copied to clipboard

Memory management (fixes #22, closes #24)

Open mnemnion opened this issue 2 years ago • 33 comments

This PR adds proper memory management to diffz.

Almost all the tests are ported to use std.testing.allocator, and they all pass. There's a few in the diff test which I didn't feel like rewriting, I would encourage one of the maintainers to do so after the merge.

mnemnion avatar Jul 03 '24 13:07 mnemnion

https://github.com/mnemnion/diffz/pull/1

techatrix avatar Jul 07 '24 18:07 techatrix

So there's a design conflict here, because a) definitely found that I wasn't errdeferring halfmatches, but the next one I hit at line 1787 is because of the diffs, which are passed in.

The problem is that inside of diffCharsToLines, we should not errdefer the diffs, which are already errdefered in diffLineMode, which is a public function. So if I double errdefer, it segfaults from a double-free.

So I think what has to happen is that exhaustive allocation failure checks have to come through the public functions only. Those are the only ones which can be called in actual use, so if they free memory correctly, it's fine.

I misread what you said and though there was one SkipZigTest line, so I absentmindedly merged and pushed the staging branch. But I'll go ahead and do a hard reset on this branch and a force push. I'll keep an eye out for the next PR.

mnemnion avatar Jul 07 '24 19:07 mnemnion

So the internal tests are fine, actually, as long as the data is properly prepared.

So I've fixed all the leaks in the tests, and wrapped tests which segfault because they try to free .rodata strings in if (false) blocks tagged TODO #23. I also included a helper function sliceToDiffList which will make converting those tests relatively painless.

mnemnion avatar Jul 07 '24 21:07 mnemnion

That should be all of it.

I realized in writing some tests for rebuildtexts that the new testing functions were, in fact, making copies of the string data. Apparently a double-free during error conditions does not get caught be the allocator, but just segfaults, which I had come to associate with trying to free .rodata, so false positive there.

mnemnion avatar Jul 08 '24 00:07 mnemnion

I added test coverage for diffMatchEfficiency, cleaning up the bugs in the process, so that's decent (not line-for-line complete) coverage of the library in its present state.

mnemnion avatar Jul 08 '24 17:07 mnemnion

Coverage is good, at 97.5%.

It looks like missing coverage is related to diffLineMode only, which isn't exercising the inside of the halfMatch code. Maybe worth circling back to.

If you have kcov installed you can now run zig build cov to get a coverage report. The output directory is in the .gitignore.

mnemnion avatar Jul 08 '24 17:07 mnemnion

I believe this is complete, although I'm willing to tweak it a bit here and there.

So I wanted to bring this comment to the front:

Also would like to hear back from someone who's run zls with these changes and a debug or release safe build for a few days or so.

It seems like a reasonable approach here is to ping the ZLS maintainers, and ask them to run with the branch themselves. I've never built it, and am not in a position to diagnose problems which might arise, unless they're definitely in the DiffMatchPatch code (but triaging that is beyond me).

Clearly an important use case for this library, so doing some stress tests / integration is important there.

mnemnion avatar Jul 08 '24 17:07 mnemnion

It seems like a reasonable approach here is to ping the ZLS maintainers, and ask them to run with the branch themselves. I've never built it, and am not in a position to diagnose problems which might arise, unless they're definitely in the DiffMatchPatch code (but triaging that is beyond me).

I am now test driving a release safe build of zls with these changes. Building was as simple checking out this pr and changing the diffz dep:

diff --git a/build.zig.zon b/build.zig.zon
index 6cfb176..d697252 100644
--- a/build.zig.zon
+++ b/build.zig.zon
@@ -15,9 +15,12 @@
             .url = "https://github.com/ziglibs/known-folders/archive/47076c6b11214a218e9244471d8762310820911a.tar.gz",
             .hash = "12209d2738a2e1dbd3781c2e5f01a2ea877dcfeea53efdfa1913247297d328e6b207",
         },
         .diffz = .{
-            .url = "https://github.com/ziglibs/diffz/archive/ef45c00d655e5e40faf35afbbde81a1fa5ed7ffb.tar.gz",
-            .hash = "1220102cb2c669d82184fb1dc5380193d37d68b54e8d75b76b2d155b9af7d7e2e76d",
+            .path = "../diffz",
         },
     },
     .paths = .{""},

Then build.

~/.../zig/zls $ zig build -Doptimize=ReleaseSafe

If you've never built zls, not sure but I think the only other change would be to config your editor's zls path.

archaistvolts avatar Jul 09 '24 01:07 archaistvolts

Note that I found a leaking test case, so this is on hold while I track that down.

It only leaks memory in exhaustive allocation failure testing (that is, it passes a 'normal' test), so you're unlikely to see it in ZLS.

mnemnion avatar Jul 09 '24 16:07 mnemnion

Alright, that's sorted.

Worth noting, that this:

It looks like missing coverage is related to diffLineMode only, which isn't exercising the inside of the halfMatch code. Maybe worth circling back to.

Is where the leaks were: in the code without coverage. I described it incorrectly, though, it was in the diffCompute path which uses a half match.

Coverage now stands at 99.2%, and suggests that diffLineMode doesn't have comprehensive leak checks yet (because the errdefers in that section of the code are not triggered by the test. Total line coverage is essential for this kind of code.

mnemnion avatar Jul 09 '24 16:07 mnemnion

I've just pulled the latest changes from this branch and am test driving. Didn't notice any issues since yesterday.

archaistvolts avatar Jul 09 '24 23:07 archaistvolts

Added checkAllFailingAllocations test for diffLineMode which, yes, smoked out some bugs.

Running this test takes an extraordinarily long time, because the nature of the algorithm is a bunch of tiny allocations, and no obvious way to batch them, and the exhaustive test function is quadratic by the number of allocations.

It triggers line mode through the "front door" so to speak, and can no doubt be replaced with something shorter which calls diffLineMode directly.

Remaining un-covered lines are little things like the Diff format string, and some errdefers which I don't think are triggerable. But I'd rather have a few red lines in kcov than be wrong about that.

mnemnion avatar Jul 14 '24 00:07 mnemnion

Added a field which configures the line-check threshold, using that and truncating the test data gets the allocation failure test down to something comparable to the other tests.

That's probably going to be my last tweak to this branch.

mnemnion avatar Jul 14 '24 18:07 mnemnion