Skip to content

cache TailwindMerge instance - #94

Closed
thedumbtechguy wants to merge 4 commits into
ruby-ui:v1from
thedumbtechguy:main
Closed

cache TailwindMerge instance#94
thedumbtechguy wants to merge 4 commits into
ruby-ui:v1from
thedumbtechguy:main

Conversation

@thedumbtechguy

Copy link
Copy Markdown
Contributor

TailwindMerge operates a cache and undertakes a lot of operations that require that an instance be reused rather than constantly instantiating one.

I added a simple benchmark

################ Simple ################* Single run: user system total realCached: 0.000234 0.000006 0.000240 ( 0.000237)Uncached: 0.004120 0.000448 0.004568 ( 0.004769)== Cached is 20.12x faster than Uncached.* 1000 runs: user system total realCached: 0.001120 0.000013 0.001133 ( 0.001332)Uncached: 2.199289 0.036016 2.235305 ( 2.511064)== Cached is 1885.18x faster than Uncached.* Iterations per second (5 second run):Cached: 877208Uncached: 452== Cached performs 1940.73x more iterations per second than Uncached.-------------------------------------------------------------------------------------
Benchmark Code
require"benchmark"require"tailwind_merge"classCachedMergerMERGER=TailwindMerge::Merger.new.freezedefcall(classes)MERGER.merge(classes)endendclassUncachedMergerdefcall(classes)TailwindMerge::Merger.new.merge(classes)endenddefrun_benchmark(name,classes)puts"\n################ #{name} ################\n"puts"\n* Single run:"single_results=Benchmark.bm(20)do |x|
x.report("Cached:"){CachedMerger.new.call(classes)}x.report("Uncached:"){UncachedMerger.new.call(classes)}endcompare_results(single_results,"Single run")puts"\n* 1000 runs:"bulk_results=Benchmark.bm(20)do |x|
x.report("Cached:"){1000.times{CachedMerger.new.call(classes)}}x.report("Uncached:"){1000.times{UncachedMerger.new.call(classes)}}endcompare_results(bulk_results,"1000 runs")# Simple IPS-like benchmarkduration=5# Run for 5 secondsputs"\n* Iterations per second (5 second run):"ips_results=[["Cached",->{CachedMerger.new.call(classes)}],["Uncached",->{UncachedMerger.new.call(classes)}]].mapdo |name,proc|
iterations=0start_time=Time.nowwhileTime.now - start_time < durationproc.calliterations += 1endips=iterations / durationputs"#{name}: #{ips.round(2)}"[name,ips]endcompare_ips(ips_results)puts"-------------------------------------------------------------------------------------"enddefcompare_results(results,label)our_time=results[0].realphlex_time=results[1].realfaster,slower=(our_time < phlex_time) ? ["Cached","Uncached"] : ["Uncached","Cached"]multiplier=[our_time,phlex_time].max / [our_time,phlex_time].minputs"== #{faster} is #{multiplier.round(2)}x faster than #{slower}."enddefcompare_ips(results)our_ips,phlex_ips=results.map{ |_,ips| ips.to_f}faster,slower=(our_ips > phlex_ips) ? ["Cached","Uncached"] : ["Uncached","Cached"]multiplier=[our_ips,phlex_ips].max / [our_ips,phlex_ips].minputs"== #{faster} performs #{multiplier.round(2)}x more iterations per second than #{slower}."end# Run benchmarksrun_benchmark("Simple","inline-block h-12 w-12 rounded-full ring-2 ring-white block font-bold px-3 py-2 text-slate-700 rounded-lg hover:bg-slate-100 hover:text-slate-900")

akodkodand others added 4 commits August 4, 2024 12:42
* FIX AttributeMerger
TailwindMerge operates a cache and undertakes a lot of operations that require that an instance be reused rather than constantly instantiating one
Signed-off-by: TheDumbTechGuy <sfroelich01@gmail.com>
@cirdes
cirdes changed the base branch from main to v1August 26, 2024 11:59
@cirdes

Copy link
Copy Markdown
Collaborator

@thedumbtechguy, wow! That is incredible.

Right now, we've decided to freeze the main branch and work on v1 until we complete the migration. We explained it here.

Would you mind sending the PR to the v1 branch or resolving the conflicts?

@thedumbtechguy

Copy link
Copy Markdown
ContributorAuthor

Sure thing. I'll resolve the issues and resubmit.

@thedumbtechguythedumbtechguy closed this by deleting the head repository Aug 29, 2024
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@thedumbtechguy@cirdes@akodkod@pierry01