-
-
Save henrik/4649195 to your computer and use it in GitHub Desktop.
| # 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 |
@koppen Yeah, I think it could be added sensibly. I've been working on a tiny wrapper (around 50 lines) around Savon, usage e.g.:
number = client.call :Entry_GetLastUsedSerialNumber
data = client.call :Entry_GetData, entityHandle: { SerialNumber: number }
What I would like is for that wrapper to bundle a marshalled WSDL, and then R-conomic could use that wrapper and not Savon directly.
One thing I've wanted to improve with R-conomic is that it currently mixes low-level building of SOAP requests with its own entity abstractions. So if it were to use a wrapper like that, I think the code would be easier to maintain.
And for simple things, or to explore the API, you could just use the wrapper directly.
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.
And around 60 KB zipped. If that doesn't slow things down measurably, it could keep the file size down even more.
Clever! I'd love to see something like this in R-conomic. The performance is pretty much what's holding back the upgrade to Savon 1+
Do you think it could be added in a sensible manner?