@@ -270,190 +270,3 @@ impl Default for Attributes {
270270 Self :: Static ( & [ ] )
271271 }
272272}
273-
274- #[ cfg( all( test, feature = "wasm_bench" ) ) ]
275- mod benchmarks {
276- use super :: * ;
277- use wasm_bindgen_test:: { wasm_bindgen_test, wasm_bindgen_test_configure} ;
278-
279- wasm_bindgen_test_configure ! ( run_in_browser) ;
280-
281- macro_rules! run {
282- ( $name: ident => {
283- $( $old: expr => $new: expr ) +
284- } ) => {
285- // NB: these benchmarks only compare diffing. They do not take into account aspects like
286- // allocation impact, which is lower for both `Static` and `Dynamic`.
287-
288- let results = vec![
289- $(
290- {
291- let mut old = $old. clone( ) ;
292- let new = $new. clone( ) ;
293- let el = gloo_utils:: document( ) . create_element( "div" ) . unwrap( ) ;
294- old. apply( & el) ;
295- (
296- format!( "{} -> {}" , attr_variant( & old) , attr_variant( & new) ) ,
297- easybench_wasm:: bench_env_limit(
298- 2.0 ,
299- ( NodeCloner ( el) , new, old) ,
300- |( el, mut new, old) | new. apply_diff( & el. 0 , old) ,
301- ) ,
302- )
303- } ,
304- ) +
305- ] ;
306-
307- let max_name_len = results. iter( ) . map( |( name, _) | name. len( ) ) . max( ) . unwrap_or_default( ) ;
308- wasm_bindgen_test:: console_log!(
309- "{}:{}" ,
310- stringify!( $name) ,
311- results. into_iter( ) . fold( String :: new( ) , |mut acc, ( name, res) | {
312- use std:: fmt:: Write ;
313-
314- write!( & mut acc, "\n \t \t {:<width$}: " , name, width=max_name_len) . unwrap( ) ;
315-
316- if res. ns_per_iter. is_nan( ) {
317- acc += "benchmark too slow to produce meaningful results" ;
318- } else {
319- write!(
320- & mut acc,
321- "{:>7.4} ns (R²={:.3}, {:>7} iterations in {:>3} samples)" ,
322- res. ns_per_iter,
323- res. goodness_of_fit,
324- res. iterations,
325- res. samples,
326- )
327- . unwrap( ) ;
328- }
329-
330- acc
331- } )
332- ) ;
333- } ;
334- }
335-
336- #[ wasm_bindgen_test]
337- fn bench_diff_empty ( ) {
338- let static_ = Attributes :: Static ( & [ ] ) ;
339- let dynamic = Attributes :: Dynamic {
340- keys : & [ ] ,
341- values : Box :: new ( [ ] ) ,
342- } ;
343- let map = Attributes :: IndexMap ( Default :: default ( ) ) ;
344-
345- run ! {
346- empty => {
347- static_ => static_
348- dynamic => dynamic
349- map => map
350- static_ => dynamic
351- static_ => map
352- dynamic => map
353- }
354- }
355- }
356-
357- #[ wasm_bindgen_test]
358- fn bench_diff_equal ( ) {
359- let static_ = Attributes :: Static ( sample_attrs ( ) ) ;
360- let dynamic = make_dynamic ( sample_values ( ) ) ;
361- let map = make_indexed_map ( sample_values ( ) ) ;
362-
363- run ! {
364- equal => {
365- static_ => static_
366- dynamic => dynamic
367- map => map
368- static_ => dynamic
369- static_ => map
370- dynamic => map
371- }
372- }
373- }
374-
375- #[ wasm_bindgen_test]
376- fn bench_diff_change_first ( ) {
377- let old = sample_values ( ) ;
378- let mut new = old. clone ( ) ;
379- new[ 0 ] = AttrValue :: Static ( "changed" ) ;
380-
381- let dynamic = ( make_dynamic ( old. clone ( ) ) , make_dynamic ( new. clone ( ) ) ) ;
382- let map = ( make_indexed_map ( old) , make_indexed_map ( new) ) ;
383-
384- run ! {
385- changed_first => {
386- dynamic. 0 => dynamic. 1
387- map. 0 => map. 1
388- dynamic. 0 => map. 1
389- }
390- }
391- }
392-
393- fn make_dynamic ( values : Vec < AttrValue > ) -> Attributes {
394- Attributes :: Dynamic {
395- keys : sample_keys ( ) ,
396- values : values. into_iter ( ) . map ( Some ) . collect ( ) ,
397- }
398- }
399-
400- fn make_indexed_map ( values : Vec < AttrValue > ) -> Attributes {
401- Attributes :: IndexMap (
402- sample_keys ( )
403- . iter ( )
404- . copied ( )
405- . zip ( values. into_iter ( ) )
406- . collect ( ) ,
407- )
408- }
409-
410- fn sample_keys ( ) -> & ' static [ & ' static str ] {
411- & [
412- "oh" , "boy" , "pipes" , "are" , "from" , "to" , "and" , "the" , "side" ,
413- ]
414- }
415-
416- fn sample_values ( ) -> Vec < AttrValue > {
417- [
418- "danny" , "the" , "the" , "calling" , "glen" , "glen" , "down" , "mountain" , "" ,
419- ]
420- . iter ( )
421- . map ( |v| AttrValue :: Static ( * v) )
422- . collect ( )
423- }
424-
425- fn sample_attrs ( ) -> & ' static [ [ & ' static str ; 2 ] ] {
426- & [
427- [ "oh" , "danny" ] ,
428- [ "boy" , "the" ] ,
429- [ "pipes" , "the" ] ,
430- [ "are" , "calling" ] ,
431- [ "from" , "glen" ] ,
432- [ "to" , "glen" ] ,
433- [ "and" , "down" ] ,
434- [ "the" , "mountain" ] ,
435- [ "side" , "" ] ,
436- ]
437- }
438-
439- fn attr_variant ( attrs : & Attributes ) -> & ' static str {
440- use Attributes :: * ;
441-
442- match attrs {
443- Static ( _) => "static" ,
444- Dynamic { .. } => "dynamic" ,
445- IndexMap ( _) => "indexed_map" ,
446- }
447- }
448-
449- /// Clones the node on [Clone] call
450- struct NodeCloner ( Element ) ;
451-
452- impl Clone for NodeCloner {
453- fn clone ( & self ) -> Self {
454- use wasm_bindgen:: JsCast ;
455-
456- Self ( self . 0 . clone_node ( ) . unwrap ( ) . dyn_into ( ) . unwrap ( ) )
457- }
458- }
459- }
0 commit comments