Skip to content

PERF: Use backend to load Note #14948

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

Merged
merged 9 commits into from
Dec 21, 2023
Merged

Conversation

david-allison
Copy link
Member

@david-allison david-allison commented Dec 11, 2023

Purpose / Description

Match Anki Desktop's implementation, and get a 3x speedup (1m30 -> 30s to load 1MM notes)

❗️WARN: while unit testing, I had a failure of:

I have annotated the class which I believe is the problem, but this is not guaranteed to fix the issue. I have performed many retests and have been unable to reproduce the test failure

Approach

  • try to use the backend, find there are type mismatches
  • fix the mismatches
  • use the backend
  • performance test

How Has This Been Tested?

1m 27 -> 28s to load 1 million notes (~87 -> 27 micros)

    @Test
    fun notePerformance() {
        val note = addNoteUsingBasicModel("hello", "world")

        // 28s 271ms    - backend + no .toMutableList()
        // 28s 461ms    - backend + .toMutableList()
        // 1m 27s       - Cursor
        for (i in 0..1_000_000) {
            col.getNote(note.id)
        }
    }

Learning (optional, can help others)

https://github.com/ankitects/anki/blob/db93939ded947d74bb25bd8552a2b2356a096509/pylib/anki/notes.py#L54-L67

Backend Codegen is problematic: it exposes a java.util.List, but this is actually a com.google.protobuf.Internal.ProtobufList

Extends List to add the capability to make the list immutable and inspect if it is modifiable.

https://protobuf.dev/reference/java/api-docs/com/google/protobuf/Internal.ProtobufList

Checklist

  • You have a descriptive commit message with a short title (first line, max 50 chars).
  • You have commented your code, particularly in hard-to-understand areas
  • You have performed a self-review of your own code
  • UI changes: include screenshots of all affected screens (in particular showing any new or changed strings)
  • UI Changes: You have tested your change using the Google Accessibility Scanner

Added in 19e92c1

Doesn't add much value, causes spam in CardBrowser
@david-allison david-allison added the Needs a new dev For PR that were good start but the original dev' left. Any dev can take over and finish it. label Dec 11, 2023
@david-allison david-allison added Needs Review and removed Needs a new dev For PR that were good start but the original dev' left. Any dev can take over and finish it. labels Dec 12, 2023
@david-allison david-allison marked this pull request as ready for review December 12, 2023 01:05
Removes a cast. Backend uses an **immutable** list
 but this is not an ArrayList, and we still want this
 value to be mutable due to other methods on the API surface
@david-allison david-allison force-pushed the note-log branch 2 times, most recently from a4ab27b to 733837a Compare December 12, 2023 02:00
Removes a cast. Backend uses an **immutable** list
 but this is not an ArrayList, and we still want this
 value to be mutable due to other methods on the API surface
This is what the backend uses:

anki.notes.Note.getMtimeSecs is an uint32
This may have caused issue 14796, as the inheritor was inheriting from RobolectricTest()

After fixing `Note` to use the backend, I got one
 non-reproducible error:

 `java.lang.UnsatisfiedLinkError: 'byte[][] net.ankiweb.rsdroid.NativeMethods.openBackend(byte[])'`

I strongly suspect that it was this class which caused
 the issue.

If this is incorrect, it's a small tiem penalty to unit tests
Copy link
Member

@BrayanDSO BrayanDSO left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty good

@BrayanDSO BrayanDSO added Needs Second Approval Has one approval, one more approval to merge and removed Needs Review labels Dec 12, 2023
@david-allison david-allison added the squash-merge Squash & force push by author/maintainers requested label Dec 21, 2023
Copy link
Member

@mikehardy mikehardy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@mikehardy
Copy link
Member

waiting for merge queue to empty so I don't thrash it with the squash merge from here then goes in

@mikehardy mikehardy added Pending Merge Things with approval that are waiting future merge (e.g. targets a future release, CI wait, etc) and removed Needs Second Approval Has one approval, one more approval to merge labels Dec 21, 2023
@mikehardy
Copy link
Member

This one gave me a fit locally. 2 macOS machines ran tests with this PR rebased to main no problem but windows consistent failures. Stopped gradle daemons, gradle clean with --rerun-tasks then it was clean. Poisoned cache or something. Hate that "I'm missing something..." feeling that gives me.

@mikehardy mikehardy merged commit c46022b into ankidroid:main Dec 21, 2023
@github-actions github-actions bot added this to the 2.17 release milestone Dec 21, 2023
@github-actions github-actions bot removed the Pending Merge Things with approval that are waiting future merge (e.g. targets a future release, CI wait, etc) label Dec 21, 2023
@github-actions github-actions bot removed the squash-merge Squash & force push by author/maintainers requested label Dec 21, 2023
@david-allison david-allison deleted the note-log branch December 21, 2023 19:36
@david-allison
Copy link
Member Author

@mikehardy
Copy link
Member

Nope, it's this, sometimes, I cannot get it to reproduce but I did just see it on a macOS machine so I've seen it cross-platform now. CI has zero instances, and locally I'm green again, I dunno

ReviewerTest > testLrnQueueAfterUndo FAILED
    java.lang.NoSuchMethodError: 'java.lang.String[] com.ichi2.libanki.Note.getFields()'
        at com.ichi2.anki.RobolectricTest.equalFirstField(RobolectricTest.kt:427)
        at com.ichi2.anki.ReviewerTest$testLrnQueueAfterUndo$1.invokeSuspend(ReviewerTest.kt:166)
        at com.ichi2.anki.ReviewerTest$testLrnQueueAfterUndo$1.invoke(ReviewerTest.kt)
        at com.ichi2.anki.ReviewerTest$testLrnQueueAfterUndo$1.invoke(ReviewerTest.kt)
        at com.ichi2.testutils.TestClass$runTest$1.invokeSuspend(TestClass.kt:154)
        at com.ichi2.testutils.TestClass$runTest$1.invoke(TestClass.kt)
        at com.ichi2.testutils.TestClass$runTest$1.invoke(TestClass.kt)
        at kotlinx.coroutines.test.TestBuildersKt__TestBuildersKt$runTest$2$1$1.invokeSuspend(TestBuilders.kt:314)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:108)
        at kotlinx.coroutines.test.TestDispatcher.processEvent$kotlinx_coroutines_test(TestDispatcher.kt:28)
        at kotlinx.coroutines.test.TestCoroutineScheduler.tryRunNextTaskUnless$kotlinx_coroutines_test(TestCoroutineScheduler.kt:103)
        at kotlinx.coroutines.test.TestBuildersKt__TestBuildersKt$runTest$2$1$workRunner$1.invokeSuspend(TestBuilders.kt:320)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:108)
        at kotlinx.coroutines.EventLoopImplBase.processNextEvent(EventLoop.common.kt:280)
        at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:85)
        at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:59)
        at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source)
        at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt:38)
        at kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source)
        at kotlinx.coroutines.test.TestBuildersJvmKt.createTestResult(TestBuildersJvm.kt:13)
        at kotlinx.coroutines.test.TestBuildersKt__TestBuildersKt.runTest-8Mi8wO0(TestBuilders.kt:308)
        at kotlinx.coroutines.test.TestBuildersKt.runTest-8Mi8wO0(Unknown Source)
        at kotlinx.coroutines.test.TestBuildersKt__TestBuildersKt.runTest-8Mi8wO0(TestBuilders.kt:166)
        at kotlinx.coroutines.test.TestBuildersKt.runTest-8Mi8wO0(Unknown Source)
        at com.ichi2.testutils.TestClass.runTest(TestClass.kt:152)
        at com.ichi2.testutils.TestClass.runTest$default(TestClass.kt:146)
        at com.ichi2.anki.ReviewerTest.testLrnQueueAfterUndo(ReviewerTest.kt:150)

bkzhn pushed a commit to bkzhn/Anki-Android that referenced this pull request Jan 14, 2024
* log: remove 'note.load'

Added in 19e92c1

Doesn't add much value, causes spam in CardBrowser

* refactor: Kotlin: note.tags -> MutableList

Removes a cast. Backend uses an **immutable** list
 but this is not an ArrayList, and we still want this
 value to be mutable due to other methods on the API surface

* refactor: Kotlin: note.fields -> MutableList

Removes a cast. Backend uses an **immutable** list
 but this is not an ArrayList, and we still want this
 value to be mutable due to other methods on the API surface

* refactor: Kotlin: note.mod -> Int

This is what the backend uses:

anki.notes.Note.getMtimeSecs is an uint32

* refactor: note: remove 'scm'

unused

* refactor: note: remove 'flags'

unused

* refactor: note: remove 'data'

unused

* tests: fix - use RunWith(AndroidJUnit4)

This may have caused issue 14796, as the inheritor was inheriting from RobolectricTest()

After fixing `Note` to use the backend, I got one
 non-reproducible error:

 `java.lang.UnsatisfiedLinkError: 'byte[][] net.ankiweb.rsdroid.NativeMethods.openBackend(byte[])'`

I strongly suspect that it was this class which caused
 the issue.

If this is incorrect, it's a small tiem penalty to unit tests

* note: load from Backend

This reduced the time to load 1 million notes from ~1m30 -> ~30s

https://github.com/ankitects/anki/blob/db93939ded947d74bb25bd8552a2b2356a096509/pylib/anki/notes.py#L54-L67
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants