Skip to content

cache TailwindMerge instance - #99

Merged
cirdes merged 2 commits into
ruby-ui:v1from
thedumbtechguy:v1
Aug 30, 2024
Merged

cache TailwindMerge instance#99
cirdes merged 2 commits into
ruby-ui:v1from
thedumbtechguy:v1

Conversation

@thedumbtechguy

Copy link
Copy Markdown
Contributor

This is a resubmission of #94


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")

Signed-off-by: TheDumbTechGuy <sfroelich01@gmail.com>

@cirdescirdes left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thedumbtechguy you just need to fix the standard.rb! 😄

@thedumbtechguy

Copy link
Copy Markdown
ContributorAuthor

Oh lol. Sure thing.

Signed-off-by: TheDumbTechGuy <sfroelich01@gmail.com>

@cirdescirdes left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!!!

@cirdes
cirdes merged commit e4207f9 into ruby-ui:v1Aug 30, 2024
@sethhorsley

Copy link
Copy Markdown
Member

Wow thanks! @thedumbtechguy good catch!

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.

3 participants

@thedumbtechguy@sethhorsley@cirdes