a-sit-plus / signum

Kotlin Multiplatform Crypto/PKI Library and ASN1 Parser + Encoder
https://a-sit-plus.github.io/signum/
Apache License 2.0
76 stars 6 forks source link

The number of exceptions thrown should be reduced. This may affect performance. #146

Open ShiinaSekiu opened 1 month ago

ShiinaSekiu commented 1 month ago

https://github.com/a-sit-plus/signum/blob/e8f9bd4a0be5a9018e6e4ffe137b22191b06c1e1/indispensable/src/commonMain/kotlin/at/asitplus/signum/indispensable/asn1/Asn1Elements.kt#L432-L443

        /**
         * Returns the next child held by this structure. Useful for iterating over its children when parsing complex structures.
         * @throws [Asn1StructuralException] if no more children are available
         */
        @Throws(Asn1StructuralException::class)
        fun nextChild() = children.getOrElse(index++) { throw Asn1StructuralException("No more content left") }

        /**
         * Exception-free version of [nextChild]
         */
        fun nextChildOrNull() = children.getOrNull(index++)
JesusMcCloud commented 1 month ago

Thank you very much! There are probably a handful of locations, where it is possible to get around throwing and catching. The more pressing issue wrt. performance is not rooted in this project, though, but in KmmResult, which cannot be a value class, as it would make it impossible to export it to an XC framework. Thus, catching incurs a performance hit. At the same time runCatching catches more than it should, so we require catching.

ShiinaSekiu commented 1 month ago

By the way, why doesn't the peek() method of this class have a corresponding peekOrNull() method?

JesusMcCloud commented 1 month ago

I'll not that down for improving API docs. peek already returns null, if no more children are left. All the feedback you have provided so far is really appreciated!

JesusMcCloud commented 1 month ago

The improved docs are part of #151

JesusMcCloud commented 3 weeks ago

KmmResult 1.9.0 will be part of the next release, significantly reducing overhead, even though the same number of exceptions are thrown.

iaik-jheher commented 1 week ago

KmmResult 1.9.0 will be part of the next release, significantly reducing overhead, even though the same number of exceptions are thrown.

This would require us to go through and review catching uses/convert them to catchingUnwrapped, to my understanding?