Avoid mutating Label#title attribute when already stripped
What does this MR do?
BaseLabel#strip_whitespace_from_title is a before_validation callback that writes self[:title] = title.strip unconditionally — including when the title is already stripped (the dominant case after the first validation pass).
This MR guards the attribute write so it only fires when the strip actually changes the value:
def strip_whitespace_from_title
return unless title
stripped = title.strip
return if stripped == title
self[:title] = stripped
endFunctionally equivalent, but skips a needless _write_attribute call on every subsequent validate.
Why?
This is the third-highest-volume mutation site surfaced by the freeze-flip probe (#600433 (closed)). When a let_it_be(:label) subject is cached across tests and re-validated, the unconditional write triggers _write_attribute on the frozen instance, raising FrozenError.
Empirically validated: the freeze-flip probe (!236505 (closed)) showed 1,520 occurrences of FrozenError originating from this exact line (app/models/concerns/base_label.rb:67). A validation MR (!236528 (closed)) layered this fix together with two others on top of the probe, and the probe pipeline's failure count dropped from 665 → 183 (a 72.5% reduction), with zero remaining occurrences of this site in the trace samples.
It's also a tiny quality win independently of the freeze rollout: the in-memory mutation on every validate is wasted work.
Verification
- Local:
bundle exec rspec spec/models/concerns/base_label_spec.rb - This MR's CI run.
- Validation evidence: !236528 (closed) (compare https://gitlab.com/gitlab-org/gitlab/-/pipelines/2533568410 baseline vs https://gitlab.com/gitlab-org/gitlab/-/pipelines/2534042883 with this fix layered).
References
- #600433 (closed) (production-code mutation tracking issue)
- #600267 (closed) (let_it_be freeze rollout)
- !236505 (closed) (probe MR)
- !236528 (closed) (validation MR)
/cc @pedropombeiro