Repository: carbon-language/carbon-lang
File: toolchain/check/eval.cpp (C++)

Code before:
                           .facet_type_id = id},
          phase);
    }

    case SemIR::BuiltinFunctionKind::PrimitiveCopy: {
      return context.constant_values().Get(arg_ids[0]);
    }

    case SemIR::BuiltinFunctionKind::StringAt: {
      Phase phase = Phase::Concrete;
      auto str_id = GetConstantValue(eval_context, arg_ids[0], &phase);
      auto index_id = GetConstantValue(eval_context, arg_ids[1], &phase);

      if (phase != Phase::Concrete) {
        return MakeNonConstantResult(phase);
      }

      auto str_struct = eval_context.insts().GetAs<SemIR::StructValue>(str_id);
      auto elements = eval_context.inst_blocks().Get(str_struct.elements_id);
      CARBON_CHECK(elements.size() == 2, "String struct should have 2 fields.");

      auto ptr_const_id = eval_context.constant_values().Get(elements[0]);
      auto string_literal = eval_context.insts().GetAs<SemIR::StringLiteral>(
          eval_context.constant_values().GetInstId(ptr_const_id));

      auto string_value = eval_context.sem_ir().string_literal_values().Get(
          string_literal.string_literal_id);

      auto index_inst = eval_context.insts().GetAs<SemIR::IntValue>(index_id);
      const auto& index_val = eval_context.ints().Get(index_inst.int_id);

      auto char_value =
          static_cast<uint8_t>(string_value[index_val.getZExtValue()]);

      auto int_id = eval_context.ints().Add(
          llvm::APSInt(llvm::APInt(32, char_value), /*isUnsigned=*/false));
      return MakeConstantResult(
          eval_context.context(),
          SemIR::IntValue{.type_id = call.type_id, .int_id = int_id}, phase);
    }

    case SemIR::BuiltinFunctionKind::PrintChar:
    case SemIR::BuiltinFunctionKind::PrintInt:
    case SemIR::BuiltinFunctionKind::ReadChar:
    case SemIR::BuiltinFunctionKind::FloatAddAssign:
    case SemIR::BuiltinFunctionKind::FloatSubAssign:
    case SemIR::BuiltinFunctionKind::FloatMulAssign:
    case SemIR::BuiltinFunctionKind::FloatDivAssign:
    case SemIR::BuiltinFunctionKind::IntSAddAssign:
    case SemIR::BuiltinFunctionKind::IntSSubAssign:
    case SemIR::BuiltinFunctionKind::IntSMulAssign:

Review comment (suggestion):
```suggestion
      const auto& string_value = eval_context.sem_ir().string_literal_values().Get(
```
Avoid copying the string

Code after:
                                       SemIR::LocId loc_id, SemIR::Call call,
                                       SemIR::BuiltinFunctionKind builtin_kind,
                                       llvm::ArrayRef<SemIR::InstId> arg_ids,
                                       Phase phase) -> SemIR::ConstantId {
  auto& context = eval_context.context();
  switch (builtin_kind) {
    case SemIR::BuiltinFunctionKind::None:
      CARBON_FATAL("Not a builtin function.");

    case SemIR::BuiltinFunctionKind::NoOp:
    case SemIR::BuiltinFunctionKind::TypeDestroy: {
      // Return an empty tuple value.
      auto type_id = GetTupleType(eval_context.context(), {});
      return MakeConstantResult(
          eval_context.context(),
          SemIR::TupleValue{.type_id = type_id,
                            .elements_id = SemIR::InstBlockId::Empty},
          phase);
    }

    case SemIR::BuiltinFunctionKind::TypeCanDestroy: {
      CARBON_CHECK(arg_ids.empty());
      auto id = eval_context.facet_types().Add(
          {.builtin_constraint_mask =
               SemIR::BuiltinConstraintMask::TypeCanDestroy});
      return MakeConstantResult(
          eval_context.context(),
          SemIR::FacetType{.type_id = SemIR::TypeType::TypeId,
                           .facet_type_id = id},
          phase);
    }

    case SemIR::BuiltinFunctionKind::PrimitiveCopy: {
      return context.constant_values().Get(arg_ids[0]);
    }

    case SemIR::BuiltinFunctionKind::StringAt: {
      Phase phase = Phase::Concrete;
      auto str_id = GetConstantValue(eval_context, arg_ids[0], &phase);
      auto index_id = GetConstantValue(eval_context, arg_ids[1], &phase);

      if (phase != Phase::Concrete) {
        return MakeNonConstantResult(phase);
      }

      auto str_struct = eval_context.insts().GetAs<SemIR::StructValue>(str_id);
      auto elements = eval_context.inst_blocks().Get(str_struct.elements_id);
      // String struct has two fields: a pointer to the string data and the
      // length.
      CARBON_CHECK(elements.size() == 2, "String struct should have 2 fields.");