memory

ruby
require "csv"

class PeopleCsvStream
  include Enumerable

  HEADERS = %w[id full_name email signed_up_at plan].freeze

Resilient CSV Export as a Streamed Response

rails performance streaming
by codesnips 3 tabs
go
package pools

import (
  "bytes"
  "sync"
)

sync.Pool for bytes.Buffer to reduce allocations in hot paths

go performance memory
by Leah Thompson 1 tab
ruby
class Tag < ApplicationRecord
  has_many :taggings, dependent: :destroy

  scope :top, ->(limit = 20) {
    joins(:taggings)
      .group(Arel.sql("tags.id"))

Memory-Safe “top tags” aggregation with pluck + group

rails activerecord performance
by codesnips 3 tabs
rust
use std::mem::MaybeUninit;

fn main() {
    let mut uninit: MaybeUninit<i32> = MaybeUninit::uninit();
    unsafe {
        uninit.as_mut_ptr().write(42);

MaybeUninit for safe uninitialized memory

rust unsafe memory
by Marcus Chen 1 tab
ruby
class Order < ApplicationRecord
  belongs_to :customer

  scope :for_export, -> {
    select(:id, :reference, :total_cents, :currency, :created_at, :customer_id)
      .where.not(exported_at: nil)

Avoid Memory Blowups: find_each + select Columns

rails activerecord performance
by codesnips 3 tabs
go
package main

import (
  "os"
  "runtime/debug"
  "strconv"

Set a soft memory limit with debug.SetMemoryLimit

go performance memory
by Leah Thompson 1 tab
rust
use std::collections::HashMap;
use std::hash::Hash;

const NIL: usize = usize::MAX;

struct Node<K, V> {

Build an O(1) LRU Cache in Rust With a HashMap and Intrusive Doubly Linked List

rust lru cache
by codesnips 3 tabs
javascript
const { Pool } = require('pg');
const Cursor = require('pg-cursor');

const pool = new Pool();

async function* streamUsers({ since, batchSize = 500 }) {

Stream a Large JSON Array Response in Node.js Without Buffering

nodejs streams transform-stream
by codesnips 3 tabs
go
package httpstream

import (
	"bufio"
	"database/sql"
	"fmt"

Streaming Large SQL Query Results as a JSON Array in Go

go database sql
by codesnips 2 tabs
php
<?php

namespace App\Exports;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Carbon;

Streaming a Filtered Orders CSV Export in Laravel with StreamedResponse

php laravel csv
by codesnips 3 tabs
python
from sqlalchemy import select

from .models import Order, db


def stream_orders(batch_size=1000):

Stream a Large CSV Export in Flask With a Generator Response

flask streaming csv
by codesnips 3 tabs
python
import os
import uuid

from flask import Blueprint, current_app, jsonify, request
from werkzeug.exceptions import RequestEntityTooLarge

Streaming Large Multipart File Uploads to Disk in Flask Without Buffering

flask werkzeug streaming
by codesnips 3 tabs