a-sit-plus / signum

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

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

Open ShiinaSekiu opened 2 hours ago

ShiinaSekiu commented 2 hours 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 2 hours 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 2 hours ago

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

JesusMcCloud commented 1 hour 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!