[ext/openssl] ASN1 performance enhancement
recently I noticed that the method
static int ossl_asn1_default_tag(VALUE obj)
(in ossl_asn1.c) iterates through an internal array each time a default tag is to be looked up resulting in O(n) runtime
performance. I thought this to be the ideal situation for using a hash and so I added one with OpenSSL::ASN1Data subclasses
acting as keys and the corresponding tags as values. I also did some profiling to see whether the constant lookup time made
some impact. I first ran a test encoding and parsing a certificate a couple of times and that's what I got:
Not much, but I think the improvement is noticeable. Next I ran a "worst case scenario" for the old code.
I encoded and decoded a Sequence with 4 BMPString values, since BMPString with tag 30 is at the end of
the internal array. Here the performance gain was roughly 15%:
Old code worst case:
New code worst case:
Please find attached the patch (including tests) that would add this improvement to Ruby trunk.
PS: I also changed the UNIVERSAL_TAG_NAME constant's name for OpenSSL::ASN1::EndOfContent from "EOC" to
"END_OF_CONTENT" because all other names are the uppercase versions of their corresponding classes, this
would have been the only exception to that rule.
Then I also exported two methods, default_tag and default_tag_of_class, for the ASN1 module. I needed
them for something I'm currently working on. These latter changes are independent of the performance
improvement described above.