Re: Repeated Hash Concatenation
Attila Szegedi <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <[email protected]> |
Someone could borrow Clojure's persistent collections implementation and plug it into FreeMarker. Attila. On 2010.09.02., at 11:02, Daniel Dekany wrote: > Thursday, September 2, 2010, 5:13:16 AM, Erin Drummond wrote: > >> Hello, >> >> The FreeMarker manual states (in: >> http://freemarker.sourceforge.net/docs/dgui_template_exp.html#dgui_template_exp_hashop_cat): >> "Note that hash concatenation is not to be used for many repeated >> concatenations, like for adding items to a hash inside a loop". I have >> a need to do this however. What is the best way to go about it? > > a) http://fmpp.sourceforge.net/pphash.html#sect43, "Writable hashes and > sequences" is one possible solution (see or even copy-paste the source > code). > > b) Wrap certain Java Map object in a way so that its methods will be > exposed, and then just use the Java API from FTL... This is tricky > though. You had to extend BeansWrapper by overriding getModelFactory > in a way so that for the Map-s that you want to access with Java API > (as oppose to with FTL hash operators like m[k], m?keys, etc) it > returns StringModel.FACTORY (or MapModel.FACTORY, but I personally > think that's a mess as it mixes the method names with the actual Map > keys). StringModel.FACTORY is what BeansWrapper uses for wrapping > objects that are just generic POJO-s for him, so don't be confused on > the name. getModelFactory could recognize those maps from a marker > interface for example. Then you had to use this new ObjectWrapper in > the whole application, i.e., do fmConfig.setObjectWrapper(new > YourSpecialBeansWrapper() where you *initialize* your set up > FreeMarker (that you usually do during application initialization). > >> It does actually work in the way that the manual forbids, however I >> assume that it is particularly messy or slow otherwise it wouldn't >> be forbidden. > > When you do H1 + H2 in FTL (where H1 and H2 are hashes), since it > treats hashes as immutable(!) data-structures, it will create a new > H12 object which just references H1 and H2, and whenever you search a > key in H12, it first passes the task to H2, and if that didn't have > the key then to H1. This means that after, say, 1000 concatenations of > a single-item hashes, whenever you look up a key, you basically do a > linear searching in a list of 1000 keys. Ouch! Not exactly the > performance what you would expect from a hash. (Yes, there are > algorithms for efficiently concatenate immutable dictionaries in way > that the resulting dictionary will remain fast... but other than you > need to be kind of a Knuth to implement them, you will be out of luck > if all that you know about a dictionary is what the > TemplateHashModel[Ex] interface exposes... and that's often the case > in FreeMarker.) > > -- > Best regards, > Daniel Dekany ------------------------------------------------------------------------------ This SF.net Dev2Dev email is sponsored by: Show off your parallel programming skills. Enter the Intel(R) Threading Challenge 2010. http://p.sf.net/sfu/intel-thread-sfd