Last active
December 11, 2015 19:29
-
-
Save henrik/4649195 to your computer and use it in GitHub Desktop.
Got a script using Savon 2 with the e-conomic SOAP API (a 3 MB WSDL) down from around 30 seconds to around 2 seconds by marshalling the Wasabi WSDL parser. Consider this a proof-of-concept.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # For Wasabi 3.0.0. | |
| # Marshals WSDL for a huge speedup with large files. | |
| # A 3 MB WSDL went from around 30 to 2 seconds. | |
| # Marshalling: 3.1 MB -> 629 KB. | |
| # Zipping: 629 KB -> 59 KB. | |
| module Wasabi | |
| class Parser | |
| alias_method :old_parse, :parse | |
| def parse | |
| old_parse | |
| # The Nokogiri document prevents marshalling. | |
| # We've parsed so we don't need the document anymore. | |
| self.document = nil | |
| end | |
| end | |
| class Document | |
| alias_method :old_parser, :parser | |
| def parser | |
| return @parser if @parser | |
| @parser = load_parser_from_marshal | |
| unless @parser | |
| old_parser | |
| dump_parser_to_marshal | |
| end | |
| @parser | |
| end | |
| private | |
| def load_parser_from_marshal | |
| if File.exist?(marshal_file) | |
| Marshal.load(unzip(File.read(marshal_file))) | |
| end | |
| end | |
| def dump_parser_to_marshal | |
| data = zip(Marshal.dump(@parser)) | |
| File.open(marshal_file, "w") { |f| f.write(data) } | |
| end | |
| def zip(string) | |
| Zlib::Deflate.deflate(string) | |
| end | |
| def unzip(string) | |
| Zlib::Inflate.inflate(string) | |
| end | |
| def marshal_file | |
| basename = File.basename(@document) | |
| "./#{basename}.marshalled.zip" | |
| end | |
| end | |
| end |
Author
Author
And around 60 KB zipped. If that doesn't slow things down measurably, it could keep the file size down even more.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The marshalled WSDL is 629 KB where the original WSDL is 3.1 MB, by the way, so the gem would be a lot smaller, too.
The gem would probably need to tie down a specific version of Wasabi (Savon's parser) to ensure it can unmarshall the file. But I think that'd be acceptable to get this speedup.