σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
uantCraft AcademyRoadmapsSkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
RoadmapsSkills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
uantCraft AcademyRoadmapsSkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
RoadmapsSkills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
Apache Arrow skill›Module 2 · Memory layout, sharing and files›Topic 2.1·~50 minAdvanced

Buffers and validity bitmaps

Skill positionModule 2 of 3 · Topic 1 of 4
2.1 Buffers and validity bitmaps
—Lab score— / 100Total XP—Streak—
Lecture0% read

3 min readCode examples: 3

  1. 01The idea in plain words
  2. 02Sizes and bitmaps
  3. 03Strings: offsets and one data buffer
  4. 04What the tasks ask

"Zero-copy" sounds like a slogan until you see what it relies on: an Arrow array is nothing but a few byte buffers whose layout is fixed in a specification. Anyone who knows the rules can find value number 5000 by arithmetic, in any language, with no library at all. This topic reads those buffers by hand, which is also the best way to learn why Arrow is fast.

01

The idea in plain words

Where we are. Topic 3 used the buffers without looking at them. This topic opens them: sizes, the validity bitmap for nulls, the offsets that locate strings. It explains the arithmetic of topic 1 and makes the next topic, sharing memory between libraries, concrete: two libraries can share an array because they agree on exactly these buffers.

DefinitionValidity bitmap

A buffer with one bit per element: 1 means the value is present, 0 means null. It lets a column of numbers have holes without reserving a special number for "missing", and it is omitted entirely when nothing is null.

02

Sizes and bitmaps

An int64 array with nulls has two buffers: the validity bitmap and the 8-byte values. Ten values need a two-byte bitmap, because eight bits fit in a byte and the last two spill into a second. Null positions still occupy their 8 bytes in the values buffer, holding unspecified bytes that readers must ignore. To know whether element i is null, read bit i % 8 of byte i // 8.

main.pyPython
# Ten prices with three nulls: two buffers, and the bitmap read back bit by bitprint("sizes:", buffer_sizes(prices), "(bitmap bytes, value bytes)")print("bits :", validity_bits(prices))print("slice of 4 starting at 3:", validity_bits(prices.slice(3, 4)))
sizes: [2, 80] (bitmap bytes, value bytes)
bits : [1, 0, 1, 1, 0, 1, 1, 1, 1, 0]
slice of 4 starting at 3: [1, 0, 1, 1]
03

Strings: offsets and one data buffer

A string column cannot be fixed-width, so Arrow stores all the characters in one data buffer, back to back, and adds an offsets buffer with n + 1 entries: string i is the bytes from offsets[i] to offsets[i + 1]. A null or an empty string makes two equal offsets. Offsets count bytes, which is why 日本 takes six. To find a string you do two reads and a slice, no search, and string_at does exactly that.

main.pyPython
# Five strings, one of them null, one empty, one non-ASCII: the offsets, then each string read from the raw buffersprint(string_offsets(names))for i in range(len(names)):    print(i, repr(string_at(names, i)))
[0, 4, 4, 6, 6, 12]
0 'AAPL'
1 None
2 'KO'
3 ''
4 '日本'
main.pyPython
# The real symbol column: 6,699 AAPL rows need almost no text, but still a 4-byte offset eachprint(len(symbols), "elements; buffers (bitmap, offsets, data):", buffer_sizes(symbols))print("offsets:", 4 * (len(symbols) + 1), "bytes = 4 x (n + 1) | data:", 4 * len(symbols), "bytes = 4 characters x n")
6699 elements; buffers (bitmap, offsets, data): [0, 26800, 26796]
offsets: 26800 bytes = 4 x (n + 1) | data: 26796 bytes = 4 characters x n
Dictionary encoding is the fix for repeated text

A symbol column pays offsets and characters for every row even though it holds five distinct values. Topic 7 shows the Arrow answer, the same idea as in Parquet: store each distinct string once and keep a small integer per row.

04

What the tasks ask

  1. 1
    `buffer_sizes`array.buffers() returns the buffers or None; use buffer.size and count a missing one as 0.
  2. 2
    `validity_bits`memoryview(bitmap); element i is (raw[(offset + i) >> 3] >> ((offset + i) & 7)) & 1; no bitmap means all present.
  3. 3
    `string_offsets` and `string_at`memoryview(offsets_buffer).cast("i") views the offsets as 32-bit integers; the string is bytes(memoryview(data)[a:b]).decode(); check the bitmap first for nulls.

Key takeaways

  • An array is a few buffers Validity, values, offsets and data; the specification fixes where everything is.
  • Nulls cost a bit One bit per element, and no bitmap at all when nothing is null.
  • Strings are offsets into one block Finding one is arithmetic, not a search. Next: sharing these buffers between libraries.
Quick check
A string array has 100 elements and its offsets buffer starts [0, 4, 4, 6, ...]. What do the first two strings look like?
Pick an answer.

Lab · what's graded

4 tasks · 100 pts
  • Weigh the buffers: buffer_sizes25 pts
  • Read the validity bitmap: validity_bits25 pts
  • Read the offsets: string_offsets25 pts
  • Pull one string out: string_at25 pts
Explorer · lab-4-1
▾ lab-4-1
▾ data
…
bash · in-browser
learner@quantcraft:~/lab-4-1$
Python not loaded yetPythonLn 1, Col 1Spaces: 4UTF-8

Reset this topic?

Everything in lab-4-1 goes back to the starter files and the terminal is cleared. Your current solution will be lost — this can't be undone.

Will be reset
  • main.py → starter code
  • Terminal history and test output
  • Unsubmitted changes
Will be kept
  • Best lab score
  • Your XP and streak
  • Lecture progress and notes