TGViewer
Go Casts | Systems & Agents 🚀 Go Casts | Systems & Agents 🚀 @gocasts · 9.93K subscribers
Post #564 6.13K
یه مقاله جذاب در مورد نوشتن کد سریع از Shopify
درسته که درباره Ruby on Rails نوشته شده اما اکثر نکاتش به خیلی از زبان و فریمورک های دیگه از جمله گولنگ هم قابل تعمیم دادن هست.

دقت کنید که Shopify در یک مقیاس خیلی بزرگ کار میکنه و نکاتش مهمه.

کدی که با Ruby on Rails نوشته میشه معروفه به کند بودن

اما در Shopify در مقیاس میلیون درخواست در دقیقه داره از Ruby on Rails استفاده میشه

تو این مقاله نکات جالبی رو اشاره میکنه برای اینکه کد سریعتری نوشته بشه

اولین نکته ش در مورد ORM یا همون ActiveRecord هست که باید درک کنید توابع مختلف orm چه رفتاری با دیتابیس دارند.

تو گولنگ همین مشکل با gorm وجود داره که به شدت میتونه لود زیادی روی دیتابیس بندازه بدون اینکه برنامه نویس متوجه این قضیه بشه.

دومین نکته ش باز در مورد دیتابیس هست که میگه از select * پرهیز کنید و فقط ستون هایی که لازم دارید رو انتخاب کنید.


نکته دیگه پرهیز از کوئری زدن به ستون هایی هست که index ندارن و در مقیاس بالا باعث میشه که full scan رخ بده
البته ایندکس اضافه کردن باعث lock شدن دیتابیس ممکنه بشه برای writeها و باید مواظب مایگریشن ایندکس باشیم

نکته بعدی کش کردن همه چیز هست…
Cache All The Things
If you can’t make something faster, a good alternative is to cache it. Things like complex view compilation and external API calls benefit greatly from caching. Especially if the resultant data doesn’t change often.

یه سری چیزارو میشه throttle کرد
Throttle Bottlenecks
But what about operations you can’t cache? Things like delivering an email, sending a webhook, or even logging in can be abused by users of an application. Essentially, any expensive operation that can’t be cached should be throttled.

برای یه سری پروسه ها هم میشه job queue و مکانیزم های async در نظر گرفت
Do It Later (In a Job)
Jobs allow us to defer work to another process through queueing systems often backed by Redis. Exporting a dataset, activating a subscription, or processing a payment are all great examples of job-worthy work

پرهیز از متاپروگرمینگ
Use Metaprogramming Sparingly
Changing a program’s structure at runtime is a powerful feature. In a highly dynamic language like Ruby, there are significant performance costs associated to metaprogramming.

دونستن تفاوت O(1) و O(n) برای مقیاس پذیر بودن خیلی مهمه
Know the difference between O(n) and O(1)
What O(n) and O(1) mean is that there are two kinds of operations. O(n) is an operation that scales in time with size, and O(1) is one that is constant in time regardless of size.

مثلا جای آرایه بهتره از hash استفاده بشه چون lookup کردن تو hash فارغ از اندازه O(1) هست


Allocate Less
استفاده و مدیریت Garbage Collector
If used improperly, dangerous methods can lead to unwanted side effects in your code. A best practice to follow is to avoid mutating global state while leveraging mutation on local state.

https://shopify.engineering/write-fast-code-ruby-rails


@gocasts
Shopify How to Write Fast Code in Ruby on Rails - Shopify Part of Shopify’s success with Ruby on Rails is an emphasis on writing fast code. But, how do you really write fast code? Let’s talk about a few ways to start writing faster code in Active Record, Rails, and Ruby.
  • 👍 24
  • ❤ 7
  • 🔥 2
  • 👏 2
More from @gocasts
  1. Oct 4, 2026از دیروز چند آیتم ظاهری در GoCasts به‌روز شده 🚀 اگه دقت کرده باشید، لوگو، اسم کانال و بیو…
  2. Oct 1, 2026اینم از عمو باب که تسلیم شد! کاری ندارم که با ایشون هم نظر بودید و هستید یا نه. به دور از…
  3. Oct 1, 2026ترکیب Sonnet 5.5 و Opus 5.5 این روزا خیلی خوب جواب میده. الگویی که برای من جواب داده اینه:…
  4. Sep 24, 2026این بهترین تجربه‌ای بود که تا حالا با یه agent داشتم. دیشب با تاخیر چند ساعته ایمیل هاستین…
  5. Sep 23, 2026بچه ها اگه تجربه software factory جدی داشتید توی شرکت و سازمان و محصول تون، بهم فیدبک بدید…
  6. Sep 23, 2026در مورد پست دیشب که در لینکدین منتشر شد لازمه این نکته رو اضافه کنم که https://lnkd.in/p/e…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →