From owner-freebsd-hackers@freebsd.org Sun Jan 6 17:17:24 2019 Return-Path: Delivered-To: freebsd-hackers@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 600A11494A03 for ; Sun, 6 Jan 2019 17:17:24 +0000 (UTC) (envelope-from asomers@gmail.com) Received: from mail-lf1-f43.google.com (mail-lf1-f43.google.com [209.85.167.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 7647B865F2 for ; Sun, 6 Jan 2019 17:17:23 +0000 (UTC) (envelope-from asomers@gmail.com) Received: by mail-lf1-f43.google.com with SMTP id a16so28560679lfg.3 for ; Sun, 06 Jan 2019 09:17:23 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=HPKvIzOT3L1PvQC4C8zBpAjpThfsiTtn11vdkvAO8Q8=; b=KURgT/yR2kM5WLCUlGYoVRwEITMkoZfrGxrB6XtY8c3VaTh/8W6jqLKRMM48d6p6m/ qSlNftG9kA6ZxJeAu7WGLlq8gWMWaK8xuP8dMPsRJ394NaFX0aSbltJQ4Tdsbc58Gduf BYaVOzdg0KJUAQsifbdbQiTDjcPsuwW3nk9kaP1wQfgcaEUiIdym1Y9Ra+f0y5I2PTE7 fiiqHb7qB0RH92AS/lRtWM9q38tm35LVwX7Fvose0NRMlFkZTbFZCzt6nMqLP3+KvHkq qjJDxG6e58wxC41kR/IhKMz0OaZGFdtf7Gmqvx3/0xbzfSnO+8L0S0uYseUNA1ajmACi wLnA== X-Gm-Message-State: AA+aEWbX71/T2ikRE28wGY+J08l/eEZh8JlB/P2Z8voMFTuasTxsAqsR EI15sMNPC3vp/GZnpt7qybUXLULvNRPcocGUHic= X-Google-Smtp-Source: AFSGD/VvvAT7/c0ls2vQhHwnlvE/b3ANKVqxswG0AXWL2Z7dbFZrdf9Z6j543a55y5yjYk4+zefX0YaD0c5/4JCtG6M= X-Received: by 2002:a19:200b:: with SMTP id g11mr28268864lfg.58.1546795036380; Sun, 06 Jan 2019 09:17:16 -0800 (PST) MIME-Version: 1.0 References: <7d7bc47d-04cf-2f9b-00a3-e3d9d92b3623@aceshardware.com> <72922F2C-9D27-47AA-BB1C-2DA8589CF008@rpi.edu> <92bd5362-d898-aa12-8f3d-9fbe23f38e0c@aceshardware.com> <26325c0b-4960-7739-72aa-c31c4e0638d3@aceshardware.com> In-Reply-To: <26325c0b-4960-7739-72aa-c31c4e0638d3@aceshardware.com> From: Alan Somers Date: Sun, 6 Jan 2019 10:17:04 -0700 Message-ID: Subject: Re: Speculative: Rust for base system components To: Brian Neal Cc: "freebsd-hackers@freebsd.org" , Garance A Drosehn Content-Type: text/plain; charset="UTF-8" X-Rspamd-Queue-Id: 7647B865F2 X-Spamd-Bar: --- Authentication-Results: mx1.freebsd.org; spf=pass (mx1.freebsd.org: domain of asomers@gmail.com designates 209.85.167.43 as permitted sender) smtp.mailfrom=asomers@gmail.com X-Spamd-Result: default: False [-3.92 / 15.00]; ARC_NA(0.00)[]; TO_DN_EQ_ADDR_SOME(0.00)[]; NEURAL_HAM_MEDIUM(-1.00)[-0.999,0]; FROM_HAS_DN(0.00)[]; RCPT_COUNT_THREE(0.00)[3]; R_SPF_ALLOW(-0.20)[+ip4:209.85.128.0/17]; NEURAL_HAM_LONG(-1.00)[-0.999,0]; MIME_GOOD(-0.10)[text/plain]; PREVIOUSLY_DELIVERED(0.00)[freebsd-hackers@freebsd.org]; DMARC_NA(0.00)[freebsd.org]; TO_DN_SOME(0.00)[]; MIME_TRACE(0.00)[0:+]; TO_MATCH_ENVRCPT_SOME(0.00)[]; MX_GOOD(-0.01)[cached: alt3.gmail-smtp-in.l.google.com]; NEURAL_HAM_SHORT(-0.80)[-0.803,0]; RCVD_IN_DNSWL_NONE(0.00)[43.167.85.209.list.dnswl.org : 127.0.5.0]; RCVD_TLS_LAST(0.00)[]; FORGED_SENDER(0.30)[asomers@freebsd.org,asomers@gmail.com]; R_DKIM_NA(0.00)[]; FREEMAIL_ENVFROM(0.00)[gmail.com]; ASN(0.00)[asn:15169, ipnet:209.85.128.0/17, country:US]; FROM_NEQ_ENVFROM(0.00)[asomers@freebsd.org,asomers@gmail.com]; IP_SCORE(-1.11)[ipnet: 209.85.128.0/17(-3.78), asn: 15169(-1.67), country: US(-0.08)]; RCVD_COUNT_TWO(0.00)[2] X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 06 Jan 2019 17:17:24 -0000 On Sun, Jan 6, 2019 at 9:54 AM Brian Neal wrote: > > I don't have it anymore, but yours is close enough. When compiling your > first example, I get 156 instructions, including map iterators and rust > result assert/unwrap logic. Your second example produces 21 lines, > including a loop iterator: > > rustc 1.31.0 with -C opt-level=s: > > ::into_iter: > mov eax, edi > mov edx, esi > ret > example::oddcount2: > xor eax, eax > test edi, edi > jle .LBB1_3 > xor ecx, ecx > .LBB1_2: > mov edx, ecx > and edx, 1 > neg edx > and edx, ecx > lea esi, [rcx + 1] > add eax, edx > mov ecx, esi > cmp edi, esi > jne .LBB1_2 > .LBB1_3: > ret Those 21 lines aren't 21 instructions; you're counting labels. Also, the first three instructions aren't actually part of the function. They're dead code, and should be eliminate by LTO. However, Rust doesn't do LTO when compiling libraries; only when linking executables. The unwrap logic, etc is also not part of the function. So in this example, Rust produces only a few more instructions than C. Also, FYI the Rust expression "0..num" is exclusive on the right. It's equivalent to C's "for (int i = 0; i < num; i++)", though that doesn't change the instruction count. -Alan > > Converting to C: > > int oddcount2(int num) { > int sum = 0; > for (int i = 0; i <= num; ++i) > { > if (i % 2 > 0) > { > sum += i; > } > } > return sum; > } > > I get the following with gcc 8.2 using -Os: > > oddcount2(int): > xor edx, edx > xor eax, eax > .L4: > cmp edx, edi > jg .L1 > test dl, 1 > je .L3 > add eax, edx > .L3: > inc edx > jmp .L4 > .L1: > ret > > Cheers, > > -Brian > > On 1/6/2019 8:01 AM, Alan Somers wrote: > > I get different results on Godbolt. I don't know exactly what your > > program was, but I tried to recreate it from your description. I > > wrote it in two ways. With opt-level=s, I got 13 instructions for one > > version, and 16 for the other. With opt-level=3, I got vectorized > > code for both. Here's my code: > > > > pub fn oddcount(num: i32) -> i32 { > > (0..num).map(|i| { > > if i % 2 > 0 { > > i > > } else { > > 0 > > } > > }).sum() > > } > > > > pub fn oddcount2(num: i32) -> i32 { > > let mut sum = 0; > > for i in 0..num { > > if i % 2 > 0 { > > sum += i; > > } > > } > > sum > > } > > > > -Alan > > _______________________________________________ > > freebsd-hackers@freebsd.org mailing list > > https://lists.freebsd.org/mailman/listinfo/freebsd-hackers > > To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@freebsd.org" > >