Skip to content

Optimize CommandLineV1Flag type dispatch, validation, and FlagSaver. - #1983

Draft
copybara-service[bot] wants to merge 1 commit into
mainfrom
test_989386175
Draft

copybara-service[bot] wants to merge 1 commit into
mainfrom
test_989386175

Conversation

@copybara-service

Copy link
Copy Markdown

Optimize CommandLineV1Flag type dispatch, validation, and FlagSaver.

  1. Replace IsOfType<T>() (up to 6 virtual TypeId() calls + 6 indirect
    op_(kFastTypeId) calls) in ChangedDirectly,
    CommandLineV1Flag::Typename(), and CommandLineV1Flag::InvokeValidator()
    with direct op == &absl_flags::FlagOps<T> pointer comparisons.
  2. Add !inited_.load(std::memory_order_acquire) fast paths to
    CommandLineV1Flag::ValidateDefaultValue(), IsModified(),
    IsSpecifiedOnCommandLine(), and HasValidatorFn(), avoiding lazily
    allocating and locking ~8,000 absl::Mutexes during flag initialization.
  3. Return a sentinel V1FlagState (current_ == nullptr) from
    CommandLineV1Flag::SaveState() when unmodified (!inited_ and
    !ChangedDirectly(op_, def_, cur_)), avoiding DataGuard() mutex
    allocation/locking and ~8,000 Clone(op_, cur_) heap allocations/deletions
    per FlagSaver scope while preserving restoration across direct variable
    mutations and SET_FLAGS_DEFAULT.

1. Replace `IsOfType<T>()` (up to 6 virtual `TypeId()` calls + 6 indirect
   `op_(kFastTypeId)` calls) in `ChangedDirectly`,
   `CommandLineV1Flag::Typename()`, and `CommandLineV1Flag::InvokeValidator()`
   with direct `op == &absl_flags::FlagOps<T>` pointer comparisons.
2. Add `!inited_.load(std::memory_order_acquire)` fast paths to
   `CommandLineV1Flag::ValidateDefaultValue()`, `IsModified()`,
   `IsSpecifiedOnCommandLine()`, and `HasValidatorFn()`, avoiding lazily
   allocating and locking ~8,000 `absl::Mutex`es during flag initialization.
3. Return a sentinel `V1FlagState` (`current_ == nullptr`) from
   `CommandLineV1Flag::SaveState()` when unmodified (`!inited_` and
   `!ChangedDirectly(op_, def_, cur_)`), avoiding `DataGuard()` mutex
   allocation/locking and ~8,000 `Clone(op_, cur_)` heap allocations/deletions
   per `FlagSaver` scope while preserving restoration across direct variable
   mutations and `SET_FLAGS_DEFAULT`.

PiperOrigin-RevId: 989386175
Sign up for free to 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.

1 participant